老铁们,咱们来做一个人生重开模拟器。
咱们不说废话,开始吧。
#include <iostream> #include <fstream> #include <string> #include <vector> #include <cstdlib> #include <ctime> using namespace std; // 定义人物类 class Character { public: Character() {} Character(string name, int age, string gender, int level) : m_name(name), m_age(age), m_gender(gender), m_level(level) {} // 设置和获取角色基本信息 void setName(string name) { m_name = name; } string getName() const { return m_name; } void setAge(int age) { m_age = age; } int getAge() const { return m_age; } void setGender(string gender) { m_gender = gender; } string getGender() const { return m_gender; } void setLevel(int level) { m_level = level; } int getLevel() const { return m_level; } // 输出角色信息 void displayInfo() const { cout << "姓名:" << m_name << endl; cout << "年龄:" << m_age << endl; cout << "性别:" << m_gender << endl; cout << "等级:" << m_level << endl; } private: string m_name; int m_age; string m_gender; int m_level; }; // 定义游戏类 class Game { public: Game() {} // 加载角色信息 void loadCharacter() { ifstream inFile; inFile.open("character.txt"); if (!inFile.is_open()) { cout << "无法打开角色文件" << endl; return; } string name; int age; string gender; int level; while (inFile >> name >> age >> gender >> level) { Character newChar(name, age, gender, level); m_characters.push_back(newChar); } inFile.close(); } // 保存角色信息 void saveCharacter() { ofstream outFile; outFile.open("character.txt", ios::trunc); if (!outFile.is_open()) { cout << "无法打开角色文件" << endl; return; } for (int i = 0; i < m_characters.size(); i++) { outFile << m_characters[i].getName() << " "; outFile << m_characters[i].getAge() << " "; outFile << m_characters[i].getGender() << " "; outFile << m_characters[i].getLevel() << endl; } outFile.close(); } // 添加新角色 void addCharacter() { string name; int age; string gender; cout << "请输入姓名:"; cin >> name; cout << "请输入年龄:"; cin >> age; cout << "请输入性别:"; cin >> gender; Character newChar(name, age, gender, 1); m_characters.push_back(newChar); } // 升级角色等级 void levelup() { cout << "请输入要升级的角色编号:"; int idx; cin >> idx; if (idx < 1 || idx > m_characters.size()) { cout << "无效的角色编号!" << endl; return; } m_characters[idx - 1].setLevel(m_characters[idx - 1].getLevel() + 1); cout << "角色 " << m_characters[idx - 1].getName() << " 的等级已升至 " << m_characters[idx - 1].getLevel() << " 级!" << endl; } // 随机事件 void randomEvent() { srand((unsigned)time(NULL)); int event = rand() % 3; // 随机事件:0-无事件,1-遇到强敌,2-捡到宝物 switch (event) { case 0: cout << "什么都没有发生。" << endl; break; case 1: cout << "你遇到了一个强敌!是否与其战斗或逃跑?" << endl; break; case 2: cout << "恭喜你,你发现了一个宝藏!" << endl; break; default: break; } } // 删除角色 void deleteCharacter() { cout << "请输入要删除的角色编号:"; int idx; cin >> idx; if (idx < 1 || idx > m_characters.size()) { cout << "无效的角色编号!" << endl; return; } m_characters.erase(m_characters.begin() + idx - 1); cout << "已删除角色!" << endl; } // 获取角色列表 const vector<Character>& getCharacters() const { return m_characters; } // 发送礼物 void sendGift() { cout << "请输入要送礼物的角色编号:"; int idx; cin >> idx; if (idx < 1 || idx > m_characters.size()) { cout << "无效的角色编号!" << endl; return; } int gift; cout << "请选择要送出的礼物:1-金币,2-经验,3-装备" << endl; cin >> gift; switch (gift) { case 1: cout << "角色 " << m_characters[idx - 1].getName() << " 收到了 100 个金币!" << endl; break; case 2: cout << "角色 " << m_characters[idx - 1].getName() << " 收到了 10 点经验值!" << endl; break; case 3: cout << "角色 " << m_characters[idx - 1].getName() << " 收到了一件装备!" << endl; break; default: break; } } // 帮助 void help() { cout << "----------------------" << endl; cout << "1. 添加新角色:" << endl; cout << " 在游戏中添加一个新的角色。" << endl; cout << "2. 查看角色列表:" << endl; cout << " 显示当前所有的角色,并随机发生一个事件。" << endl; cout << "3. 升级角色等级:" << endl; cout << " 提升某个角色的等级。" << endl; cout << "4. 随机事件:" << endl; cout << " 随机发生一个事件。" << endl; cout << "5. 删除角色:" << endl; cout << " 删除某个角色。" << endl; cout << "6. 发送礼物:" << endl; cout << " 向某个角色发送礼物,可以是金币、经验或装备。" << endl; cout << "7. 帮助:" << endl; cout << " 显示游戏帮助信息。" << endl; cout << "8. 保存并退出:" << endl; cout << " 保存当前游戏进度并退出游戏。" << endl; cout << "----------------------" << endl; } private: vector<Character> m_characters; }; // 主函数 int main() { Game game; game.loadCharacter(); while (true) { cout << endl; cout << "人生重开模拟器" << endl; cout << "----------------------" << endl; cout << "请选择操作:" << endl; cout << "1. 添加新角色" << endl; cout << "2. 查看角色列表" << endl; cout << "3. 升级角色等级" << endl; cout << "4. 随机事件" << endl; cout << "5. 删除角色" << endl; cout << "6. 发送礼物" << endl; cout << "7. 帮助" << endl; cout << "8. 保存并退出" << endl; int choice = 0; cin >> choice; switch (choice) { case 1: game.addCharacter(); break; case 2: { const vector<Character>& characters = game.getCharacters(); for (int i = 0; i < characters.size(); i++) { cout << "----------------------" << endl; characters[i].displayInfo(); } cout << "----------------------" << endl; game.randomEvent(); break; } case 3: game.levelup(); break; case 4: game.randomEvent(); break; case 5: game.deleteCharacter(); break; case 6: game.sendGift(); break; case 7: game.help(); break; case 8: game.saveCharacter(); return 0; default: break; } } return 0; }
今天的文章
c++小游戏——人生重开模拟器分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/bian-cheng-ji-chu/96908.html