Sign in to follow this  
violentcrayon

[C#] First Game- A Few Questions

Recommended Posts

Okay...these are probably pretty basic, but it's been a rather annoying process. I've tried Googling it to no avail. Basically, I've been learning C# (my first language) for about 4 days now and I feel that I've gained enough basic knowledge to begin work on my first application- a text-based RPG. However, I've run into a minor problem.
...
damage  = rand.Next(4);
...
public static int totalDamage = damage - Player.Character.armor;
...
Character.health -= Monsters.totalDamage;
...
That's the code I'm basically having problems with. What happens is that sometimes, the monster deals damage below zero, which results in adding to the player's health pool. I tried changing the variable type to byte, but then I ran into ridiculously high numbers being generated. How can I make it so that the totalDamage variable can only equal a positive number? The second question regards a random event. Basically, I'd like the character to randomly gain a sickness that reduces his stats. I was thinking of creating a timer and then using rand.Next()...ie:
public void Timer()
{
//code for timer
}

if (Timer >= rand.Next(100))
{
GiveAilment();
}
The problem with that is I don't know how to make that if statement run every, say, 5 minutes. Thanks for any help!

Share this post


Link to post
Share on other sites
Quote:

How can I make it so that the totalDamage variable can only equal a positive number?


C# supports unsigned variables, but that likely won't solve your problem. The problem is that you're doing -= damage no matter what. You're probably better off with a quick if statement there to just not do it if the number is invalid (negative).

Quote:


The problem with that is I don't know how to make that if statement run every, say, 5 minutes.


This is a bit more tricky, and arguably not what you want. Ignoring that... the easiest way would likely to put some stuff in your main loop to check if 5 minutes has passed since the last time you randomly fubar'd the character, and trigger if it has.

Share this post


Link to post
Share on other sites
Thanks for the prompt reply! I'll be implementing the first fix in a moment. I figured I could use an if statement to skip the damage portion, but I didn't know if there was a way of doing it just by changing the variable type.

Regarding the disease thing...why would it be something not to do? Also, I don't exactly have a loop...it's just kind of...an enless chain of methods. >< As I said, I just started a few days ago, and I don't have any books on it. Basically, I know a whole lot of nothing. Everything actually works, though, except for that minor detail which will be corrected shortly.

Share this post


Link to post
Share on other sites
With the way you're implementing damage and armor, you'll have to check to see if the damage is less than the armor value.. something like this:

if (character.armor > damage) {
totaldamage = 0;
}
else {
totaldamage = damage - character.armor;
}

This will ensure you don't ever add health to the character in the event that the initial damage is less than the armor value.

As for the timer, I'm not a C# programmer, but you could just have frequent checks throughout the program (after a round of the fight, after a movement to a new location) to peek at the current TIME (in s), and after 5 mins have gone by from the previous timed event, then GiveAilment(). Since this is text-based and probably round-based instead of real-time, having many timer checks through the code won't affect performance..


You just mentioned you're not using any loops? You'll want to learn those now - nearly all programs use them in some fashion.

For example:

Start game
Make/load character
Begin game loop
//game code (travel, etc) until fight happens
Begin fight loop
//fight code
end fight loop when character or monster dies
End game loop when user exits game (dies, logs off, etc)

Share this post


Link to post
Share on other sites
Quote:

How can I make it so that the totalDamage variable can only equal a positive number?


Write a little method to clamp the damage value between 0 and whatever the maximum is. You could also store the damage as an unsigned type, but beware that you'll get compiler errors related to requiring explicit casts if you do so, and if you perform the casts you'll end up in the same boat. Besides in some cases it may be desirable to allow "negative" damages.

Quote:

The problem with that is I don't know how to make that if statement run every, say, 5 minutes.

Once you have your timer/clock object, ask it for the current time every frame, and use that plus the current time from the last frame to figure out how much time has elapsed. Accumulate and store that elapsed time someplace, and when it is greater than or equal to five minutes, execute the code.

There are built-in timer methods in the .NET frameowkr, but many of them are designed for threading stuff and/or for low-priority repeating events that don't need a lot of accuracy. Often you are better off implementing the timer yourself using lower-level functions (p/invoking QueryPerformanceCounter or timeGetTime are popular). Google for C# timing methods, you'll get a lot of results.

Share this post


Link to post
Share on other sites
Quote:
Original post by thesilencer
You just mentioned you're not using any loops? You'll want to learn those now - nearly all programs use them in some fashion.


I am using loops, but not like a main game loop. For example:

I have my fight sequence method called BattleEncounter(). Original, huh? Anyway. In it, there is a while loop that runs the entire loop as long as the monster's hit points are greater than zero. Then the first statement is an if statement to check if the player's hit points reach zero. If they do, it breaks the loops, displays a message, and ends the game.

But as far as a main game loop, there isn't one. I haven't written the exploration code yet. I'm focusing on combat system at the moment. The plan was to write the story in an XML file then parse the XML file for the story related output. Then whenever there is a part that needs to be explored, I'd need to write a namespace called World. Then I would have an Explore class, where I would have different classes that would print text telling the user if he could go N, S, W, or E. Then use a switch statement to go to the next room. Each room would reside within a class called Rooms, and each room would be a method.

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