Zombiegame Code
Zombiegame Code
#include <ctime>
#include <cstdlib>
class Player {
public:
int health;
int ammo;
Player() {
health = 100;
ammo = 10;
}
void shoot() {
if (ammo > 0) {
ammo--;
cout << "You shoot a zombie! Ammo left: " << ammo << endl;
} else {
cout << "You're out of ammo! Run away!" << endl;
}
}
void run() {
cout << "You run away from the zombies!" << endl;
health -= 10;
cout << "Your health is now: " << health << endl;
}
bool isAlive() {
return health > 0;
}
};
class Zombie {
public:
int health;
Zombie() {
health = 20;
}
bool isAlive() {
return health > 0;
}
};
int main() {
srand(time(0));
Player player;
Zombie zombie;
while (player.isAlive() && zombie.isAlive()) {
cout << "Your health: " << player.health << endl;
cout << "Zombie health: " << zombie.health << endl;
cout << "Ammo left: " << player.ammo << endl;
cout << "What do you do? (1) Shoot, (2) Run" << endl;
int choice;
cin >> choice;
if (choice == 1) {
player.shoot();
if (rand() % 2 == 0) {
zombie.health -= 10;
cout << "You hit the zombie! Zombie health is now: " <<
zombie.health << endl;
} else {
cout << "You missed the zombie!" << endl;
}
} else if (choice == 2) {
player.run();
}
if (zombie.isAlive()) {
zombie.attack(player);
}
}
if (player.isAlive()) {
cout << "You survived! Congratulations!" << endl;
} else {
cout << "You died! Game over!" << endl;
}
return 0;