用C++写拳皇代码
时间: 2024-01-26 08:01:47 浏览: 218
c++玫瑰花代码.zip
以下是一个简单的拳皇游戏C++代码示例:
```cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
int player_health = 100;
int enemy_health = 100;
while (player_health > 0 && enemy_health > 0)
{
cout << "请选择你的招式:" << endl;
cout << "1. 普通攻击" << endl;
cout << "2. 重击" << endl;
cout << "3. 快速连招" << endl;
int choice;
cin >> choice;
if (choice == 1)
{
int damage = rand() % 6 + 10;
enemy_health -= damage;
cout << "你发动了普通攻击,造成了" << damage << "点伤害" << endl;
}
else if (choice == 2)
{
int damage = rand() % 6 + 20;
enemy_health -= damage;
player_health -= 10;
cout << "你发动了重击,造成了" << damage << "点伤害,但自己也承受了10点伤害" << endl;
}
else if (choice == 3)
{
int damage1 = rand() % 6 + 5;
int damage2 = rand() % 6 + 5;
int damage3 = rand() % 6 + 5;
int total_damage = damage1 + damage2 + damage3;
enemy_health -= total_damage;
cout << "你发动了快速连招,造成了" << total_damage << "点伤害" << endl;
}
else
{
cout << "输入错误,请重新选择!" << endl;
continue;
}
if (enemy_health <= 0)
{
cout << "你赢得了胜利!" << endl;
break;
}
int enemy_choice = rand() % 3 + 1;
if (enemy_choice == 1)
{
int damage = rand() % 6 + 10;
player_health -= damage;
cout << "敌人发动了普通攻击,你受到了" << damage << "点伤害" << endl;
}
else if (enemy_choice == 2)
{
int damage = rand() % 6 + 20;
player_health -= damage;
enemy_health += 10;
cout << "敌人发动了重击,你受到了" << damage << "点伤害,但敌人也承受了10点伤害" << endl;
}
else
{
int damage1 = rand() % 6 + 5;
int damage2 = rand() % 6 + 5;
int damage3 = rand() % 6 + 5;
int total_damage = damage1 + damage2 + damage3;
player_health -= total_damage;
cout << "敌人发动了快速连招,你受到了" << total_damage << "点伤害" << endl;
}
if (player_health <= 0)
{
cout << "你输了!" << endl;
break;
}
}
return 0;
}
```
这个代码实现了一个简单的拳皇游戏,玩家和敌人轮流选择招式进行攻击,直到一方的血量降到0为止。玩家可以选择普通攻击、重击和快速连招三种招式,敌人则随机选择招式进行攻击。游戏结束后会输出胜负结果。注意:为了使用rand()函数,需要包含cstdlib头文件并在程序开始前调用srand(time(NULL))函数以初始化随机数生成器。
阅读全文