life<=0

This is a discussion on life<=0 within the C++ Programming forums, part of the General Programming Boards category; Alright, I can't seem to figure out a couple of things here. I'm making an RPG and am having a ...

  1. #1
    Registered User webturtle0's Avatar
    Join Date
    Nov 2002
    Posts
    76

    Exclamation life<=0

    Alright, I can't seem to figure out a couple of things here. I'm making an RPG and am having a couple probs with the fighting. First, I need help with the life. I want it so when my life or mlife is <=0, they die. Just a simple message to say "You killed the enemy" or whatever and brings it back to the main forest menu. I can't figure out what to use for that and where to put it.
    Also, when I want to run from the enemy, I want it to go back to the main forest menu and be able to go from there.
    I think that's about it so far. I may need help later on with file saving. I looked through past threads and so far haven't found much. If anyone can help, I would greatly appreciate it. Thanks!
    Code:
    switch (town1)
    {
    case 2:
         cout<<"Legend of the Green Dragon - Forest"<<'\n';
         cout<<"-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-"<<'\n';
         cout<<" "<<'\n';
         cout<<name<<", you slowly walk into the forest. As you walk through, you're careful to"<<'\n';
         cout<<"notice anything that may seem out of the ordinary. Although, around here,"<<'\n';
         cout<<"strange is a normal thing."<<'\n';
         cout<<" "<<'\n';
         cout<<"What would you like to do?"<<'\n';
         cout<<"(1)Kill something"<<'\n';
         cout <<"(2)Look for the Green Dragon"<<'\n';
         cout <<"(3)Leave the forest"<<'\n';
         cin>>forest;
         cin.ignore(80, '\n');
         system("CLS");
    	
         switch (forest)
         {
         case 1:
    
               srand((unsigned)time(NULL));
               monster = (rand() %1) + 1;
    
               switch (monster)
               {
               case 1:
                     strcpy(monster1, "an Old Man");
                     mlife=20*level;
                     monmaxattack=monmaxattack*level;
                     monminattack=monminattack+level;
                     break;
               }
               cout<<"Legend of the Green Dragon - Forest"<<'\n';
               cout<<"-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-"<<'\n';
               cout<<" "<<'\n';
               cout<<"You've encountered "<< monster1 <<"."<<'\n';
               cout<<"Your hit points: "<<life<<'\n';
               cout<<"Enemy hit points: "<<mlife<<'\n';
               cout<<"(1)Attack"<<'\n';
               cout<<"(2)Run"<<'\n';
               cin>>fightchoice;
               system("CLS");
               do
               {
                    switch(fightchoice)
               {
               case 1:
                     srand((unsigned)time(NULL));
                     damage = (rand() %maxattack) +minattack;
                     cout<<"Legend of the Green Dragon - Forest"<<'\n';
                     cout<<"-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-"<<'\n';
                     cout<<" "<<'\n';
                     cout<<"You dealt "<<damage<<" damage"<<'\n';
                     mlife=mlife-damage;
                     mondamage = (rand() %monmaxattack) +monminattack;
                     cout<<"The enemy dealt "<<mondamage<<" damage"<<'\n';
                     life=life-mondamage;
                        
                     cout<<" "<<'\n';
                     cout<<"Enemy: "<<monster1<<'\n';
                     cout<<" "<<'\n';
                     cout<<"Your hit points: "<<life<<'\n';
                     cout<<"Enemy hit points: "<<mlife<<'\n';
                     cout<<"(1)Attack"<<'\n';
                     cout<<"(2)Run"<<'\n';
                     cin>>fightchoice;
                     system("CLS");
                     break;
    					
    
                     gained = (rand() %(experience*level)) + 1;
                     experience=experience+gained;
                     cout<<"You gained "<<experience<<" experience"<<'\n';
    					
    
                     if(experience=(300*level))
                     {
                           level=level+1;
                     }
                     case 2:
                           cout<<"You run from the enemy"<<'\n';
                           break;
                     }
                }while(fightchoice!=4);
          }
    }
    return 0;
    }
    Attached Files Attached Files
    Last edited by webturtle0; 04-20-2003 at 02:05 AM.
    "Yo"

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Have you actually tried:
    Code:
    if(Life <= 0)
    {
       cout << "You died!" << endl;
       YouAreDead = true; //Some way to signal that you're not allowed to play anymore
    }
    ?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User webturtle0's Avatar
    Join Date
    Nov 2002
    Posts
    76
    I did, but I wasn't exactly sure where to put it. I tried it in a few places, but I couldn't get it to work.
    "Yo"

  4. #4
    Registered User Ion Blade's Avatar
    Join Date
    May 2002
    Posts
    35
    a good place to put it would be right after a hit/attack occurs, and it says "the monster dealt x damage". if the player dies, do your "you are dead" message and break out of the loop, otherwise keep going as normal.

  5. #5
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    I try to point this out when i see it. You have made calculations the way that many other people do int heir first game. Example:

    Code:
    monmaxattack=monmaxattack*level;
    mlife=mlife-damage;
    life=life-mondamage
    Each of these lines can be optimized by shortening them, like so:

    Code:
    monmaxattack *= level;
    mlife -= damage;
    life -= mondamage;
    This does the same thing but you dont have to state the variable twice.

  6. #6
    Registered User zerodevide's Avatar
    Join Date
    Apr 2003
    Posts
    6

    Re: life<=0

    There's a number of issues in your code, but this little windo is a pain for this type of thing, but.....

    In your fightchoice switch, you do a break before the case is done. Experience is never computed.

    Living or dead: Place your test right after life=life-mondamage;
    If they're dead, break;

    Your do..while for fightchoice doesn't stop unless the choice is 4, though you menu only has two (2) choices...loop will never end.

    To get out under your plan, case 2 (fightchoice) should change fightchoice to a 4, which will dump the loop.

    Lastly, you should probably make most of the repeating stuff as functions. Saves time, and would probably make it a bit easier to read.

Popular pages Recent additions subscribe to a feed

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