c++丧尸危机游戏代码
时间: 2024-04-21 21:20:14 浏览: 39
C++丧尸危机游戏代码是一个基于C++语言编写的游戏程序,它模拟了一个丧尸危机的场景,玩家需要在游戏中与丧尸进行战斗并生存下去。以下是一个简单的C++丧尸危机游戏代码的示例:
```cpp
#include <iostream>
#include <string>
using namespace std;
class Player {
private:
string name;
int health;
int damage;
public:
Player(string playerName, int playerHealth, int playerDamage) {
name = playerName;
health = playerHealth;
damage = playerDamage;
}
void attack() {
cout << name << " attacks and deals " << damage << " damage!" << endl;
}
void takeDamage(int amount) {
health -= amount;
cout << name << " takes " << amount << " damage!" << endl;
if (health <= 0) {
cout << name << " has been defeated!" << endl;
}
}
};
class Zombie {
private:
int health;
int damage;
public:
Zombie(int zombieHealth, int zombieDamage) {
health = zombieHealth;
damage = zombieDamage;
}
void attack() {
cout << "Zombie attacks and deals " << damage << " damage!" << endl;
}
void takeDamage(int amount) {
health -= amount;
cout << "Zombie takes " << amount << " damage!" << endl;
if (health <= 0) {
cout << "Zombie has been defeated!" << endl;
}
}
};
int main() {
Player player("Player1", 100, 20);
Zombie zombie(50, 10);
while (player.health > 0 && zombie.health > 0) {
player.attack();
zombie.takeDamage(player.damage);
if (zombie.health > 0) {
zombie.attack();
player.takeDamage(zombie.damage);
}
}
if (player.health > 0) {
cout << "Player wins!" << endl;
} else {
cout << "Zombie wins!" << endl;
}
return 0;
}
```
这段代码创建了一个Player类和一个Zombie类,分别表示玩家和丧尸。在游戏中,玩家和丧尸可以互相攻击,并根据造成的伤害来更新各自的生命值。游戏通过循环判断玩家和丧尸的生命值是否大于0来确定游戏的胜负。
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)