0% found this document useful (0 votes)
7 views

Zombiegame Code

by mokshit kachhwaha
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Zombiegame Code

by mokshit kachhwaha
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <iostream>

#include <ctime>
#include <cstdlib>

using namespace std;

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;
}

void attack(Player& player) {


player.health -= 10;
cout << "A zombie bites you! Your health is now: " << player.health <<
endl;
}

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;

You might also like