Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

My first C++ Object game

This is my first little game in C++ using objects.

If you want to download the working exe, click here.

If you want to compile it yourself, the code is here.
Code:
/*
 Simple Bet-up-the-enemy Game
 Yeyinde
 12/11/06
*/
#include <iostream>
#include <stdlib.h>
#include <ctime>
using namespace std;
/*
 Battler Class
 Superclass for Player and Enemy
*/
class Battler
{
public:
    int hp, maxhp, strength, defence;
    bool defending;
    Battler()
    {
        defending = false;
    }
    void attack_effect(Battler attacker)
    {
        int damage = (rand() % attacker.strength * 2) + 1;
        damage /= (defence / 2);
        if (defending)
            damage /= 2;
        hp -= damage;
        cout << damage << " damage taken!\n";
        if (hp < 0)
            hp = 0;
        cout << "HP: " << hp << "/" << maxhp << endl;
    }
};
/*
 Player Class
*/
class Player : public Battler
{
public:
    int level, exp, next_exp;
    Player()
    {
        level = 1;
        hp = (rand() % 11) + 30;
        maxhp = hp;
        exp = 0;
        next_exp = 15;
        strength = (rand() % 6) + 5;
        defence = (rand() % 3) + 5;
    }
    void print_status()
    {
        cout << "Your status:\n";
        cout << "LV: " << level << endl;
        cout << "HP: " << hp << "/" << maxhp << endl;
        cout << "EXP: " << exp << "/" << next_exp << endl;
        cout << "NEXT: " << next_exp - exp << endl;
        cout << "STR: " << strength << endl;
        cout << "DEF: " << defence << endl;
    }
    void gain_exp(int n_exp)
    {
        char input;
        exp += n_exp;
        cout << "Gained " << n_exp << "EXP!\n";
        system("PAUSE");
        while (exp >= next_exp)
        {
            cout << endl;
            cout << "LEVEL UP!\n";
            cout << "What stat will you increase?\n";
            cout << " 1: MAXHP (+" << 5 * (level + 1) / 2 << ")\n";
            cout << " 2: STR (+" << 1 * (level + 1) / 2 << ")\n";
            cout << " 3: DEF (+" << 1 * (level + 1) / 2 << ")\n";
            while (true)
            {
                cout << "Selection: ";
                cin >> input;
                if (input == '1' || input == '2' || input == '3')
                    break;
                else
                    cout << "Invalid selection.\n";
            }
            cout << endl;
            if (input == '1')
            {
                maxhp += 5 * (level + 1) / 2;
                cout << "MAXHP: " << maxhp << endl;
            }
            else if (input == '2')
            {
                strength += 1 * (level + 1) / 2;
                cout << "STR: " << strength << endl;
            }
            else
            {
                defence += 1 * (level) / 2;
                cout << "DEF: " << defence << endl;
            }
            level += 1;
            hp = maxhp;
            exp -= next_exp;
            next_exp += next_exp / 2;
            system("PAUSE");
        }
        cout << endl;
    }
} player;
/*
 Enemy Class
*/
class Enemy : public Battler
{
public:
    int exp;
    Enemy(int level)
    {
        hp = (rand() % 6) + 5 * level;
        maxhp = hp;
        exp = hp / 2;
        strength = (rand() % 6) + 5 * level;
        defence = (rand() % 3) + 3 * level;
    }
    void print_status()
    {
        cout << "Enemy status:\n";
        cout << "HP: " << hp << "/" << maxhp << endl;
    }
};
char player_action()
{
    char input;
    cout << "What will you do?\n";
    cout << " 1: Attack\n 2: Defend\n 3: View Status\n";
    while (true)
    {
        cout << "Selection: ";
        cin >> input;
        if (input == '1' || input == '2' || input == '3')
            break;
        else
            cout << "Invalid selection.";
    }
    return input;
}
/*
 Main Processing
*/
int main()
{
    char input;
    int e_action;
    srand(time(0));
    cout << "BATTLE GAME!!\n";
    cout << "Defeat all the enemies without being defeated!\n\n";
    system("PAUSE");
    while (player.hp > 0)
    {
        cout << "Enemy appears!\n\n";
        Enemy enemy(player.level);
        // Combat Loop
        while (enemy.hp > 0)
        {
            // Player Selection Loop
            player.defending = false;
            while (true)
            {
                cout << endl;
                input = player_action();
                cout << endl;
                if (input == '1')
                {
                    cout << "You attack!\n";
                    enemy.attack_effect(player);
                    break;
                }
                else if (input == '2')
                {
                    cout << "You defends!\n";
                    player.defending = true;
                    break;
                }
                else
                    player.print_status();
                cout << endl;
            }
            system("PAUSE");
            // Enemy Action
            enemy.defending = false;
            cout << endl;
            if (enemy.hp > 0)
            {
                e_action = rand() % 5;
                if (e_action == 3)
                {
                    cout << "Enemy defends!\n";
                    enemy.defending = true;
                }
                else
                {
                    cout << "Enemy attacks!\n";
                    player.attack_effect(enemy);
                    if (player.hp == 0)
                    {
                        cout << "Game over...\n";
                        system("PAUSE");
                        return 0;
                    }
                }
                system("PAUSE");
            }
        }
        cout << "Enemy has fallen!\n";
        player.gain_exp(enemy.exp);
        system("CLS");
    }
    return 0;
}

Please note that this uses Windows libraries. If you intend on compiling on a different OS, do make the changes in the script where necessary.
 

Mega Flare

Awesome Bro

thats some pretty good code there. I got one Complant. I dont like how you got your code writen down. I think it looks ugly. But maybe thats how you learned. But other then that its a good game. Simple but good
 
Even though I'm not a C++ programmer (I like C) I do understand the code and it looks pretty good. There are some things I would do differently though...mainly on preference of style.
Since I tend to write code ment to be run on Linux, BSD, and Windows I got in the habbit of:
Making great use of Macros to divide up platform specific parts of the code.
Specifying the sign of a variable (signed unsigned).
Adding debuging code that is only compiled in if a flag for it is raised during compiling
and lots of other stuff...
For a program this small I wouldn't worry about any of that but keep it in mind. Also if you ever use Macros extensively watch your self as some ungodly bugs can occure from their incorrect use...god I have some horror stories...
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top