ARTICLE DETAIL

资讯详情

深耕网站建设与运营推广的一线实战洞察。

荒岛求生1.1.7版本

荒岛求生1.1.7版本 这个版本主要修改了存档功能避免未来进行物品更新时出现bug除此之外我还对已知bug进行了修复提升游玩的流畅度。下面是游戏代码#include iostream #include cstdio #include cstdlib #include ctime #include windows.h #include conio.h #include vector #include string #include fstream #include algorithm using namespace std; #define _CRT_SECURE_NO_WARNINGS // 延时函数单位毫秒 void delay(int ms) { Sleep(ms); } // 清屏 void clearScreen() { system(cls); } // 外出探索返回结果结构体 struct ExploreRet { int addMoney; int addJindu; bool isBack; ExploreRet() : addMoney(0), addJindu(0), isBack(false) {} }; // 物品数据结构 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; } int getSpirit() { return spirit; } int getOutDays() { return outDays; } 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; } // 每日状态更新处理消耗、外出结算 ExploreRet dailyUpdate(int basedifficulty, float adjdifficulty) { ExploreRet ret; if (!isAlive) return ret; float baseConsume 5 (adjdifficulty - 1) * 3; hunger - static_castint(baseConsume); thirst - static_castint(baseConsume); if (!isOut) { fatigue max(0, fatigue - (10 - (static_castint(adjdifficulty) - 1) * 2)); if (spirit 70) spirit spirit - basedifficulty * 3; else if (spirit 30) spirit spirit basedifficulty * 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 * basedifficulty) 1000 * basedifficulty; ret.addMoney reward; ret.addJindu rand() % (7 * static_castint(adjdifficulty)); ret.isBack true; cout name 探索归来获得 reward 金币 endl; } } // 限制数值区间0?100 hunger max(0, min(100, hunger)); thirst max(0, min(100, thirst)); fatigue max(0, min(100, fatigue)); // 饥饿缺水扣血扣心情 if (hunger 0) { health - (5 (adjdifficulty - 1) * 4); spirit - (basedifficulty * 5); } if (thirst 0) { health - (5 (adjdifficulty - 1) * 4); spirit - (basedifficulty * 5); } spirit max(0, min(100, spirit)); health max(0, min(100, health)); // 血量归0角色死亡 if (health 0) { isAlive false; cout name 已经不幸离世... endl; } return ret; } // 使用物品叠加属性上限100 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); } // 角色出发外出探索疲劳必须小于30 bool goOut(int days) { if (fatigue 30 isAlive) { isOut true; outDays days; fatigue 20; return true; } return false; } // 打印角色状态文本描述 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 抑郁; cout endl; } // 根据心情返回难度修正系数 float spiritFactor1() { if (spirit 70) return 0.9f; if (spirit 30) return 1.0f; return 1.1f; } }; // 游戏主管理类 class Game { private: vectorPlayer players; vectorItem items; vectorint backpack; int day; int money; int difficulty; string playerName; int jindu; int currentMod; int zd_max; int zd; int wz; int wjday_max; int todayWeather; bool title[100] { false }; // 文件加密异或密钥 const vectorunsigned char key { 0x1A, 0x3B, 0x5C, 0x7D, 0x9E }; // 异或加密/解密文件读写都调用一次 void cryptFile(const string filename) { ifstream in(filename, ios::binary); if (!in.is_open()) return; vectorunsigned char data((istreambuf_iteratorchar(in)), istreambuf_iteratorchar()); in.close(); for (size_t i 0; i data.size(); i) { data[i] ^ key[i % key.size()]; } ofstream out(filename, ios::binary); out.write((char*)data.data(), data.size()); out.close(); } // 保存玩家名字列表 void saveUser(int slot) { string fname user to_string(slot) .dat; ofstream fout(fname); if (!fout.is_open()) return; fout players.size() endl; for (auto p : players) { fout p.getName() endl; } fout.close(); cryptFile(fname); } // 读取存档里玩家名字列表 bool loadUser(int slot, vectorstring outNames) { string fname user to_string(slot) .dat; cryptFile(fname); ifstream fin(fname); if (!fin.is_open()) { cryptFile(fname); return false; } int cnt; fin cnt; fin.ignore(); outNames.clear(); for (int i 0; i cnt; i) { string nm; getline(fin, nm); outNames.push_back(nm); } fin.close(); cryptFile(fname); return true; } public: Game() : wjday_max(0), wz(-1), zd(0), zd_max(-1), day(1), money(1000), difficulty(1), jindu(0), currentMod(0) { // 初始化全部物品 items.push_back({ 信号枪, 0, 0, 0, 0, 50000, 20000, 使用后能快速结束本次游戏无尽、挑战除外并获得大量物资 }); 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, 关键时能救你小命 }); // 初始背包物品数量 backpack { 1,5, 3, 3, 3, 3, 5, 1 }; } // 游戏入口 void start() { srand(time(NULL)); showWelcome(); MainPage(); return; } // 欢迎界面输入主角名字 void showWelcome() { clearScreen(); cout endl; cout 荒岛求生 endl; cout endl; cout 按任意键开始游戏 endl; _getch(); clearScreen(); cout 请输入你的名字; cin playerName; cin.ignore(); return; } // 创建4名角色主角3同伴 void createPlayers() { players.push_back(Player(playerName)); string name; for (int i 0; i 3; i) { clearScreen(); cout 请输入第 i 2 个同伴的名字; cin name; cin.ignore(); players.push_back(Player(name)); } return; } // 重置游戏天数、进度角色恢复初始状态 void resetGame() { day 1; jindu 0; for (int i 0; i players.size(); i) { players[i] Player(players[i].getName()); } return; } // 主菜单循环 void MainPage() { while (1) { if (currentMod 1) { mainLoop(); currentMod 0; } if (currentMod 2) { diaocha(); currentMod 0; } clearScreen(); cout 1.开始游戏 endl; cout 2.挑战模式 endl; cout 3.公告 endl; cout 4.关于游戏 endl; cout 5.存档/读档 endl; cout 6.退出游戏 endl; int choise _getch() - 0; switch (choise) { case 1: clearScreen(); cout 正在加载中; delay(1000); cout .; delay(1000); cout .; delay(1000); cout .; delay(1000); clearScreen(); if (players.size() 0) createPlayers(); resetGame(); cout 选择难度1-30为无尽; cin difficulty; cin.ignore(); if (difficulty 3 || difficulty 0) difficulty 1; mainLoop(); currentMod 0; break; case 2: if (players.size() 0) createPlayers(); resetGame(); cout 本次挑战内容为调查岛屿 endl; difficulty 3; diaocha(); currentMod 0; break; case 3: Notice(); break; case 4: cout 暂未开放 endl; _getch(); break; case 5: saveMenu(currentMod); break; default: exit(0); break; } } return; } // 公告界面 void Notice() { clearScreen(); cout \n荒岛求生出新版本啦这个版本我们修改了存档功能修复了部分BUG。欢迎前来体验\n; cout \n 9.5 endl; _getch(); return; } // 挑战模式调查岛屿主循环 void diaocha() { currentMod 2; _getch(); clearScreen(); while (true) { clearScreen(); cout 第 day 天 endl; zd; // 自动存档计数判断 if (zd zd_max zd_max 0) { saveGame(wz); zd 0; } bool isWin; if (checkGameOver(isWin)) { gameOver(isWin); break; } dailyUpdate(); cout 当前调查进度 jindu % endl; if (jindu 100) { int remain 50 - day; if (remain 0) remain 0; cout 救援队正在赶往还需 remain 天 endl; } showStatus(); triggerRandomEvent(); playerAction(); day; } return; } // 随机事件触发判定 void triggerRandomEvent() { int baseProb 30; int diffAdd (difficulty 0 ? 1 : difficulty) * 10; int weatherAdd 0; if (todayWeather 1) weatherAdd 15; else if (todayWeather 0) weatherAdd -5; int finalProb max(10, min(60, baseProb diffAdd weatherAdd)); int randomNum rand() % 100; if (randomNum finalProb) return; int eventCount 8; int eventId rand() % eventCount; cout \n 突发随机事件 endl; executeEvent(eventId); cout endl; delay(2000); return; } // 执行具体随机事件逻辑 void executeEvent(int eventId) { vectorint validPlayers; for (int i 0; i players.size(); i) { if (players[i].getIsAlive() !players[i].getIsOut()) { validPlayers.push_back(i); } } if (validPlayers.empty()) return; int randomPlayerIdx validPlayers[rand() % validPlayers.size()]; Player p players[randomPlayerIdx]; switch (eventId) { case 0: { cout 【幸运】 p.getName() 在路边发现了一片野果林采摘后补充了饥饿 endl; p.setHunger(p.getHunger() 20); p.setSpirit(p.getSpirit() 10); break; } case 1: { cout 【倒霉】 p.getName() 被荒岛蚊虫叮咬皮肤红肿血量和心情下降 endl; p.setHealth(p.getHealth() - 10); p.setSpirit(p.getSpirit() - 15); break; } case 2: { int gold rand() % 500 * (difficulty 0 ? 1 : difficulty) 200; cout 【幸运】全体成员在沙滩捡到了 gold 枚金币 endl; money gold; break; } case 3: { cout 【意外】 p.getName() 在行走时崴到了脚血量下降疲劳值骤增 endl; p.setHealth(p.getHealth() - 15); p.setFatigue(p.getFatigue() 25); break; } case 4: { cout 【幸运】发现一处天然清泉全体存活成员的水分值大幅恢复 endl; for (int i 0; i players.size(); i) { if (players[i].getIsAlive() !players[i].getIsOut()) { players[i].setThirst(players[i].getThirst() 30); } } break; } case 5: { int itemIdx rand() % (items.size() - 1); int loseNum rand() % 3 1; backpack[itemIdx] max(0, backpack[itemIdx] - loseNum); cout 【倒霉】天气潮湿 items[itemIdx].name 受潮损坏了 loseNum 个 endl; break; } case 6: { cout 【探索】发现一个废弃的物资箱是否上前拾取1-拾取 2-放弃 endl; int choice _getch() - 0; if (choice 1) { int randRes rand() % 2; if (randRes 0) { int goodItem rand() % (items.size() - 1); backpack[goodItem] 2; cout 【幸运】物资箱里有2个 items[goodItem].name 拾取成功 endl; } else { cout 【陷阱】物资箱有野兽陷阱 p.getName() 被误伤血量大幅下降 endl; p.setHealth(p.getHealth() - 25); } } else { cout 你选择放弃拾取安全离开。 endl; } break; } case 7: { cout 【休整】大家决定原地休息半天疲劳值降低心情有所好转 endl; for (int i 0; i players.size(); i) { if (players[i].getIsAlive() !players[i].getIsOut()) { players[i].setFatigue(players[i].getFatigue() - 20); players[i].setSpirit(players[i].getSpirit() 10); } } break; } } return; } // 普通生存模式主循环 void mainLoop() { currentMod 1; while (true) { clearScreen(); cout 第 day 天 endl; zd; if (zd zd_max zd_max 0) { saveGame(wz); zd 0; } bool isWin; if (checkGameOver(isWin)) { gameOver(isWin); break; } // 信号枪快速通关 if (difficulty ! 0 backpack[0] 0) { cout 是否使用信号枪1-是 2-否 endl; int choise _getch() - 0; if (choise 1) { backpack[0]--; cout 信号枪发射成功救援已确认奖励物资已到账 endl; _getch(); for (int i 1; i backpack.size(); i) backpack[i] 15; gameOver(true); return; } } dailyUpdate(); showStatus(); triggerRandomEvent(); playerAction(); day; } return; } // 全局每日更新生成天气、遍历所有角色执行每日消耗 void dailyUpdate() { todayWeather rand() % 3; cout \n今日天气; if (todayWeather 0) cout 晴朗消耗减少10%; else if (todayWeather 1) cout 下雨消耗增加10%; else cout 阴天正常消耗; cout endl; float weatherFactor 1.0f; if (todayWeather 0) weatherFactor 0.9f; else if (todayWeather 1) weatherFactor 1.1f; for (int i 0; i players.size(); i) { float spiritFactor players[i].spiritFactor1(); float adjustedDifficulty difficulty * weatherFactor * spiritFactor; ExploreRet ret players[i].dailyUpdate(difficulty, adjustedDifficulty); if (ret.isBack) { money ret.addMoney; jindu min(100, jindu ret.addJindu); } } return; } // 打印全部角色状态、背包、金币 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 i 1 . items[i].name x backpack[i] 效果 items[i].introduce endl; } cout \n金币 money endl; return; } // 玩家每日行动选择菜单 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); } return; } // 给存活在家的角色分配使用物品 void distributeFood() { clearScreen(); cout 【分配食物】 endl; for (int i 0; i players.size(); i) { if (!players[i].getIsAlive() || players[i].getIsOut()) continue; while (true) { cout 给 players[i].getName() 分配物品(1-是 2-否); int choice _getch() - 0; if (choice ! 1) { break; } cout \n选择物品 endl; bool hasItem false; for (int j 1; j items.size(); j) { if (backpack[j] 0) { cout j 1 . items[j].name x backpack[j] endl; hasItem true; } } if (!hasItem) { cout 没有可用物品 endl; delay(1000); break; } cout 请选择; char input _getch(); int sel input - 0; if (sel 2 || sel (int)items.size()) { cout 无效选择 endl; delay(1000); continue; } int itemChoice sel - 1; if (itemChoice 0 itemChoice (int)items.size() backpack[itemChoice] 0) { players[i].useItem(items[itemChoice]); backpack[itemChoice]--; cout 使用成功 endl; } else { cout 物品不足 endl; delay(1000); continue; } delay(1000); break; } clearScreen(); } return; } // 派遣角色外出探索 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]; bool ok players[idx].goOut(rand() % 3 1); if (ok) { cout \n players[idx].getName() 已外出探索 endl; } else { cout \n players[idx].getName() 疲劳过高无法外出(疲劳需要30) endl; } delay(1000); return; } // 商店买卖物品 void showShop() { clearScreen(); cout 【商店】 当前金币 money endl; for (int i 0; i items.size(); i) { cout i 1 . items[i].name 买入 items[i].buyPrice 卖出 items[i].sellPrice 持有 backpack[i] 效果 items[i].introduce endl; } cout 选择物品; int input _getch(); if (input 1 || input 0 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; cin.ignore(); if(num 0) { cout 数量必须大于0 endl; }else{ 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; cin.ignore(); if(num 0) { cout 数量必须大于0 endl; }else if (num backpack[choice]) { money num * items[choice].sellPrice; backpack[choice] - num; cout 卖出成功 endl; } else { cout 物品不足 endl; } } delay(1000); return; } // 游戏结束条件判断 bool checkGameOver(bool isWin) { int aliveCount 0; for (int i 0; i players.size(); i) { if (players[i].getIsAlive()) aliveCount; } // 全部角色死亡游戏失败 if (aliveCount 0) { isWin false; return true; } // 普通生存模式存活足够天数胜利 if (difficulty ! 0 day difficulty * 30 currentMod 1) { isWin true; return true; } // 挑战模式岛屿调查逻辑 if (currentMod 2) { if(jindu 100) { if(day 50) { isWin true; return true; } }else{ if(day 50) { isWin false; return true; } } } return false; } // 游戏结束界面胜利/失败展示 void gameOver(bool b) { clearScreen(); currentMod 0; if (!b) { cout 游戏结束 endl; cout 你在荒岛生存了 day 天 endl; cout 但还是没能逃出这里…… endl; cout 最终金币 money endl; cout 按任意键重启... endl; _getch(); return; } else { cout 游戏结束 endl; cout 你在荒岛生存了 day 天 endl; if (currentMod 1) { cout 等来了救援 endl; if (difficulty 1) money 10000; else if (difficulty 2) money 20000; else money 30000; cout 最终金币 money 通关奖励已发放 endl; } else { money 50000; cout 你完成了对岛屿的调查 endl; cout 最终金币 money 挑战奖励40000金币 endl; } cout 按任意键重启... endl; _getch(); return; } } // 保存背包物品数据 void savegame(int slot) { string filename save to_string(slot) _items.dat; ofstream fout(filename, ios::out); if (!fout) return; fout backpack.size() endl; for (int i 0; i backpack.size(); i) { fout backpack[i] endl; } fout.close(); cryptFile(filename); return; } // 完整存档信息、角色、背包全部写入加密文件 void saveGame(int slot) { saveUser(slot); string infoFile save to_string(slot) _info.dat; ofstream fout(infoFile, ios::out); if (!fout.is_open()) { cout 保存失败无法创建存档文件。 endl; delay(1000); return; } fout day endl; fout difficulty endl; fout jindu endl; fout money endl; fout players.size() endl; for (int i 0; i players.size(); i) { 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; fout players[i].getIsAlive() endl; fout players[i].getIsOut() endl; fout players[i].getOutDays() endl; } fout currentMod endl; fout zd_max endl; fout zd endl; fout wz endl; fout.close(); cryptFile(infoFile); savegame(slot); cout 游戏已保存到存档位 slot 已加密 endl; delay(1000); return; } // 读取完整存档 bool loadGame(int slot) { vectorstring nameList; if (!loadUser(slot, nameList)) { cout user slot .dat读取失败 endl; delay(1000); return false; } string infoFile save to_string(slot) _info.dat; cryptFile(infoFile); ifstream fin(infoFile, ios::in); if (!fin.is_open()) { cout 加载失败存档文件不存在。 endl; delay(1000); cryptFile(infoFile); return false; } players.clear(); fin day; fin difficulty; fin jindu; fin money; int playerCount; fin playerCount; fin.ignore(); for (int i 0; i playerCount; i) { Player p(nameList[i]); 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 currentMod; fin zd_max; fin zd; fin wz; fin.close(); cryptFile(infoFile); string itemFile save to_string(slot) _items.dat; cryptFile(itemFile); ifstream itemFin(itemFile); if (!itemFin) { cout 物品文件损坏 endl; delay(1000); cryptFile(itemFile); return false; } int bpSize; itemFin bpSize; backpack.resize(bpSize); for (int i 0; i bpSize; i) { itemFin backpack[i]; } itemFin.close(); cryptFile(itemFile); cout 存档 slot 加载成功 endl; delay(1000); return true; } // 存档管理子菜单 void saveMenu(int path) { while (1) { 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 8. 返回 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)) { path currentMod; return; } break; } case 7: clearScreen(); cout 几天存一次 endl; cin zd_max; cin.ignore(); if (zd_max 0) { cout 间隔天数必须大于0 endl; delay(1200); break; } A: cout 存到哪1-3 endl; cin wz; cin.ignore(); if (wz 1 || wz 3) { cout 非法输入 endl; goto A; } cout 自动存档设置成功将每 zd_max 天存档到位置 wz endl; delay(1500); break; case 8: return; break; default: cout 无效选择 endl; delay(1000); break; } } } }; int main() { Game game; game.start(); return 0; }
返回列表