
hello大家好这是凌空制作的荒岛求生系列游戏此版本进行了较大的改动更改了游戏的框架等方面存档功能不易与旧版本兼容望能见谅#include iostream #include cstdio #include cstdlib #include ctime #include windows.h #include conio.h #include vector #include string #include fstream using namespace std; // 解决 Dev-C 兼容性问题 #define _CRT_SECURE_NO_WARNINGS // 工具函数 void delay(int ms) { Sleep(ms); } void clearScreen() { system(cls); } // 物品结构体 struct Item { string name; int health; // 血量加成 int hunger; // 饥饿值加成 int thirst; // 水分加成 int spirit; //心情加成 int buyPrice; // 购买价 int sellPrice;// 出售价 string introduce;//物品介绍 }; // 角色类 class Player { private: string name; int health; // 血量 int hunger; // 饥饿值 int thirst; // 水分 int fatigue; // 疲劳值 int spirit; //心情 bool isAlive; // 是否存活 bool isOut; // 是否外出 int outDays; // 外出天数 public: // 构造函数 Player(string n) : name(n), health(100), hunger(100), thirst(100), fatigue(0), spirit(70), isAlive(true), isOut(false), outDays(0) {} // 获取角色状态 string getName() { return name; } int getHealth() { return health; } int getHunger() { return hunger; } int getThirst() { return thirst; } int getFatigue() { return fatigue; } bool getIsAlive() { return isAlive; } bool getIsOut() { return isOut; } void setHealth(int h) { health max(0, min(100, h)); } void setHunger(int h) { hunger max(0, min(100, h)); } void setThirst(int t) { thirst max(0, min(100, t)); } void setFatigue(int f) { fatigue max(0, min(100, f)); } void setSpirit(int s) { spirit max(0, min(100, s)); } void setAlive(bool a) { isAlive a; } void setOut(bool o) { isOut o; } void setOutDays(int d) { outDays d; } int getSpirit() { return spirit; } int getOutDays() { return outDays; } // 更新每日状态 void dailyUpdate(int basedifficulty ,float adjdifficulty, int money,int mod,int jindu) { // 增加money引用用于探索奖励 if (!isAlive) return; // 1. 基础消耗难度平滑过渡 float baseConsume 5 (adjdifficulty - 1) * 3; hunger - static_castint(baseConsume); thirst - static_castint(baseConsume); // 2. 疲劳值与外出处理 if (!isOut) { // 在家恢复疲劳和心情难度影响较小 fatigue max(0, fatigue - (10 - (static_castint(adjdifficulty) - 1) * 2)); if(spirit70) spiritspirit-basedifficulty*3; else if(spirit30) spiritspiritbasedifficulty*2; } else { // 外出消耗 fatigue (10 (adjdifficulty - 1) * 5); health - (2 (adjdifficulty - 1) * 3); spirit-(3(basedifficulty)*5); outDays--; // 探索归来新增奖励逻辑 if (outDays 0) { isOut false; int reward rand()%(50 * static_castint(adjdifficulty)) 20 * adjdifficulty; money reward; jindumin(100,jindurand()%(20 * static_castint(adjdifficulty)) 5 * static_castint(adjdifficulty)5); cout name 探索归来获得 reward 金币 endl; } } // 3. 状态边界处理 hunger max(0, min(100, hunger)); thirst max(0, min(100, thirst)); fatigue max(0, min(100, fatigue)); // 4. 饥饿/口渴导致血量消耗 if (hunger 0){ health - (5 (adjdifficulty - 1) * 4); spirit-(basedifficulty*5); } if (thirst 0){ health - (5 (adjdifficulty - 1) * 4); spirit-(basedifficulty*5); } spiritmax(0,min(100,spirit)); // 5. 死亡判定 health max(0, min(100, health)); if (health 0) { isAlive false; cout name 已经不幸离世... endl; } } // 使用物品 void useItem(Item item) { health min(100, health item.health); hunger min(100, hunger item.hunger); thirst min(100, thirst item.thirst); spirit min(100, spirit item.spirit); } // 外出探索 void goOut(int days) { if (fatigue 30 isAlive) { isOut true; outDays days; fatigue 20; } } // 显示状态 void showStatus() { if (!isAlive) { cout name 已去世 endl; return; } if (isOut) { cout name 外出中剩余 outDays 天 endl; return; } cout name 状态; // 血量 cout 血量; if (health 70) cout 充足; else if (health 30) cout 良好; else cout 危险; // 饥饿 cout 饥饿; if (hunger 70) cout 很饱; else if (hunger 30) cout 适中; else cout 饥饿; // 水分 cout 水分; if (thirst 70) cout 充足; else if (thirst 30) cout 适中; else cout 缺水; // 疲劳 cout 疲劳; if (fatigue 70) cout 累趴; else if (fatigue 30) cout 疲劳; else cout 精神; //心情 cout心情; if(spirit 70) cout高兴; else if(spirit 30) cout平静; else cout抑郁; coutendl; } //计算心情对难度系数的影响 float spiritFactor1(){ if(spirit70) return 0.9; if(spirit30) return 1.0; return 1.1; } }; // 游戏管理类 class Game { private: vectorPlayer players; // 角色列表 vectorItem items; // 物品列表 vectorint backpack; // 背包物品数量 int day; // 当前天数 int money; // 金币 int difficulty; // 难度 string playerName; // 玩家姓名 int jindu; int currentMod; public: Game() : day(1), money(1000), difficulty(1),jindu(0),currentMod(0) { // 初始化物品 items.push_back({樱桃,5,10,5,5,150,100,好吃是好吃就是吃不饱}); items.push_back({香蕉, 10, 20, 0, 10, 500, 300,十分常见的水果能帮助缓解饥饿}); items.push_back({苹果, 15, 15, 10, 15, 500, 300,营养均衡啥都能补}); items.push_back({矿泉水, 0, 0, 30, 0, 250, 200,农夫山贼出品的天然矿泉水}); items.push_back({尖叫水, 0, 0, 25, 30, 400, 250,除了贵能补水、能提高心情的饮料}); items.push_back({不干净的水,0,0,25,-10,150,50,十分不卫生的水不过能喝就行}); items.push_back({绷带, 30, 0, 0, 0, 500, 300,关键时能救你小命}); items.push_back({信号枪, 0,0,0,0,50000,20000,使用后能快速结束本次游戏无尽除外并获得大量物资}); backpack {5,3, 3, 3, 3, 5, 1 , 1};//初始化物品 } // 开始游戏 void start() { srand(time(NULL)); showWelcome(); MainPage(); } // 欢迎界面 void showWelcome() { clearScreen(); cout endl; cout 荒岛求生 endl; cout endl; cout 按任意键开始游戏 endl; getch(); clearScreen(); cout 请输入你的名字; cin playerName; } // 创建角色 void createPlayers() { players.push_back(Player(playerName)); string name; for (int i 0; i 3; i) { clearScreen(); cout 请输入第 i2 个同伴的名字; cin name; players.push_back(Player(name)); } } //主页 void MainPage(){ currentMod0; clearScreen(); cout1.开始游戏endl; cout2.挑战模式endl; cout3.公告endl; cout4.关于游戏endl; cout5.存档/读档endl; cout6.退出游戏endl; resetGame(); jindu 0; day 1; int choisegetch()-0; switch (choise) { case 1: clearScreen(); cout正在加载中; delay(1000); cout.; delay(1000); cout.; delay(1000); cout.; delay(1000); clearScreen(); createPlayers(); cout 选择难度1-30为无尽; cin difficulty; if (difficulty 3||difficulty0) difficulty 1; mainLoop(); break; case 2: createPlayers(); cout本次挑战内容为调查岛屿endl; diaocha(); break; case 3: cout暂未开放endl; getch(); MainPage(); break; case 5: saveMenu(currentMod); break; default: cout暂未开放endl; getch(); MainPage(); break; } } void diaocha(){ currentMod2; getch(); clearScreen(); jindu0; day1; while (true) { clearScreen(); cout 第 day 天 endl; // 检查游戏结束条件 bool isWin; if (checkGameOver(isWin,currentMod)) { gameOver(isWin,currentMod); // 传入胜利状态 break; } // 每日更新 dailyUpdate(currentMod); cout当前调查进度jindu%endl; // 显示状态 showStatus(); // 玩家操作 playerAction(); day; } } void resetGame() { day 1; // 天数重置 // 角色状态重置复活、恢复初始状态 for (int i 0; i players.size(); i) { // 由于Player类成员是私有需添加重置方法或通过构造函数重建 players[i] Player(players[i].getName()); // 用原姓名重建角色 } } // 主游戏 void mainLoop() { currentMod1; while (true) { clearScreen(); cout 第 day 天 endl; // 检查游戏结束条件 bool isWin; if(difficulty!0backpack[7]0){ cout是否使用信号枪1-是 2-否endl; int choisegetch()-0; if(choise1){ backpack[7]--; cout 信号枪发射成功救援已确认奖励物资已到账 endl; getch(); for(int i0;ibackpack.size()-1;i) backpack[i]15; gameOver(true,currentMod); return; } } if (checkGameOver(isWin,currentMod)) { gameOver(isWin,1); // 传入胜利状态 break; } // 每日更新 dailyUpdate(currentMod); // 显示状态 showStatus(); // 玩家操作 playerAction(); day; } } // 【修改Game类的每日更新方法加入天气影响】 void dailyUpdate(int mod) { // 随机天气0:晴, 1:雨, 2:阴 int weather rand() % 3; cout \n今日天气; if (weather 0) cout 晴朗消耗减少10%; else if (weather 1) cout 下雨消耗增加10%; else cout 阴天正常消耗; cout endl; // 计算天气对难度的影响系数 float weatherFactor 1.0f; if (weather 0) weatherFactor 0.9f; // 晴天消耗减少 else if (weather 1) weatherFactor 1.1f; // 雨天消耗增加 // 对所有角色执行每日更新传入调整后的难度和金币引用 for (int i 0; i players.size(); i) { //计算心情对难度的影响系数 float spiritFactor 1.0f; spiritFactorplayers[i].spiritFactor1(); float adjustedDifficulty difficulty * weatherFactor*spiritFactor; players[i].dailyUpdate(difficulty,adjustedDifficulty, money,mod,jindu); // 传入money引用 } } // 显示状态 void showStatus() { cout \n【角色状态】 endl; for (int i 0; i players.size(); i) { players[i].showStatus(); } cout \n【背包】 endl; for (int i 0; i items.size(); i) { cout i1 . items[i].name x backpack[i] 效果items[i].introduceendl; } cout \n金币 money endl; } // 玩家操作 void playerAction() { cout \n【操作】 endl; cout 1. 分配食物 endl; cout 2. 派人外出探索 endl; cout 3. 商店 endl; cout 4.存档 endl; cout 5. 结束当天 endl; A: cout 请选择; int choice getch() - 0; switch (choice) { case 1: distributeFood(); break; case 2: sendExploration(); break; case 3: showShop(); break; case 4: saveMenu(currentMod); goto A; break; case 5: break; default: cout 无效选择 endl; delay(1000); } } // 分配食物 void distributeFood() { clearScreen(); cout 【分配食物】 endl; for (int i 0; i players.size(); i) { if (!players[i].getIsAlive() || players[i].getIsOut()) continue; cout 给 players[i].getName() 分配物品(1-是 2-否); int choice getch() - 0; if (choice ! 1) continue; cout \n选择物品 endl; for (int j 0; j items.size()-1; j) { if (backpack[j] 0) { cout j1 . items[j].name x backpack[j] endl; } } cout 请选择; char input getch(); if (input 1 || input 0 (items.size() - 1)) { // 物品索引0~items.size()-2排除信号枪 cout 无效选择 endl; delay(1000); continue; } int itemChoice (input - 0) - 1; if (itemChoice 0 itemChoice items.size() backpack[itemChoice] 0) { players[i].useItem(items[itemChoice]); backpack[itemChoice]--; cout 使用成功 endl; } else { cout 物品不足 endl; } delay(1000); clearScreen(); } } // 派人外出探索 void sendExploration() { clearScreen(); cout 【外出探索】 endl; vectorint validIndices; // 存储存活且未外出的角色索引 for (int i 0; i players.size(); i) { if (players[i].getIsAlive() !players[i].getIsOut()) { validIndices.push_back(i); cout validIndices.size() . players[i].getName() endl; } } if (validIndices.empty()) { cout 无可用角色外出 endl; delay(1000); return; } cout 选择外出的人1- validIndices.size() ; char input getch(); if (input 1 || input 0 validIndices.size()) { // 严格验证范围 cout \n无效选择 endl; delay(1000); return; } int idx validIndices[input - 1]; // 映射到实际索引 players[idx].goOut(rand() % 3 1); cout \n players[idx].getName() 已外出探索 endl; delay(1000); } // 商店 void showShop() { clearScreen(); cout 【商店】 当前金币 money endl; for (int i 0; i items.size(); i) { cout i1 . items[i].name 买入 items[i].buyPrice 卖出 items[i].sellPrice 持有 backpack[i]效果items[i].introduce endl; } cout 选择物品; int inputgetch(); if (input 1 || input 0 items.size()) { // 验证范围1~items.size() cout 无效选择 endl; delay(1000); return; } int choice (input - 0) - 1; // 转换为索引 cout 1.买入 2.卖出; int action getch() - 0; if (action 1) { cout 买入数量; int num; cin num; int total num * items[choice].buyPrice; if (total money) { money - total; backpack[choice] num; cout 买入成功 endl; } else { cout 金币不足 endl; } } else if (action 2) { cout 卖出数量; int num; cin num; if (num backpack[choice]) { money num * items[choice].sellPrice; backpack[choice] - num; cout 卖出成功 endl; } else { cout 物品不足 endl; } } delay(1000); } // 检查游戏结束 bool checkGameOver(bool isWin,int mod) { // 用引用返回是否胜利 // 统计存活人数 int aliveCount 0; for (int i 0; i players.size(); i) { if (players[i].getIsAlive()) aliveCount; } // 失败条件所有角色死亡 if (aliveCount 0) { isWin false; return true; // 游戏结束 } // 胜利条件非无尽模式且存活达到目标天数难度1→30天难度2→60天难度3→90天 if (difficulty ! 0 day difficulty * 30mod1) { isWin true; return true; // 游戏结束 } if(mod2jindu100){ isWintrue; return true; } // 未结束 return false; } // 游戏结束 void gameOver(bool b,int mod) { clearScreen(); if(!b) { cout 游戏结束 endl; cout 你在荒岛生存了 day 天 endl; if(mod1)cout但还是没能逃出这里……endl; else cout你最终没能调查完岛屿任务失败endl; cout 最终金币 money endl; cout 按任意键重启... endl; getch(); MainPage(); return; } else{ cout 游戏结束 endl; cout 你在荒岛生存了 day 天 endl; if(mod1){ cout等来了救援endl; if(difficulty1){ money10000; cout 最终金币 money 奖励10000金币 endl; } else if(difficulty2){ money20000; cout 最终金币 money 奖励20000金币 endl; } else{ money30000; cout 最终金币 money 奖励30000金币 endl; } } else{ money50000; cout你完成了对岛屿的调查endl; cout最终金币money奖励50000金币endl; } cout 按任意键重启... endl; getch(); MainPage(); return; } } // 保存游戏进度 void saveGame(int slot) { // 存档文件名如save1.dat, save2.dat string filename save to_string(slot) .dat; ofstream fout(filename, ios::out); if (!fout.is_open()) { cout 保存失败无法创建存档文件。 endl; delay(1000); return; } // 保存基本游戏状态 fout day endl; fout money endl; fout difficulty endl; fout jindu endl; fout players.size() endl; // 保存角色数量 // 保存每个角色的状态 for (int i 0; i players.size(); i) { fout players[i].getName() endl; fout players[i].getHealth() endl; fout players[i].getHunger() endl; fout players[i].getThirst() endl; fout players[i].getFatigue() endl; // 新增保存的状态 fout players[i].getSpirit() endl; // 需要在Player类添加getSpirit()方法 fout players[i].getIsAlive() endl; fout players[i].getIsOut() endl; fout players[i].getOutDays() endl; // 需要在Player类添加getOutDays()方法 } // 保存背包物品 fout backpack.size() endl; for (int i 0; i backpack.size(); i) { fout backpack[i] endl; } fout currentMod endl; fout.close(); cout 游戏已保存到存档位 slot endl; delay(1000); } // 加载游戏进度 bool loadGame(int slot) { string filename save to_string(slot) .dat; ifstream fin(filename, ios::in); if (!fin.is_open()) { cout 加载失败存档文件不存在。 endl; delay(1000); return false; } // 清空当前游戏状态 players.clear(); // 加载基本游戏状态 fin day; fin money; fin difficulty; fin jindu; // 加载角色 int playerCount; fin playerCount; fin.ignore(); // 忽略换行符 for (int i 0; i playerCount; i) { // 仅保留外层循环 string name; getline(fin, name); // 读取角色姓名 Player p(name); // 读取当前角色的完整状态 int health, hunger, thirst, fatigue, spirit; bool isAlive, isOut; int outDays; fin health hunger thirst fatigue spirit; fin isAlive isOut outDays; // 应用状态 p.setHealth(health); p.setHunger(hunger); p.setThirst(thirst); p.setFatigue(fatigue); p.setSpirit(spirit); p.setAlive(isAlive); p.setOut(isOut); p.setOutDays(outDays); players.push_back(p); // 添加当前角色 fin.ignore(); // 忽略换行符确保下一个角色姓名正确读取 } // 加载背包物品 int backpackSize; fin backpackSize; backpack.resize(backpackSize); backpack.resize(backpackSize, 0); for (int i 0; i backpackSize; i) { fin backpack[i]; } fin currentMod; fin.close(); cout 存档 slot 加载成功 endl; delay(1000); return true; } // 显示存档管理菜单 void saveMenu(int path) { clearScreen(); cout 存档管理 endl; cout 1. 保存到存档位 1 endl; cout 2. 保存到存档位 2 endl; cout 3. 保存到存档位 3 endl; cout 4. 从存档位 1 加载 endl; cout 5. 从存档位 2 加载 endl; cout 6. 从存档位 3 加载 endl; cout 7. 返回 endl; cout 请选择; int choice getch() - 0; switch (choice) { case 1: case 2: case 3: saveGame(choice); break; case 4: case 5: case 6: if (loadGame(choice - 3)) { if (currentMod 1) { mainLoop(); // 主游戏模式 } else if (currentMod 2) { diaocha(); // 挑战模式 } } break; case 7: if(path0) MainPage(); else if(path1) mainLoop(); else if(path2) diaocha(); break; default: cout 无效选择 endl; delay(1000); } saveMenu(path); } }; // 主函数 int main() { Game game; game.start(); return 0; }