Jump to content
  • Advertisement
Sign in to follow this  
kyle10590

Need help with C++ text game exp and level up system

This topic is 3442 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, I've been making my C++ text game. Gone fairly well so far. Had a few delays with some mistakes I have made. Got most of that fixed up. Now I am working on the level up and experience points system. I can't make it level up past level two. I had it kinda working before, but the exp doubled each time I defeat an enemy, and releveled all the previous levels each time it got higher. Here's the code:
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <windows.h>

using namespace std;

class Player; // forward declaration

class Enemy
{
private:
    int lvl;

public:
    int health;
    Enemy()
    {
        health = 100;
        lvl = 1;
    }

    void attack(Player & p);

    friend class Player;
};

class Player
{
private:

public:

    int health;
    int exp;
    int lvl;
    Player(bool hero)
    {
         if (hero)
         {
             health = 300;
             lvl = 1;
             exp = 0;
         }

         else
         {
             health = 1;
         }
    }

    void attack(Enemy & e);
    void lvlUp(Player & p);

    friend class Enemy;
};

void Player::attack(Enemy & e)
{
    int randomNumber = rand();

    int dmg = (randomNumber % 30) + 0; // Between 0-30

    cout << "\nYou've done " << dmg << " damage!" << endl;

    e.health -= dmg;

}

void Enemy::attack(Player & p)
{
    int randomNumber = rand();

    int dmg = (randomNumber % 20) + 0;

    cout << "\nThe Enemy does " << dmg << " damage to you!\n" << endl;

    p.health -= dmg;
}

void Player::lvlUp(Player & p)
{

}



int main()
{
    srand(time(0)); // only seed once

    char playerInput;
    char action;
    char move;

    cout << "\t\tWelcome to The Warrior's Adventure!\n\n" << endl;

    Player p(true); // change to true to make a hero

    do
    {
        int randomNumber = 0;
        int direc = 0;
        cout << "You start walking... \n" << endl;
        cin.ignore(1);
        system("CLS");
        do
        {
            cin.ignore(1);

            randomNumber = rand();
            direc = randomNumber % 5 + 3;
            cout << "You keep walking... \n" << endl;
            cin.ignore(1);
            system("CLS");
        }
        while(direc != 4);

        string enemFound = "\nYou've encountered a Enemy! Press \"a\" to attack, or \"r\" to run.";

        cout << enemFound << endl;

        Enemy e;

        cin >> action;
        system("CLS");


        if(action == 'r' || action == 'R')
        {
            cout << "You run away!\n" << endl;
        }

    if(action == 'a' || action == 'A')
    {

        if(p.health <= 0)
        {
              system("CLS");
              cout << "\t\n\nYou have died..." << endl;
              cout << "\t\nGAME OVER!" << endl << endl;

              return 0;
        }

        do{
            p.attack(e);
            cin.ignore(1);

            if(e.health >= 1)
            {
                e.attack(p);
                cin.ignore(1);
            }

          } while(e.health >= 0);

          if(e.health <= 0)
          {
              cout << "\n\nYou defeat the Enemy! *Vistory Music*\n" << endl;

              cout << "You gained " << 100 << " experience from the Boar."
              << endl;

              p.exp += 100;
          }

           if(p.exp >= 200)
          {
              cout << "\nYou've gone up to level 2!" << endl;
              p.lvl++;
              p.health + 50;
          }
          else if(p.exp >= 400)
          {
              cout << "\nYou've gone up to level 3!" << endl;
              p.lvl++;
              p.health + 40;
          }
          else if(p.exp >= 600)
          {
              cout << "\nYou've gone up to level 4!" << endl;
              p.lvl++;
              p.health + 50;
          }
          else if(p.exp >= 750)
          {
              cout << "\nYou've gone up to level 5!" << endl;
              p.lvl++;
              p.health + 50;
          }
    }

         cin >> action;
    } while(action != '!' || playerInput != '!');
}

Share this post


Link to post
Share on other sites
Advertisement

if(p.exp >= 200)
{
cout << "\nYou've gone up to level 2!" << endl;
p.lvl++;
p.health + 50; <----SHOULD THIS BE p.health += 50?
}
else if(p.exp >= 400)
{
cout << "\nYou've gone up to level 3!" << endl;
p.lvl++;
p.health + 40; <---SAME AS ABOVE?
}
else if(p.exp >= 600)
{
cout << "\nYou've gone up to level 4!" << endl;
p.lvl++;
p.health + 50;<- AND HERE TOO.
}
else if(p.exp >= 750)
{
cout << "\nYou've gone up to level 5!" << endl;
p.lvl++;
p.health + 50;
}

not sure what your are trying to do with p.health+50. You probably mean += 50



Your original problem is in your level condition.It is wrong. Every time your player gains experience and its total experience is over 200, the
first statement is executed.

if(p.exp >= 200) problem is in your condition statements. Notice that this gets executed everytime after exp is >=200. The other does not get called.

{
cout << "\nYou've gone up to level 2!" << endl;
p.lvl++;
p.health + 50;
}
else if(p.exp >= 400)
{
cout << "\nYou've gone up to level 3!" << endl;
p.lvl++;
p.health + 40;
}
else if(p.exp >= 600)
{
cout << "\nYou've gone up to level 4!" << endl;
p.lvl++;
p.health + 50;
}
else if(p.exp >= 750)
{
cout << "\nYou've gone up to level 5!" << endl;
p.lvl++;
p.health + 50;
}



To solve your problem do this :

if(exp >=200 && exp <300) //your level is 2
if(exp >=300 && exp <400) //your level is 3
if(exp >=400 && exp <500) //your level is 4
and so on.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!