I'm making a fighting game and I want to have the game loop until one person's HP is less than 1 (equal to 0). How do you do that?
I'm making a fighting game and I want to have the game loop until one person's HP is less than 1 (equal to 0). How do you do that?
You can put the variable in the scope of the loop (which I don't recommend) or you can have a pointer to the health in the scope of the loop (which I also don't recommend), or you can call a function during the loop that checks the players health and use that for comparisons (which I do recommend ).
Oh, what I mean is when I loop, the change doesn't register.
? Post an example. That can be any number of problems then.
not quite sure what u mean either but i'm in a posting mood tonight!
When i did my first game i did the health/game loop in the same deal, its buggy but its basic, example:
Code:do { body of game and all the junk; } while(health != 0);
The real question here is fuh, do you pronounce your name as if it rhymes with "duh" or would you pronounce it "foo?"
Hmm not a very hard question! In my game I have a similar thing for the monsters life just use a while loop:
Code:struct new { unsigned short int health; }; new user; user.health = 10; while(user.health != 0) { //your code }
new is a C++ keyword and a wouldn't recommend using it as an indentifier.Originally posted by devour89
Hmm not a very hard question! In my game I have a similar thing for the monsters life just use a while loop:
Code:struct new { unsigned short int health; }; new user; user.health = 10; while(user.health != 0) { //your code }
Everyone wanted the code, so:
<code>
#include <stdlib.h>
#include <iostream.h>
int main()
{
int health;
int foe_health;
int damage;
int foe_damage;
int attack;
int l;
cout<<"How much health should you and the opponent have? ";
cin>>health;
foe_health=health;
cout<<"\nThe fight begins!";
do
do
do
{
randomize();
cout<<"Press 1 for punch, 2 for kick, and 3 for blast. ";
cin>>attack;
if (attack==1)
{
damage=rand () % 6;
foe_damage=rand () % 7;
cout<<"You punched and did "<<damage<<" damage and the opponent did "<<foe_damage<<" damage.";
foe_health-damage;
health-foe_damage;
cout<<"You have "<<health<<" health and the opponent has "<<foe_health<<" health.";
}
if (attack==2)
{
damage=rand () % 10;
foe_damage=rand () % 11;
cout<<"You kicked and did "<<damage<<" damage and the opponent did "<<foe_damage<<" damage.";
oppenent_health-damage;
health-foe_damage;
cout<<"You have "<<health<<" health and the opponent has "<<foe_health<<" health.";
}
if (attack==3)
{
damage=rand () % 20;
foe_damage=rand () % 20;
cout<<"You blasted and did "<<damage<<" damage and the opponent did "<<foe_damage<<" damage.";
foe_health-damage;
health-foe_damage;
cout<<"You have "<<health<<" health and the opponent has "<<foe_health<<" health.";
}
if (attack==4)
{
damage=foe_health;
cout<<"You found the destroy move. To do it again, enter 4 at the attack screen. You did "<<foe_health<<" damage\n";
oppenent_health-damage;
health-foe_damage;
cout<<"You have "<<health<<" health and the opponent has "<<foe_health<<" health.";
}
{
cout<<"\nTo leave, enter 123. Any other number will continue.\n";
cin>>l;
}
}
while (l!=123);
while (health>0)
while (foe_health>0)
return 0;
}
</code>
Could you please post what I can do to make it so when it loops, it does the damage and keeps it for the next loop?
A Statement like these are useless, because it doesn't do anything to health, it only returns the result of the subtraction, if you wanted to do like this:oppenent_health-damage;
health-foe_damage;
that would desplay the result of the subtraction, but if you want to decrement the health, you should do this:Code:cout << health-foe_damage;
I think this is what you were asking for, isn't it?Code:health-=foe_damage; //equavelant to healt = health - foe_damage;
Note:
I don't get the use of the while loops at the end of the program,
I think they are uselesswhile (l!=123);
while (health>0)
while (foe_health>0)
none...
I think you should make use of getch(), so that when the user enters 1, 2, or 3, he doesn't have to press enter, just when he enters it, your program will read it.
none...
I think you should use a while loop instead of three do while loops.
Much easier to understandCode:while (l!=123 || health>0 || foe_health>0) { //The fight code }