刚刚
2026-11-19 16:15:41
发布于:浙江
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <windows.h>
using namespace std;
// 角色类
class Fighter {
private:
string name;
int hp; // 生命值
int maxHp; // 最大生命值
int attack; // 攻击力
int defense; // 防御力
int skillCount; // 技能次数
public:
Fighter(string n, int h, int a, int d) {
name = n;
maxHp = h;
hp = h;
attack = a;
defense = d;
skillCount = 3;
}
string getName() { return name; }
int getHp() { return hp; }
int getMaxHp() { return maxHp; }
// 普通攻击
int normalAttack() {
int damage = attack + (rand() % 20 - 10); // 攻击力上下浮动10
return damage > 0 ? damage : 1;
}
// 技能攻击(伤害更高)
int skillAttack() {
if (skillCount > 0) {
skillCount--;
int damage = attack * 2 + (rand() % 30 - 15);
return damage > 0 ? damage : 1;
}
return -1; // 技能次数用完
}
// 受到伤害
void takeDamage(int damage) {
int realDamage = damage - defense / 2;
if (realDamage < 1) realDamage = 1;
hp -= realDamage;
if (hp < 0) hp = 0;
}
// 恢复少量生命
void heal() {
int healAmount = maxHp / 5;
hp += healAmount;
if (hp > maxHp) hp = maxHp;
cout << name << "使用了恢复,恢复了 " << healAmount << " 点生命值!" << endl;
}
bool isAlive() { return hp > 0; }
int getSkillCount() { return skillCount; }
void showStatus() {
cout << "----------------------------------------" << endl;
cout << name << " 状态:" << endl;
cout << "HP: " << hp << "/" << maxHp << endl;
cout << "攻击力: " << attack << " 防御力: " << defense << endl;
cout << "剩余技能次数: " << skillCount << endl;
cout << "----------------------------------------" << endl;
}
};
// 显示战斗标题
void showTitle() {
system("cls");
cout << "" << endl;
cout << " 贺宇翔 VS 林泽坚 巅峰对决" << endl;
cout << "" << endl;
cout << endl;
}
// 显示战斗菜单
void showMenu() {
cout << "请选择行动:" << endl;
cout << "1. 普通攻击" << endl;
cout << "2. 技能攻击(伤害高,剩余次数有限)" << endl;
cout << "3. 恢复生命" << endl;
cout << "0. 逃跑认输" << endl;
cout << "你的选择: ";
}
int main() {
srand((unsigned)time(NULL));
// 创建两个角色
Fighter he("贺宇翔", 200, 35, 15);
Fighter lin("林泽坚", 180, 40, 10);
int round = 1;
bool gameOver = false;
showTitle();
cout << "战斗开始!" << endl;
cout << "贺宇翔:林泽坚,今天我要打败你!" << endl;
cout << "林泽坚:哼,放马过来吧!" << endl;
cout << endl;
system("pause");
// 战斗主循环
while (!gameOver) {
showTitle();
cout << "========== 第 " << round << " 回合 ==========" << endl << endl;
// 显示双方状态
he.showStatus();
cout << endl;
lin.showStatus();
cout << endl;
// 玩家操作(贺宇翔)
int choice;
bool validChoice = false;
while (!validChoice) {
showMenu();
cin >> choice;
switch (choice) {
case 1: {
int damage = he.normalAttack();
lin.takeDamage(damage);
cout << endl << "贺宇翔发动普通攻击,对林泽坚造成 " << damage << " 点伤害!" << endl;
validChoice = true;
break;
}
case 2: {
if (he.getSkillCount() > 0) {
int damage = he.skillAttack();
lin.takeDamage(damage);
cout << endl << "贺宇翔发动技能攻击!对林泽坚造成 " << damage << " 点伤害!" << endl;
} else {
cout << "技能次数已用完!请选择其他操作。" << endl;
}
validChoice = true;
break;
}
case 3: {
he.heal();
validChoice = true;
break;
}
case 0: {
cout << endl << "贺宇翔选择了逃跑认输!" << endl;
cout << "林泽坚获胜!" << endl;
gameOver = true;
validChoice = true;
break;
}
default: {
cout << "无效选择,请重新输入!" << endl;
break;
}
}
}
if (gameOver) break;
// 检查林泽坚是否被打败
if (!lin.isAlive()) {
cout << endl << "林泽坚被打败了!" << endl;
cout << "贺宇翔获胜!" << endl;
gameOver = true;
break;
}
cout << endl;
system("pause");
showTitle();
cout << "========== 第 " << round << " 回合(林泽坚行动) ==========" << endl << endl;
// 林泽坚AI行动
int aiChoice = rand() % 10;
if (aiChoice < 5) {
// 普通攻击
int damage = lin.normalAttack();
he.takeDamage(damage);
cout << "林泽坚发动普通攻击,对贺宇翔造成 " << damage << " 点伤害!" << endl;
} else if (aiChoice < 8) {
// 技能攻击
if (lin.getSkillCount() > 0) {
int damage = lin.skillAttack();
he.takeDamage(damage);
cout << "林泽坚发动技能攻击!对贺宇翔造成 " << damage << " 点伤害!" << endl;
} else {
int damage = lin.normalAttack();
he.takeDamage(damage);
cout << "林泽坚技能次数用完,改用普通攻击,造成 " << damage << " 点伤害!" << endl;
}
} else {
// 恢复
lin.heal();
}
// 检查贺宇翔是否被打败
if (!he.isAlive()) {
cout << endl << "贺宇翔被打败了!" << endl;
cout << "林泽坚获胜!" << endl;
gameOver = true;
break;
}
cout << endl;
system("pause");
round++;
}
cout << endl;
cout << "========================================" << endl;
cout << " 战斗结束!" << endl;
cout << "========================================" << endl;
system("pause");
return 0;
}
全部评论 1
大棒大巴发
4天前 来自 浙江
0
























有帮助,赞一个