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.
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.
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.