C++文字冒险小游戏代码
时间: 2023-07-31 08:13:35 浏览: 56
好的,以下是一个简单的 C++ 文字冒险小游戏的代码,供你参考:
```cpp
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
// 定义魔法属性枚举类型
enum MagicType {
FIRE,
ICE,
LIGHTNING
};
// 定义魔法师类
class Wizard {
public:
Wizard(string name, MagicType magicType) {
this->name = name;
this->magicType = magicType;
this->level = 1;
this->exp = 0;
this->gold = 0;
}
// 获取魔法师名字
string getName() {
return this->name;
}
// 获取魔法属性
MagicType getMagicType() {
return this->magicType;
}
// 获取魔法师等级
int getLevel() {
return this->level;
}
// 获取魔法师经验值
int getExp() {
return this->exp;
}
// 获取魔法师金币
int getGold() {
return this->gold;
}
// 增加经验值
void addExp(int exp) {
this->exp += exp;
if (this->exp >= this->level * 10) {
levelUp();
}
}
// 增加金币
void addGold(int gold) {
this->gold += gold;
}
private:
string name; // 魔法师名字
MagicType magicType; // 魔法属性
int level; // 魔法师等级
int exp; // 魔法师经验值
int gold; // 魔法师金币
// 升级
void levelUp() {
this->level++;
this->exp = 0;
cout << "恭喜你升级了!当前等级为:" << this->level << endl;
}
};
// 定义场景枚举类型
enum SceneType {
START,
FOREST,
CAVE,
TOWN,
BOSS
};
// 定义场景类
class Scene {
public:
Scene(string name, SceneType sceneType) {
this->name = name;
this->sceneType = sceneType;
}
// 获取场景名字
string getName() {
return this->name;
}
// 获取场景类型
SceneType getSceneType() {
return this->sceneType;
}
// 进入场景
void enter(Wizard& wizard) {
cout << "进入" << this->name << "场景" << endl;
switch (this->sceneType) {
case START:
cout << "欢迎来到魔法世界!" << endl;
break;
case FOREST:
cout << "你来到一片森林,里面有许多魔法怪物,要小心!" << endl;
battle(wizard);
break;
case CAVE:
cout << "你来到了一个山洞,里面似乎有什么宝藏!" << endl;
explore(wizard);
break;
case TOWN:
cout << "你来到了一个小镇,可以购买装备和药品!" << endl;
shop(wizard);
break;
case BOSS:
cout << "你来到了最终Boss的巢穴,准备好战斗了吗?" << endl;
battle(wizard, true);
break;
default:
break;
}
}
private:
string name; // 场景名字
SceneType sceneType; // 场景类型
// 进行战斗
void battle(Wizard& wizard, bool isBoss = false) {
int enemyHp = rand() % 10 + 1;
int playerHp = 10;
int round = 1;
cout << "敌人出现了!它的生命值为:" << enemyHp << endl;
while (enemyHp > 0 && playerHp > 0) {
int playerDamage = rand() % 3 + 1;
int enemyDamage = rand() % 3 + 1;
if (isBoss) {
playerDamage += wizard.getLevel();
enemyDamage += wizard.getLevel();
}
cout << "第" << round << "回合:你使用了魔法攻击,造成了" << playerDamage << "点伤害!" << endl;
enemyHp -= playerDamage;
if (enemyHp <= 0) {
cout << "你战胜了敌人!获得了" << wizard.getLevel() << "点经验值和10个金币!" << endl;
wizard.addExp(wizard.getLevel());
wizard.addGold(10);
break;
}
cout << "敌人使用了魔法攻击,造成了" << enemyDamage << "点伤害!" << endl;
playerHp -= enemyDamage;
if (playerHp <= 0) {
cout << "你被敌人打败了!" << endl;
break;
}
round++;
}
}
// 探索宝藏
void explore(Wizard& wizard) {
int gold = rand() % 10 + 1;
cout << "你发现了一些金币,共" << gold << "个!" << endl;
wizard.addGold(gold);
}
// 商店购买
void shop(Wizard& wizard) {
int choice;
while (true) {
cout << "请选择购买项目:" << endl;
cout << "1. 魔法药品(10金币/个)" << endl;
cout << "2. 魔法装备(50金币/个)" << endl;
cout << "3. 离开商店" << endl;
cin >> choice;
if (choice == 1) {
if (wizard.getGold() < 10) {
cout << "你的金币不足!" << endl;
} else {
wizard.addGold(-10);
cout << "购买成功!你的金币还剩下:" << wizard.getGold() << endl;
}
} else if (choice == 2) {
if (wizard.getGold() < 50) {
cout << "你的金币不足!" << endl;
} else {
wizard.addGold(-50);
cout << "购买成功!你的金币还剩下:" << wizard.getGold() << endl;
}
} else if (choice == 3) {
break;
} else {
cout << "选择无效!" << endl;
}
}
}
};
int main() {
srand(time(nullptr)); // 初始化随机数种子
string name;
int magicTypeChoice;
cout << "欢迎来到魔法世界!请输入你的名字:" << endl;
cin >> name;
cout << "请选择你的魔法属性:" << endl;
cout << "1. 火" << endl;
cout << "2. 冰" << endl;
cout << "3. 雷" << endl;
cin >> magicTypeChoice;
MagicType magicType;
switch (magicTypeChoice) {
case 1:
magicType = FIRE;
break;
case 2:
magicType = ICE;
break;
case 3:
magicType = LIGHTNING;
break;
default:
magicType = FIRE;
break;
}
Wizard wizard(name, magicType);
Scene startScene("起始之地", START);
Scene forestScene("森林", FOREST);
Scene caveScene("山洞", CAVE);
Scene townScene("小镇", TOWN);
Scene bossScene("最终Boss巢穴", BOSS);
Scene* currentScene = &startScene; // 当前场景
while (true) {
currentScene->enter(wizard);
if (currentScene->getSceneType() == BOSS && wizard.getExp() >= wizard.getLevel() * 100) {
cout << "恭喜你通关了!" << endl;
break;
}
int choice;
cout << "请选择下一步:" << endl;
cout << "1. 前往森林" << endl;
cout << "2. 前往山洞" << endl;
cout << "3. 前往小镇" << endl;
if (currentScene->getSceneType() == BOSS) {
cout << "4. 再次挑战最终Boss" << endl;
}
cin >> choice;
if (choice == 1) {
currentScene = &forestScene;
} else if (choice == 2) {
currentScene = &caveScene;
} else if (choice == 3) {
currentScene = &townScene;
} else if (choice == 4 && currentScene->getSceneType() == BOSS) {
currentScene = &bossScene;
} else {
cout << "选择无效!" << endl;
}
}
return 0;
}
```
注意:这只是一个简单的示例代码,实际的游戏可能需要更多的功能和优化。