- MysGln 的博客
cpp
- @ 2024-12-7 15:56:09
#include #include #include #include #include #include #include using namespace std;
void clearScreen() { for(int i = 0; i < 50; i++) cout << endl; }
// 基本字符画 const string MONSTER[] = { R"( >=< )", R"( [o_o] )", R"( /╹◡╹\ )", R"( |◕‿◕| )" };
const string PLAYER = R"(
0
/|
/ \ )";
const string SWORD = " †=>"; const string SHIELD = "[|]"; const string POTION = "○";
// 游戏角色类 class Character { protected: string name; int health; int maxHealth; int attack; int defense;
public: Character(string n, int h, int a, int d) : name(n), health(h), maxHealth(h), attack(a), defense(d) {}
bool isAlive() const { return health > 0; }
string getName() const { return name; }
int getHealth() const { return health; }
int getMaxHealth() const { return maxHealth; }
int getAttack() const { return attack; }
void takeDamage(int damage) {
damage = max(1, damage - defense);
health = max(0, health - damage);
}
void heal(int amount) {
health = min(maxHealth, health + amount);
}
string getHealthBar() const {
string bar = "[";
int barLength = 10;
int filledLength = (health * barLength) / maxHealth;
for(int i = 0; i < barLength; i++) {
bar += (i < filledLength) ? "♥" : ".";
}
bar += "]";
return bar;
}
};
// 玩家类 class Player : public Character { private: int level; int exp; int potions;
public: Player(string n) : Character(n, 100, 20, 5), level(1), exp(0), potions(3) {}
void gainExp(int amount) {
exp += amount;
if(exp >= level * 100) {
levelUp();
}
}
void levelUp() {
level++;
maxHealth += 20;
health = maxHealth;
attack += 5;
defense += 2;
exp = 0;
cout << "\n★ 升级!现在是等级 " << level << " ★\n";
}
bool usePotion() {
if(potions > 0) {
potions--;
heal(50);
return true;
}
return false;
}
void showStatus() {
cout << "\n===== " << name << " 的状态 =====\n";
cout << "等级: " << level << " |经验: " << exp << "/" << level * 100 << endl;
cout << "生命: " << getHealthBar() << " (" << health << "/" << maxHealth << ")\n";
cout << "攻击: " << attack << " |防御: " << defense << endl;
cout << "药水: " << potions << "瓶\n";
cout << "=====================\n";
}
};
// 怪物类 class Monster : public Character { private: int expValue;
public: Monster(string n, int h, int a, int d, int exp) : Character(n, h, a, d), expValue(exp) {}
int getExpValue() const { return expValue; }
void showStatus() {
cout << name << "\n";
cout << "生命: " << getHealthBar() << endl;
}
};
// 战斗系统 class Battle { private: Player& player; Monster& monster;
void showBattleScreen() {
clearScreen();
cout << "\n=== 战斗开始! ===\n\n";
cout << MONSTER[rand() % 4] << endl;
monster.showStatus();
cout << "\n VS\n\n";
cout << PLAYER << endl;
player.showStatus();
}
void showBattleMenu() {
cout << "\n选择行动:\n";
cout << "1. 攻击 " << SWORD << endl;
cout << "2. 防御 " << SHIELD << endl;
cout << "3. 使用药水 " << POTION << endl;
cout << "4. 逃跑\n";
}
void animateAttack(bool isPlayer) {
string attack = isPlayer ? ">>>" : "<<<";
cout << (isPlayer ? "\n玩家攻击: " : "\n怪物攻击: ");
for(int i = 0; i < 3; i++) {
cout << attack;
cout.flush();
this_thread::sleep_for(chrono::milliseconds(100));
cout << "\b\b\b \b\b\b";
cout.flush();
this_thread::sleep_for(chrono::milliseconds(100));
}
cout << endl;
}
public: Battle(Player& p, Monster& m) : player(p), monster(m) {}
bool start() {
while(player.isAlive() && monster.isAlive()) {
showBattleScreen();
showBattleMenu();
int choice;
cout << "你的选择: ";
cin >> choice;
switch(choice) {
case 1: {
animateAttack(true);
int damage = player.getAttack();
monster.takeDamage(damage);
cout << "你对" << monster.getName() << "造成了 " << damage << " 点伤害!\n";
break;
}
case 2:
cout << "\n你架起盾牌准备防御!\n";
break;
case 3:
if(player.usePotion()) {
cout << "\n你使用了一瓶药水,恢复了50点生命!\n";
} else {
cout << "\n你没有药水了!\n";
}
break;
case 4:
if(rand() % 2) {
cout << "\n成功逃跑!\n";
return true;
} else {
cout << "\n逃跑失败!\n";
}
break;
default:
continue;
}
if(monster.isAlive()) {
this_thread::sleep_for(chrono::milliseconds(1000));
animateAttack(false);
int damage = monster.getAttack();
player.takeDamage(damage);
cout << monster.getName() << "对你造成了 " << damage << " 点伤害!\n";
this_thread::sleep_for(chrono::milliseconds(1000));
}
}
if(player.isAlive()) {
cout << "\n战斗胜利!获得 " << monster.getExpValue() << " 点经验值!\n";
player.gainExp(monster.getExpValue());
return true;
}
return false;
}
};
// 游戏主类 class Game { private: Player player; vector monsters;
void createMonsters() {
monsters = {
Monster("小史莱姆", 50, 10, 2, 20),
Monster("骷髅战士", 70, 15, 3, 35),
Monster("地精", 60, 12, 2, 25),
Monster("兽人", 100, 20, 5, 50)
};
}
void showTitle() {
cout << R"(
╔═════════════════════════════╗
║ ASCII 冒险游戏 ║
╠═════════════════════════════╣
║ 1. 探索 ║
║ 2. 查看状态 ║
║ 3. 休息 ║
║ 4. 退出 ║
╚═════════════════════════════╝
)" << endl;
}
public: Game(string playerName) : player(playerName) { createMonsters(); }
void run() {
bool running = true;
while(running && player.isAlive()) {
clearScreen();
showTitle();
int choice;
cout << "请选择行动: ";
cin >> choice;
switch(choice) {
case 1: {
if(rand() % 100 < 70) {
Monster& monster = monsters[rand() % monsters.size()];
cout << "\n你遇到了 " << monster.getName() << "!\n";
this_thread::sleep_for(chrono::milliseconds(1000));
Battle battle(player, monster);
if(!battle.start() && !player.isAlive()) {
cout << "\n游戏结束!\n";
running = false;
}
} else {
cout << "\n你找到了一个宝箱!获得一瓶药水!\n";
player.heal(30);
}
cout << "\n按回车继续...";
cin.ignore();
cin.get();
break;
}
case 2:
clearScreen();
player.showStatus();
cout << "\n按回车继续...";
cin.ignore();
cin.get();
break;
case 3: {
clearScreen();
cout << "休息中";
for(int i = 0; i < 3; i++) {
cout << ".";
cout.flush();
this_thread::sleep_for(chrono::milliseconds(500));
}
player.heal(50);
cout << "\n休息完成!恢复了50点生命值。\n";
this_thread::sleep_for(chrono::milliseconds(1000));
break;
}
case 4:
running = false;
break;
}
}
}
};
int main() { srand(time(0)); string name; cout << "欢迎来到ASCII冒险游戏!\n"; cout << "请输入你的名字: "; getline(cin, name);
Game game(name);
game.run();
return 0;
}