直接上代码
#include
#include
struct Character {
std::string name;
int health;
int attack;
};
int main() {
Character player;
player.name = "旅行者";
player.health = 100;
player.attack = 10;
Character enemy;
enemy.name = "原神";
enemy.health = 100;
enemy.attack = 10;
std::cout << "欢迎来到原神游戏!\n";
std::cout << "你是" << player.name << ",拥有" << player.health << "点生命值和" << player.attack << "点攻击力。\n";
std::cout << "你的对手是" << enemy.name << ",拥有" << enemy.health << "点生命值和" << enemy.attack << "点攻击力。\n\n";
// 游戏循环
while (player.health > 0 && enemy.health > 0) {
// 玩家攻击敌人
std::cout << player.name << "攻击" << enemy.name << ",造成" << player.attack << "点伤害。\n";
enemy.health -= player.attack;
if (enemy.health <= 0) {
std::cout << enemy.name << "被击败了!恭喜你获得胜利!\n";
break;
}
// 敌人攻击玩家
std::cout << enemy.name << "反击" << player.name << ",造成" << enemy.attack << "点伤害。\n";
player.health -= enemy.attack;
if (player.health <= 0) {
std::cout << player.name << "被击败了!很遗憾你失败了。\n";
}
std::cout << "当前状态:\n";
std::cout << "你的生命值:" << player.health << "\n";
std::cout << enemy.name << "的生命值:" << enemy.health << "\n\n";
}
std::cout << "游戏结束。\n";
return 0;
}
这个游戏中,你将扮演旅行者与原神进行对战。两方拥有生命值和攻击力,互相攻击对方直至有一方生命值归零。游戏循环不断进行,直到一方胜利或双方均失败。游戏会输出当前双方的生命值状态,并在游戏结束时显示胜利或失败信息。这只是一个微小的模拟,希望你喜欢!