How do you carry data over when looping?

This is a discussion on How do you carry data over when looping? within the C++ Programming forums, part of the General Programming Boards category; I'm making a fighting game and I want to have the game loop until one person's HP is less than ...

  1. #1
    fuh
    Guest

    Question How do you carry data over when looping?

    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?

  2. #2
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    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 ).

  3. #3
    fuh
    Guest
    Oh, what I mean is when I loop, the change doesn't register.

  4. #4
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    ? Post an example. That can be any number of problems then.

  5. #5
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    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);

  6. #6
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    fuh:
    Post some code...
    none...

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    The real question here is fuh, do you pronounce your name as if it rhymes with "duh" or would you pronounce it "foo?"

  8. #8
    Spanky devour89's Avatar
    Join Date
    Nov 2002
    Posts
    148
    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
    }

  9. #9
    booyakasha
    Join Date
    Nov 2002
    Posts
    208
    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
    }
    new is a C++ keyword and a wouldn't recommend using it as an indentifier.

  10. #10
    fuh
    Guest
    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?

  11. #11
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    oppenent_health-damage;
    health-foe_damage;
    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:
    Code:
    cout << health-foe_damage;
    that would desplay the result of the subtraction, but if you want to decrement the health, you should do this:
    Code:
    health-=foe_damage; //equavelant to healt = health - foe_damage;
    I think this is what you were asking for, isn't it?

    Note:
    I don't get the use of the while loops at the end of the program,
    while (l!=123);
    while (health>0)
    while (foe_health>0)
    I think they are useless
    none...

  12. #12
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    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...

  13. #13
    Registered User Caze's Avatar
    Join Date
    Nov 2002
    Posts
    18
    I think you should use a while loop instead of three do while loops.
    Code:
    while (l!=123 || health>0 || foe_health>0)
    {
    //The fight code
    }
    Much easier to understand

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simultaneously waiting for data on FIFO and UDP using select call
    By yogesh3073 in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-05-2007, 08:53 AM
  2. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  3. Errors
    By Rhidian in forum C Programming
    Replies: 10
    Last Post: 04-04-2005, 12:22 PM
  4. Binary Tree, couple questions
    By scoobasean in forum C Programming
    Replies: 3
    Last Post: 03-12-2005, 08:09 PM
  5. Replies: 1
    Last Post: 07-31-2002, 11:35 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21