#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
// 植物类
class Plant {
private:
int health;
int attack;
public:
Plant(int health, int attack) {
this->health = health;
this->attack = attack;
}
int getHealth() {
return health;
}
int getAttack() {
return attack;
}
void setHealth(int health) {
this->health = health;
}
void attackZombie() {
cout << "植物攻击僵尸,造成" << attack << "点伤害。" << endl;
}
};
// 僵尸类
class Zombie {
private:
int health;
int attack;
public:
Zombie(int health, int attack) {
this->health = health;
this->attack = attack;
}
int getHealth() {
return health;
}
int getAttack() {
return attack;
}
void setHealth(int health) {
this->health = health;
}
void attackPlant() {
cout << "僵尸攻击植物,造成" << attack << "点伤害。" << endl;
}
};
int main() {
srand(time(NULL));
Plant plant(100, 10);
Zombie zombie(100, 5);
while (plant.getHealth() > 0 && zombie.getHealth() > 0) {
int plantAttack = rand() % plant.getAttack();
int zombieAttack = rand() % zombie.getAttack();
plant.attackZombie();
zombie.setHealth(zombie.getHealth() - plantAttack);
cout << "僵尸剩余生命值:" << zombie.getHealth() << endl;
if (zombie.getHealth() <= 0) {
cout << "僵尸被击败,植物获胜!" << endl;
break;
}
zombie.attackPlant();
plant.setHealth(plant.getHealth() - zombieAttack);
cout << "植物剩余生命值:" << plant.getHealth() << endl;
if (plant.getHealth() <= 0) {
cout << "植物被击败,僵尸获胜!" << endl;
break;
}
}
return 0;
}
用C++个植物大战僵尸游戏
最新推荐文章于 2025-04-18 19:11:53 发布