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; } ``` 注意:这只是一个简单的示例代码,实际的游戏可能需要更多的功能和优化。

相关推荐

最新推荐

recommend-type

C++面向对象实现五子棋小游戏

本文介绍了如何运用面向对象思想进行五子棋游戏的设计与开发,与面向过程程序设计比较,面向对象程序设计更易于实现对现实世界的描述,提高软件的扩展性和可维护性。附上最终的程序源码,推荐给大家,有需要的小伙伴...
recommend-type

C++基于EasyX图形库实现2048小游戏

主要为大家详细介绍了C++基于EasyX图形库实现2048小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

学籍管理系统源代码 c++.docx

为了学校更加高效,快捷,方便的管理学生信息,并实现以下功能: (1)对学生信息进行录入:先输入学生的学籍,然后输入学生姓名,年龄,性别,籍贯,系别,专业,班级等,最后输入学生状态(入学)。...
recommend-type

使用C++调用Python代码的方法详解

主要介绍了使用C++调用Python代码并给大家介绍了.py和.pyc的区别,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
recommend-type

约瑟夫环问题用C++代码实现

8. 【题目】约瑟夫环(约瑟夫问题)是一个数学的应用问题:已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围。从编号为1的人开始报数,数到k的那个人出列;他的下一个人又从1开始报数,数到k的那个人又...
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

【实战演练】MATLAB用遗传算法改进粒子群GA-PSO算法

![MATLAB智能算法合集](https://static.fuxi.netease.com/fuxi-official/web/20221101/83f465753fd49c41536a5640367d4340.jpg) # 2.1 遗传算法的原理和实现 遗传算法(GA)是一种受生物进化过程启发的优化算法。它通过模拟自然选择和遗传机制来搜索最优解。 **2.1.1 遗传算法的编码和解码** 编码是将问题空间中的解表示为二进制字符串或其他数据结构的过程。解码是将编码的解转换为问题空间中的实际解的过程。常见的编码方法包括二进制编码、实数编码和树形编码。 **2.1.2 遗传算法的交叉和
recommend-type

openstack的20种接口有哪些

以下是OpenStack的20种API接口: 1. Identity (Keystone) API 2. Compute (Nova) API 3. Networking (Neutron) API 4. Block Storage (Cinder) API 5. Object Storage (Swift) API 6. Image (Glance) API 7. Telemetry (Ceilometer) API 8. Orchestration (Heat) API 9. Database (Trove) API 10. Bare Metal (Ironic) API 11. DNS
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。