943,860 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 417
  • C# RSS
Jul 3rd, 2009
0

noob question

Expand Post »
Hi guys, i downloaded visual c# 2008 about a month ago and have been trying stuff out for myself.

I'm attempting to make a text based RPG, i made big progress but i'm stuck now.

Basically i want to show the player's HP in green whenever he has 51 to 100% of the maximum HP for his level, yellow from 26 - 50HP and red when below 25%.

My code shows the HP in green from 100% all the way down to 25%, going directly to red and bypassing yellow, how do i fix this?

here's the code.

C# Syntax (Toggle Plain Text)
  1. if (ok.player > 51% ok.maxhp)
  2. {
  3. Console.ForegroundColor = ConsoleColor.Green;
  4. Console.Write("{0}/{1} HP", ok.player, ok.maxhp);
  5. Console.ForegroundColor = ConsoleColor.White;
  6. }
  7. else
  8. if ((ok.player >= 26% ok.maxhp) && (ok.player <= 50% ok.maxhp))
  9. {
  10. Console.ForegroundColor = ConsoleColor.Yellow;
  11. Console.Write("{0}/{1} HP", ok.player, ok.maxhp);
  12. Console.ForegroundColor = ConsoleColor.White;
  13. }
  14. else
  15. if (ok.player < 25% ok.maxhp)
  16. {
  17. Console.ForegroundColor = ConsoleColor.Red;
  18. Console.Write("{0}/{1} HP", ok.player, ok.maxhp);
  19. Console.ForegroundColor = ConsoleColor.White;
  20. }

a person in another forum gave me the following solution, but it didn't work, instead it showed the HP in green from 100 to 0% never changing color.


C# Syntax (Toggle Plain Text)
  1. decimal one_percent = (decimal)(ok.maxhp / 100);
  2. decimal value = (decimal)ok.player;
  3.  
  4. if (value > (one_percent * 51))
  5. {
  6. // ok.player is greater than 51% of ok.maxhp
  7.  
  8. Console.ForegroundColor = ConsoleColor.Green;
  9. Console.Write("{0}/{1} HP", ok.player, ok.maxhp);
  10. Console.ForegroundColor = ConsoleColor.White;
  11. }
  12. else if (value >= (one_percent * 26) && value <= (one_percent * 50))
  13. {
  14. // ok.player is between 26% and 50% of ok.maxhp
  15.  
  16. Console.ForegroundColor = ConsoleColor.Yellow;
  17. Console.Write("{0}/{1} HP", ok.player, ok.maxhp);
  18. Console.ForegroundColor = ConsoleColor.White;
  19. }
  20. else if (value < (one_percent * 25))
  21. {
  22. // ok.player is less than 25% of ok.maxhp
  23.  
  24. Console.ForegroundColor = ConsoleColor.Red;
  25. Console.Write("{0}/{1} HP", ok.player, ok.maxhp);
  26. Console.ForegroundColor = ConsoleColor.White;
  27. }
Similar Threads
Reputation Points: 7
Solved Threads: 2
Light Poster
thanatos1 is offline Offline
25 posts
since Jul 2009
Jul 4th, 2009
0

Re: noob question

I downloaded it about a year ago, still learning but I believe I can help.
Your code : if (ok.player > 51% ok.maxhp)
The % is not what you think a percentage, it is the modulo operator! So you are comparing something against 51 modulo ok.maxhp, which is not what you want.
Other persons code : decimal one_percent = (decimal)(ok.maxhp / 100);
ok.maxhp / 100 is an integer divide, if maxhp is never greater than 100, one_percent will always be zero!
This is my code, hope it helps. You only have to restore the ok variable in it.
c# Syntax (Toggle Plain Text)
  1. double maxhp = 55;
  2. double player = 25;
  3. decimal one_percent = (decimal)(maxhp / 100.0);
  4. decimal value = (decimal)player;
  5.  
  6. if (value > (one_percent * 51))
  7. {
  8. // ok.player is greater than 51% of ok.maxhp
  9. Console.ForegroundColor = ConsoleColor.Green;
  10. Console.Write("{0}/{1} HP", player, maxhp);
  11. }
  12. else if (value >= (one_percent * 26) && value <= (one_percent * 50))
  13. {
  14. // ok.player is between 26% and 50% of ok.maxhp
  15. Console.ForegroundColor = ConsoleColor.Yellow;
  16. Console.Write("{0}/{1} HP", player, maxhp);
  17. }
  18. else if (value < (one_percent * 25))
  19. {
  20. // ok.player is less than 25% of ok.maxhp
  21. Console.ForegroundColor = ConsoleColor.Red;
  22. Console.Write("{0}/{1} HP", player, maxhp);
  23. }
  24. Console.ForegroundColor = ConsoleColor.White;
Reputation Points: 2035
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,740 posts
since Oct 2008
Jul 4th, 2009
0

Re: noob question

Thank you very much for your reply.

I have no doubt that code works, sadly yesterday night after posting the question i changed around some stuff in other code of the same program and now a lot of stuff doesn't work, i hate that i get no error messages, but the game still doesn't work as intended.

Basically i had 2 classes, one full of getters and setters with info, and another called world, where everthing took place in the game. But i wanted to create a class called "fight" and take my fighting code over there to make it a bit more manageable since my "world" class had around 1000 lines of code already.

But some info from the world and "items" class doesn't travel to my fight class, i heard that in c# you're only allowed to send info from 1 class to another and not more, if that's true, do i HAVE to make all my code in 1 class?


I'm starting college in January and i want to at least know a little something before i get there, i think everyone that takes a computer science program already knows a thing or two.
Last edited by thanatos1; Jul 4th, 2009 at 12:33 pm.
Reputation Points: 7
Solved Threads: 2
Light Poster
thanatos1 is offline Offline
25 posts
since Jul 2009
Jul 4th, 2009
0

Re: noob question

>i heard that in c# you're only allowed to send info from 1 class to another and not more

In C# you can't have multiple inheritance, if it is that what you mean. You can have many classes working together if you want to.
Reputation Points: 2035
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,740 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: C# help pls....!!!
Next Thread in C# Forum Timeline: c# client, java server





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC