Sign in to follow this  
FlawleX

C# - Combat system?

Recommended Posts

Hello!

I recently started working on a text based RPG game assignment written in C# and it was going well untill i reached the combat part. I really don't know how to do it and i can't seem to find any solution for it.
This is my first C# project aswell and im new to programming in general.

I was going for a random number generator for the damage but it didn't work out so good so i scraped that but it still won't work!

The issue is that the boss HP won't go down! It becomes like a infinite loop of the same text over and over.
The game is in it's VERY early stages and some of the variables are just placeholders! But i need you guy's help to get past this problem!

The game files currently being used (Lot's of placeholder names and some unused variables):

Program.cs (Main file) : http://pastebin.com/TEQswSyi
Combat.cs (Some of the combat features): http://pastebin.com/bs1BUWUL
Chroniax.cs (One of the bosses): http://pastebin.com/TaFxgAyV

Share this post


Link to post
Share on other sites
[quote name='FlawleX' timestamp='1317586485' post='4868363']
Hello!

I recently started working on a text based RPG game assignment written in C# and it was going well untill i reached the combat part. I really don't know how to do it and i can't seem to find any solution for it.
This is my first C# project aswell and im new to programming in general.

I was going for a random number generator for the damage but it didn't work out so good so i scraped that but it still won't work!

The issue is that the boss HP won't go down! It becomes like a infinite loop of the same text over and over.
The game is in it's VERY early stages and some of the variables are just placeholders! But i need you guy's help to get past this problem!

The game files currently being used (Lot's of placeholder names and some unused variables):

Program.cs (Main file) : [url="http://pastebin.com/TEQswSyi"]http://pastebin.com/TEQswSyi[/url]
Combat.cs (Some of the combat features): [url="http://pastebin.com/bs1BUWUL"]http://pastebin.com/bs1BUWUL[/url]
Chroniax.cs (One of the bosses): [url="http://pastebin.com/TaFxgAyV"]http://pastebin.com/TaFxgAyV[/url]
[/quote]



Well, congratulations on starting programming.

The specific problem you are having of "The issue is that the boss HP won't go down! " is because you never actually decrease the bosses hp's in the attack loop, so it will go over and over and over.

From what I can see, you should research a couple things. First, look into constructors with a bit more detail.

For example, I don't think this code does what you think it should:

[code][color=#181818][font=monospace][list=1][*] public Chroniax()[*] {[*] userInfo information = new userInfo();[*] Combat combatFunctions = new Combat();[*][*] Random chroniaxTotalDamage = new Random();[*][*] chroniaxTD = chroniaxTotalDamage.Next(chroniaxAttackPower, 700);[*][*] chroniaxAttackPower = 400;[*] chroniaxFullHealth = 1000;[*][*] chroniaxFullHealth -= combatFunctions.heavyDMG;[*][*] /*[*] chroniaxHealth = chroniaxFullHealth -= combatFunctions.heavyDMG;[*] */[*] }[/list][/font][/color]
[/code]



A constructor is called once and only once, when the item is created. There "[color=#181818][font=monospace]chroniaxFullHealth -= combatFunctions.heavyDMG;" doing that makes no sense.[/font][/color]
[color=#181818][font=monospace]
[/font][/color]
[color=#181818][font=monospace]The biggest catch though is in your combat loop:[/font][/color]
[color=#181818][font=monospace]
[/font][/color]
[color=#181818][font=monospace][code][/font][/color][color=#181818][font=monospace][list=1][*]while (bossAlive == true)[*] {[*][*] if (chroniax.chroniaxFullHealth < 0)[*] {[*] bossAlive = false;[*] }[*][*] Console.WriteLine(combatFunctions.warriorAbilities);[*][*] try[*] {[*] abilityChoice = int.Parse(Console.ReadLine());[*] }[*] catch[*] {[*] Console.ForegroundColor = ConsoleColor.Red;[*] Console.WriteLine("An error occured! Please try again!");[*] Console.ForegroundColor = ConsoleColor.Gray;[*] Console.WriteLine("\nPress Enter to continue...");[*] }[*][*][*] if (abilityChoice == 1)[*] {[*][*] Console.WriteLine("You hit the boss for: " + combatFunctions.heavyDMG);[*] Console.WriteLine("The boss has {0} HP left", chroniax.chroniaxFullHealth);[*][*] }[*][*] if (abilityChoice == 2)[*] {[*][*] Console.WriteLine("You hit the boss for: " + combatFunctions.mortalDMG);[*] Console.WriteLine("The boss has {0} HP left", chroniax.chroniaxFullHealth);[*][*] }[*][*] if (abilityChoice == 3)[*] {[*][*] Console.WriteLine("You hit the boss for: " + combatFunctions.heroicDMG);[*] Console.WriteLine("The boss has {0} HP left", chroniax.chroniaxFullHealth);[*] }[*][*] Console.WriteLine("The boss swings you for {0} damage", chroniax.chroniaxTD);[*] Console.ReadKey();[*][*] }[*][*] }[*][/list][/font][/color]
[color=#181818][font=monospace][/code]
[/font][/color]
[color=#181818][font=monospace]
[/font][/color]
[color=#181818][font=monospace]
[/font][/color]
[color=#181818][font=monospace]What you need to do is actually apply the damage against the boss object. First off, you have two health members and one doesn't seem to be used, so I will go with the one you did:[/font][/color]
[color=#181818][font=monospace]
[/font][/color]
[color=#181818][font=monospace]public int chroniaxFullHealth {get; protected set;}[/font][/color]
[color=#181818][font=monospace]
[/font][/color]
[color=#181818][font=monospace]What you want to do is change that to [/font][/color]
[color=#181818][font=monospace]
[/font][/color]
[color=#181818][font=monospace]public int chroniaxFullHealth {get; set;}[/font][/color]
[color=#181818][font=monospace]
[/font][/color]
[color=#181818][font=monospace]So that this value can be changed by code outside the [/font][/color][color=#181818][font=monospace]Chroniax class.[/font][/color]
[color=#181818][font=monospace]
[/font][/color]
[color=#181818][font=monospace]Then in place of: [/font][/color]
[color=#181818][font=monospace][code][/font][/color][font="monospace"][color="#181818"]if (abilityChoice == 1)[/color][/font]
[font="monospace"][color="#181818"] {[/color][/font]
[font="monospace"][color="#181818"]
[/color][/font]
[font="monospace"][color="#181818"] Console.WriteLine("You hit the boss for: " + combatFunctions.heavyDMG);[/color][/font]
[font="monospace"][color="#181818"] Console.WriteLine("The boss has {0} HP left", chroniax.chroniaxFullHealth);[/color][/font]
[font="monospace"][color="#181818"]
[/color][/font]
[font="monospace"][color="#181818"] }[/color][/font][color=#181818][font=monospace][/code]
[/font][/color]
[color=#181818][font=monospace]
[/font][/color]
[color=#181818][font=monospace]You want:[/font][/color]
[color=#181818][font=monospace][code][/font][/color][font="monospace"][color="#181818"]if (abilityChoice == 1)[/color][/font]
[font="monospace"][color="#181818"] {[/color][/font]
[font="monospace"][color="#181818"]
[/color][/font]
[font="monospace"][color="#181818"] Console.WriteLine("You hit the boss for: " + combatFunctions.heavyDMG);[/color][/font]
[font="monospace"][color="#181818"] chroniax.chroniaxFullHealth -= combatFunctions.heavyDMG;[/color][/font]
[font="monospace"][color="#181818"] Console.WriteLine("The boss has {0} HP left", chroniax.chroniaxFullHealth);[/color][/font]
[font="monospace"][color="#181818"]
[/color][/font]
[font="monospace"][color="#181818"] }[/color][/font][color=#181818][font=monospace][/code]
[/font][/color]
[color=#181818][font=monospace]
[/font][/color]
[color=#181818][font=monospace]
[/font][/color]
[color=#181818][font=monospace]You will want to do the same thing for the various damage types.[/font][/color]
[color=#181818][font=monospace]
[/font][/color]
[color=#181818][font=monospace]There are other issues, but this should at least get it so you bosses HP's eventually go to zero allowing the loop to exit.[/font][/color]

Share this post


Link to post
Share on other sites
[quote name='Serapth' timestamp='1317588087' post='4868380']
God the CODE tag on this site blows. :(

Sorry, don't have time to repaste all the code right now, hope you can make sense of what I was [i]trying [/i]to say.
[/quote]

Thanks alot for your help!

Indeed the code tag isn't so good but i can still read it so thanks again! :D

Share this post


Link to post
Share on other sites
Wow! I would have ran from the freaking [u][i]dragon[/i][/u], too!!! :lol:

Here, let me fix part of your code (which I ran through Notepad first):

[code] if (attackChoice == true && (attackOrFlee == 2))
{
Console.Clear();
Console.WriteLine("WISE decision! Who could possibly stand toe-to-toe with a DRAGON?!");
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("You WIN!!!");
Console.ForegroundColor = ConsoleColor.Gray;
}[/code]
Just kidding with you, man! :)

Sorry that unlike unlike Serapth I don't have a programming answer for you as I am a complete newbie. But, I just wanted to say thanks a lot for sharing that code and for posting. Even though I'm making my first program in Python, it was interesting to see such a simple, basic game just like the one I'm writing.

When I made games with RPG Maker, a front-end for Ruby, the generic advice for non-boss monsters was that you should kill all appropriate monsters in about five hits and all appropriate monsters you fight should kill you in about five hits. That means if you're fighting a "weak" monster, it won't take as many hits to kill it, but if you're fighting a "strong" monster -- i.e., one on a "higher level" than your character is ready to face -- it will take more hits to kill and chances are, it'll kill your character in fewer hits too.

Another generic piece of advice was to have a 20% range above and below base-damage and chance-to-hit stats. So, if your base damage is 100, that means the attack will do between 80 and 120 points of damage, if my math is correct. :wink:

One last piece of generic, standard advice was to have weapons raise the character's base damage by half. So, if his Strength stat (or whatever you use to determine base damage) is 100, a magic sword of normal power level would add 50 to that stat, making his base damage 150.

That's all assuming you're going to have a character that "levels up," or becomes categorically more powerful as your game progresses as classical, console-style RPGs do.

You don't seem to have a combat engine that resembles classic RPGs, though. On first glance, it seems as though you have three combat options, each does more damage than the next... I very well could be wrong, though.


Have you never played Dungeons and Dragons, though? Pretty well all of D&D's stuff is [url="http://en.wikipedia.org/wiki/Open_Game_License"]OGL[/url] and can be accessed here: [url="http://www.d20srd.org/"]http://www.d20srd.org/[/url]

That's basically a complete character generation system, combat engine and compendium of monsters with which almost all sword-and-sorcery players are already familiar.

If you haven't, I doubt you'll want to learn an entire pencil-and-paper RPG system just to make a game, but...


Hope I've helped in some small way!

Thanks again for posting your code. I enjoyed reading it.

Share this post


Link to post
Share on other sites
[quote name='Sharpe' timestamp='1317597831' post='4868426']
Wow! I would have ran from the freaking [u][i]dragon[/i][/u], too!!! :lol:

Here, let me fix part of your code (which I ran through Notepad first):

[code] if (attackChoice == true && (attackOrFlee == 2))
{
Console.Clear();
Console.WriteLine("WISE decision! Who could possibly stand toe-to-toe with a DRAGON?!");
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("You WIN!!!");
Console.ForegroundColor = ConsoleColor.Gray;
}[/code]
Just kidding with you, man! :)

Sorry that unlike unlike Serapth I don't have a programming answer for you as I am a complete newbie. But, I just wanted to say thanks a lot for sharing that code and for posting. Even though I'm making my first program in Python, it was interesting to see such a simple, basic game just like the one I'm writing.

When I made games with RPG Maker, a front-end for Ruby, the generic advice for non-boss monsters was that you should kill all appropriate monsters in about five hits and all appropriate monsters you fight should kill you in about five hits. That means if you're fighting a "weak" monster, it won't take as many hits to kill it, but if you're fighting a "strong" monster -- i.e., one on a "higher level" than your character is ready to face -- it will take more hits to kill and chances are, it'll kill your character in fewer hits too.

Another generic piece of advice was to have a 20% range above and below base-damage and chance-to-hit stats. So, if your base damage is 100, that means the attack will do between 80 and 120 points of damage, if my math is correct. :wink:

One last piece of generic, standard advice was to have weapons raise the character's base damage by half. So, if his Strength stat (or whatever you use to determine base damage) is 100, a magic sword of normal power level would add 50 to that stat, making his base damage 150.

That's all assuming you're going to have a character that "levels up," or becomes categorically more powerful as your game progresses as classical, console-style RPGs do.

You don't seem to have a combat engine that resembles classic RPGs, though. On first glance, it seems as though you have three combat options, each does more damage than the next... I very well could be wrong, though.


Have you never played Dungeons and Dragons, though? Pretty well all of D&D's stuff is [url="http://en.wikipedia.org/wiki/Open_Game_License"]OGL[/url] and can be accessed here: [url="http://www.d20srd.org/"]http://www.d20srd.org/[/url]

That's basically a complete character generation system, combat engine and compendium of monsters with which almost all sword-and-sorcery players are already familiar.

If you haven't, I doubt you'll want to learn an entire pencil-and-paper RPG system just to make a game, but...


Hope I've helped in some small way!

Thanks again for posting your code. I enjoyed reading it.
[/quote]

Thanks for all the advice mate! The combat system is in it's very early stages currently and alot will be changed to make it more dynamic. I will for example not have a set damage value, the reason that it has a set damage value currently is only for testing :)

Share this post


Link to post
Share on other sites
I just ran in to another few problems!

If i use a random number generator to generate the damage for example:

[code]Random heavyStrike = new Random();
int heavyDMG = heavyStrike.Next(charStats.attackPower, charStats.attackDamage);[/code]

I cannot take the damage the player deals - the bosses hp. But when i have a set damage value like lets say heavyDMG = 50; I can just use bossHP -= heavyDMG. But if i have a random number generator it says that i deal 0 damage and obviously the bossHP doesn't go down.

And the code doesn't start if i try to loop the random numbers to get different values everytime. I have even tried activating the loop from a bool when the combat is started and then stop it afterwards but it still doesn't work.

Any ideas on how i can solve this?

This is the important and relevant part of the combat file (im using classes to be able to send variables from file to file that's why it doesn't say int heavyDMG etc):

[code]
Random heavyStrike = new Random();
Random heroicStrike = new Random();
Random mortalStrike = new Random();


heavyDMG = heavyStrike.Next(charStats.attackPower, charStats.attackDamage);
heroicDMG = heroicStrike.Next(charStats.attackPower, charStats.attackDamage);
mortalDMG = mortalStrike.Next(charStats.attackPower, charStats.attackDamage);
[/code]

And the combat part in the main file:
[code]
Console.WriteLine("You hit the boss for: " + combatFunctions.mortalDMG);
chroniax.chroniaxHealth -= combatFunctions.mortalDMG;
Console.WriteLine("The boss has {0} HP left", chroniax.chroniaxHealth);
Console.WriteLine("");
Console.ReadKey();
[/code]

Share this post


Link to post
Share on other sites
I'm not sure I understand what you mean, so I'll try as best I can.

Why can't you simply assign the generated number to the damage properties in the abilities class?
In your code example you create a new variable, but in your code that assigns a pre-defined value you use the public properties.

Also, I don't understand what code "isn't starting" when you're looping. But if you have an issue with random numbers not being random, here's a hint:
The random function in programming functions are not really random, so it's good practice to seed them first. A simple and effective seed is to use the current time, like so:

[code]Random random = new Random(DateTime.Now.Millisecond);[/code]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this