-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
109 lines (94 loc) · 3.28 KB
/
main.cpp
File metadata and controls
109 lines (94 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <iostream>
#include <string>
#include <chrono>
#include <thread>
class Player {
// The player object, where most of the player variables are stored
public:
std::string name;
std::string weaponname;
int health;
int attack;
void Attack() {
// Attack function - called whenever the player attacks
std::cout << name << " attacks! Dealt " << attack << " points of damage!" << std::endl;
}
void Damage(int damageval) {
// Damage function - called whenever the player takes damage
std::cout << name << " received " << damageval << " points of damage! " << name << " now has " << health << " points of health!\n" << std::endl;
health = health - damageval;
}
};
class Enemy {
// The enemy object, pretty much a copy of the player object
public:
std::string name;
std::string weaponname;
int health;
int attack;
void Attack() {
// Attack function - called whenever the player attacks
std::cout << name << " attacks! Dealt " << attack << " points of damage!" << std::endl;
}
void Damage(int damageval) {
// Damage function - called whenever the player takes damage
std::cout << name << " received " << damageval << " points of damage! " << name << " now has " << health << " points of health!\n" << std::endl;
health = health - damageval;
}
};
int main() {
// This code is executed at runtime
Player Player1;
Enemy Enemy1;
Player1.health = 0;
Player1.attack = 0;
Enemy1.health = 0;
Enemy1.attack = 0;
while (Player1.name == "")
{
std::cout << "Please input your name!\n> ";
std::cin >> Player1.name;
}
while (Enemy1.name == "")
{
std::cout << "Please input your enemy's name!\n> ";
std::cin >> Enemy1.name;
}
while (Player1.health < 1)
{
std::cout << "Please input your desired health!\n> ";
std::cin >> Player1.health;
}
while (Enemy1.health < 1)
{
std::cout << "Please input your enemy's health!\n> ";
std::cin >> Enemy1.health;
}
while (Player1.attack < 1)
{
std::cout << "Please input your desired attack value!\n> ";
std::cin >> Player1.attack;
}
while (Enemy1.attack < 1)
{
std::cout << "Please input your enemy's attack value!\n> ";
std::cin >> Enemy1.attack;
}
std::cout << Player1.name << " challenges " << Enemy1.name << " to a fight!\n" << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(1500)); // This makes the program wait for 1.5 seconds
while (Enemy1.health > 0 & Player1.health > 0) {
// This loop ends whenever either the player or enemy runs out of health.
Player1.Attack();
Enemy1.Damage(Player1.attack);
std::this_thread::sleep_for(std::chrono::milliseconds(500));
Enemy1.Attack();
Player1.Damage(Enemy1.attack);
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
if (Enemy1.health < 1) {
std::cout << Enemy1.name << " has been defeated! " << Player1.name << " wins!" << std::endl;
}
if (Player1.health < 1) {
std::cout << Player1.name << " has been defeated! " << Enemy1.name << " wins!" << std::endl;
}
}