人机小游戏
2026-01-17 11:25:12
发布于:浙江
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <limits>
#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif
using namespace std;
// 技能类
class Skill {
private:
string name;
int damageMultiplier; // 伤害倍数
int mpCost; // 魔法消耗
string description;
public:
Skill(string n, int mult, int mp, string desc)
: name(n), damageMultiplier(mult), mpCost(mp), description(desc) {}
string getName() const { return name; }
int getDamageMultiplier() const { return damageMultiplier; }
int getMpCost() const { return mpCost; }
string getDescription() const { return description; }
};
class Player {
private:
string name;
int health;
int maxHealth;
int attackPower;
int defensePower;
int mp; // 魔法值
int maxMp;
int level;
int exp;
int coins;
vector<Skill> skills;
int skillIndex; // 当前选中的技能
bool isDefending; // 是否处于防御状态
public:
Player(string n) : name(n), health(100), maxHealth(100),
attackPower(15 + rand() % 10), defensePower(5 + rand() % 5),
mp(50), maxMp(50), level(1), exp(0), coins(0),
skillIndex(0), isDefending(false) {
// 初始化技能
skills.push_back(Skill("普通攻击", 1, 0, "基本的物理攻击"));
skills.push_back(Skill("重击", 2, 10, "强力一击,造成2倍伤害"));
skills.push_back(Skill("治疗术", 0, 15, "恢复20点生命值"));
skills.push_back(Skill("防御姿态", 0, 5, "下回合防御力翻倍"));
cout << "========================================\n";
cout << name << " 加入了战斗!\n";
cout << "初始属性: HP=" << health << ", MP=" << mp
<< ", 攻击=" << attackPower << ", 防御=" << defensePower << endl;
cout << "========================================\n\n";
}
string getName() const { return name; }
int getHealth() const { return health; }
int getMaxHealth() const { return maxHealth; }
int getMp() const { return mp; }
int getMaxMp() const { return maxMp; }
int getLevel() const { return level; }
int getExp() const { return exp; }
int getCoins() const { return coins; }
const vector<Skill>& getSkills() const { return skills; } // 添加这个getter
string getSkillName(int index) const { // 添加这个方法获取技能名
if (index >= 0 && index < skills.size()) {
return skills[index].getName();
}
return "普通攻击";
}
void attack(Player& opponent, int skillIndex = 0) {
if (skillIndex < 0 || skillIndex >= skills.size()) {
skillIndex = 0;
}
Skill& selectedSkill = skills[skillIndex];
if (selectedSkill.getMpCost() > mp && skillIndex != 0) {
cout << "魔法值不足!使用普通攻击代替。\n";
skillIndex = 0;
selectedSkill = skills[0];
}
if (selectedSkill.getName() == "治疗术") {
int healAmount = 20;
health += healAmount;
if (health > maxHealth) health = maxHealth;
mp -= selectedSkill.getMpCost();
cout << name << " 使用了 " << selectedSkill.getName() << "!\n";
cout << "恢复了 " << healAmount << " 点生命值!\n";
return;
}
if (selectedSkill.getName() == "防御姿态") {
isDefending = true;
mp -= selectedSkill.getMpCost();
cout << name << " 使用了 " << selectedSkill.getName() << "!\n";
cout << "下回合防御力将大幅提升!\n";
return;
}
// 普通攻击或重击
int baseDamage = attackPower;
int actualDefense = opponent.defensePower;
// 如果对手处于防御状态
if (opponent.isDefending) {
actualDefense *= 2;
}
if (selectedSkill.getName() == "重击") {
mp -= selectedSkill.getMpCost();
}
int damage = baseDamage * selectedSkill.getDamageMultiplier();
damage -= rand() % (actualDefense / 2 + 1);
if (damage < 1) damage = 1;
opponent.health -= damage;
if (opponent.health < 0) opponent.health = 0;
cout << name << " 对 " << opponent.getName()
<< " 使用了 " << selectedSkill.getName() << "!\n";
cout << "造成了 " << damage << " 点伤害!\n";
if (selectedSkill.getName() == "重击") {
cout << "*** 会心一击! ***\n";
}
}
bool isAlive() const { return health > 0; }
void showStatus() const {
cout << "+--------------------------------+\n";
cout << "| " << name << " 等级:" << level << " \n";
cout << "| HP: " << health << "/" << maxHealth << " ";
// 使用简单的ASCII进度条
int hpBars = (health * 20) / maxHealth;
cout << "[";
for (int i = 0; i < 20; i++) {
if (i < hpBars) cout << "=";
else cout << " ";
}
cout << "]\n";
cout << "| MP: " << mp << "/" << maxMp << " ";
int mpBars = (mp * 20) / maxMp;
cout << "[";
for (int i = 0; i < 20; i++) {
if (i < mpBars) cout << "#";
else cout << " ";
}
cout << "]\n";
cout << "| 攻击: " << attackPower << " 防御: " << defensePower;
if (isDefending) cout << " (防御+)";
cout << "\n";
cout << "| 经验: " << exp << "/" << (level * 100) << "\n";
cout << "| 金币: " << coins << "\n";
cout << "+--------------------------------+\n";
}
void showSkills() {
cout << "可使用的技能:\n";
for (size_t i = 0; i < skills.size(); i++) {
cout << i+1 << ". " << skills[i].getName();
cout << " (消耗MP: " << skills[i].getMpCost() << ")";
if (i == 0) cout << " [默认]";
cout << "\n 描述: " << skills[i].getDescription() << "\n";
}
}
int chooseSkill() {
int choice;
cout << "请选择技能 (1-" << skills.size() << "): ";
while (true) {
cin >> choice;
if (choice >= 1 && choice <= static_cast<int>(skills.size())) {
if (skills[choice-1].getMpCost() <= mp || choice == 1) {
return choice - 1;
} else {
cout << "MP不足!请重新选择: ";
}
} else {
cout << "无效选择,请重新输入: ";
}
}
}
void endTurn() {
// 每回合恢复少量MP
mp += 5;
if (mp > maxMp) mp = maxMp;
// 重置防御姿态效果
isDefending = false;
}
void gainExp(int amount) {
exp += amount;
cout << name << " 获得了 " << amount << " 点经验!\n";
if (exp >= level * 100) {
levelUp();
}
}
void gainCoins(int amount) {
coins += amount;
cout << name << " 获得了 " << amount << " 枚金币!\n";
}
void resetDefense() {
isDefending = false;
}
private:
void levelUp() {
while (exp >= level * 100) {
exp -= level * 100;
level++;
maxHealth += 20;
health = maxHealth;
maxMp += 10;
mp = maxMp;
attackPower += 5;
defensePower += 2;
cout << "\n================================\n";
cout << " 恭喜 " << name << " 升级了!\n";
cout << " 当前等级: " << level << "\n";
cout << " HP上限提升!\n";
cout << " MP上限提升!\n";
cout << " 攻击力提升!\n";
cout << " 防御力提升!\n";
cout << "================================\n\n";
}
}
};
// 修改函数参数
void showBattleAnimation(const string& attacker, const string& skill) {
cout << "\n";
cout << "\n";
cout << attacker << " 发动了 " << skill << "!\n";
cout << "\n";
cout << "\n";
// 简单的攻击动画
for (int i = 0; i < 3; i++) {
cout << "* ";
cout.flush();
#ifdef _WIN32
Sleep(300);
#else
usleep(300000);
#endif
}
cout << "\n\n";
}
void clearScreen() {
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
}
void showHeader(int round, int turn) {
cout << "\n";
cout << "\n";
cout << " 第 " << round << " 轮战斗\n";
cout << " 回合 " << turn << "\n";
cout << "\n\n";
}
int main() {
srand(time(0));
clearScreen();
cout << "========================================\n";
cout << " 欢迎来到奇幻对战游戏!\n";
cout << " 勇士们,为荣耀而战!\n";
cout << "========================================\n\n";
string player1Name, player2Name;
cout << "请输入第一位勇士的名字: ";
cin >> player1Name;
cout << "请输入第二位勇士的名字: ";
cin >> player2Name;
Player player1(player1Name);
Player player2(player2Name);
int turn = 1;
int round = 1;
while (player1.isAlive() && player2.isAlive()) {
clearScreen();
showHeader(round, turn);
// 显示双方状态
cout << "=== 对战双方状态 ===\n";
player1.showStatus();
cout << "\n";
player2.showStatus();
cout << "\n";
Player& attacker = (turn == 1) ? player1 : player2;
Player& defender = (turn == 1) ? player2 : player1;
cout << attacker.getName() << " 的回合!\n\n";
// 显示技能列表
attacker.showSkills();
cout << "\n";
// 选择技能
int skillChoice = attacker.chooseSkill();
// 显示攻击动画 - 修复这里
clearScreen();
showHeader(round, turn);
showBattleAnimation(attacker.getName(), attacker.getSkillName(skillChoice));
// 执行攻击
attacker.attack(defender, skillChoice);
// 回合结束处理
attacker.endTurn();
// 检查胜负
if (!defender.isAlive()) {
cout << "\n========================================\n";
cout << " " << defender.getName() << " 被击败了!\n";
cout << "\n";
cout << " *** " << attacker.getName() << " 获得了胜利! ***\n";
cout << "\n";
// 奖励
attacker.gainExp(50);
attacker.gainCoins(100);
// 显示胜利者信息
cout << "\n========================================\n";
cout << " 胜利者信息\n";
cout << "========================================\n";
attacker.showStatus();
cout << "========================================\n";
cout << "\n战斗结束!感谢游玩!\n";
break;
}
// 切换回合
turn = (turn == 1) ? 2 : 1;
if (turn == 1) round++;
// 继续下一回合
if (player1.isAlive() && player2.isAlive()) {
cout << "\n========================================\n";
cout << "按回车键继续下一回合...";
cin.ignore();
cin.get();
}
}
// 询问是否再玩一次
char choice;
cout << "\n是否再来一局?(y/n): ";
cin >> choice;
if (choice == 'y' || choice == 'Y') {
cout << "\n\n";
main(); // 重启游戏
} else {
cout << "\n感谢游玩!再见!\n";
}
return 0;
}
这里空空如也

















有帮助,赞一个