荒岛生存(地图版)
2026-07-26 15:27:29
发布于:浙江
#include<iostream>
#include<conio.h>
#include<windows.h>
#include<cstdlib>
#include<ctime>
#include<vector>
#include<string>
using namespace std;
// ============================================================
// 设置中文编码
// ============================================================
void setChinese() {
SetConsoleOutputCP(936);
SetConsoleCP(936);
}
// ============================================================
// 地图大小
// ============================================================
const int MAP_W = 50;
const int MAP_H = 16;
const int WORLD_W = 200;
const int WORLD_H = 18;
// ============================================================
// 丧尸结构体
// ============================================================
struct Zombie {
int x, y;
int health;
int damage;
bool alive;
};
vector<Zombie> zombies;
// ============================================================
// 工具系统
// ============================================================
struct Tool {
string name;
int woodCost;
int stoneCost;
int metalCost;
int damage;
int harvest;
bool owned;
};
Tool toolsList[8] = {
{"拳头", 0, 0, 0, 2, 1, true},
{"木棍", 2, 0, 0, 5, 1, false},
{"石斧", 2, 2, 0, 10, 3, false},
{"石镐", 2, 3, 0, 8, 4, false},
{"鱼竿", 3, 1, 0, 0, 0, false},
{"弓箭", 3, 2, 1, 15, 0, false},
{"铁剑", 2, 3, 3, 20, 0, false},
{"铁斧", 2, 3, 3, 18, 5, false}
};
int metal = 0;
// ============================================================
// 游戏变量
// ============================================================
int playerX = 10;
int playerY = 14;
int health = 100;
int hunger = 80;
int thirst = 80;
int energy = 100;
int day = 1;
int wood = 0;
int stone = 0;
int food = 5;
int water = 5;
int shelter = 0;
int score = 0;
int weapon = 0;
bool gameOver = false;
bool isDay = true;
int cameraX = 0;
bool isInWater = false;
int killCount = 0;
bool isNearShelter = false;
const int SHELTER_X = 10;
const int SHELTER_Y = 14;
char worldMap[WORLD_H][WORLD_W];
// ============================================================
// 公用函数
// ============================================================
void gotoxy(int x, int y) {
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void hideCursor() {
CONSOLE_CURSOR_INFO cursorInfo;
GetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursorInfo);
cursorInfo.bVisible = false;
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursorInfo);
}
void setColor(int color) {
char cmd[10];
sprintf(cmd, "color %02X", color);
system(cmd);
}
void clearScreen() {
system("cls");
}
// ============================================================
// 生成地图
// ============================================================
void generateMap() {
// 初始化所有格子为空格
for (int y = 0; y < WORLD_H; y++) {
for (int x = 0; x < WORLD_W; x++) {
worldMap[y][x] = ' ';
}
}
// 地面 (y=15)
for (int x = 0; x < WORLD_W; x++) {
worldMap[15][x] = '=';
}
// 随机高地
for (int x = 5; x < WORLD_W - 5; x++) {
if (rand() % 20 == 0) {
int height = 1 + rand() % 3;
for (int h = 0; h < height; h++) {
if (15 - h > 0) {
worldMap[15 - h][x] = '#';
}
}
}
}
// 坑洞
for (int x = 20; x < WORLD_W - 20; x += 15 + rand() % 10) {
int width = 2 + rand() % 3;
for (int w = 0; w < width; w++) {
if (x + w < WORLD_W) {
worldMap[15][x + w] = ' ';
}
}
x += width;
}
// 树木 (T)
for (int i = 0; i < 35; i++) {
int x = 5 + rand() % (WORLD_W - 10);
int y = 12 + rand() % 3;
if (worldMap[y][x] == ' ') {
worldMap[y][x] = 'T';
if (y > 0) worldMap[y-1][x] = '^';
}
}
// 石头 (S)
for (int i = 0; i < 20; i++) {
int x = 5 + rand() % (WORLD_W - 10);
if (worldMap[14][x] == ' ') {
worldMap[14][x] = 'S';
}
}
// 水源 (W)
for (int i = 0; i < 6; i++) {
int x = 10 + rand() % (WORLD_W - 20);
if (worldMap[14][x] == ' ') {
worldMap[14][x] = 'W';
if (x + 1 < WORLD_W && worldMap[14][x+1] == ' ') {
worldMap[14][x+1] = 'W';
}
}
}
// 草地 (.)
for (int i = 0; i < 50; i++) {
int x = 3 + rand() % (WORLD_W - 6);
if (worldMap[14][x] == ' ') {
worldMap[14][x] = '.';
}
}
// 金属矿 (M)
for (int i = 0; i < 6; i++) {
int x = 15 + rand() % (WORLD_W - 30);
if (worldMap[14][x] == ' ') {
worldMap[14][x] = 'M';
}
}
// 庇护所 (H)
worldMap[SHELTER_Y][SHELTER_X] = 'H';
if (SHELTER_Y > 0) worldMap[SHELTER_Y-1][SHELTER_X] = '^';
// 终点 (F)
worldMap[14][WORLD_W - 5] = 'F';
worldMap[13][WORLD_W - 5] = '^';
}
// ============================================================
// 查找安全地面
// ============================================================
void findSafeGround() {
for (int offset = 0; offset < 20; offset++) {
for (int dx = 0; dx <= offset; dx++) {
int checkX = playerX + dx;
if (checkX < WORLD_W && worldMap[14][checkX] == '=') {
playerX = checkX;
playerY = 14;
isInWater = false;
return;
}
}
for (int dx = 0; dx <= offset; dx++) {
int checkX = playerX - dx;
if (checkX >= 0 && worldMap[14][checkX] == '=') {
playerX = checkX;
playerY = 14;
isInWater = false;
return;
}
}
}
playerX = 10;
playerY = 14;
isInWater = false;
}
// ============================================================
// 检查是否在庇护所附近
// ============================================================
void checkShelter() {
int dx = abs(playerX - SHELTER_X);
int dy = abs(playerY - SHELTER_Y);
isNearShelter = (dx <= 2 && dy <= 2);
}
// ============================================================
// 获取武器信息
// ============================================================
string getWeaponName() {
if (weapon < 0 || weapon >= 8) return "拳头";
if (!toolsList[weapon].owned) return "拳头";
return toolsList[weapon].name;
}
int getWeaponDamage() {
if (weapon < 0 || weapon >= 8) return 2;
if (!toolsList[weapon].owned) return 2;
return toolsList[weapon].damage;
}
int getHarvestBonus() {
if (weapon < 0 || weapon >= 8) return 1;
if (!toolsList[weapon].owned) return 1;
if (toolsList[weapon].harvest > 0) return toolsList[weapon].harvest;
return 1;
}
// ============================================================
// 获取地形
// ============================================================
char getTile(int x, int y) {
if (x < 0 || x >= WORLD_W || y < 0 || y >= WORLD_H) {
return ' ';
}
return worldMap[y][x];
}
// ============================================================
// 显示地图
// ============================================================
void showMap() {
cameraX = playerX - MAP_W / 2;
if (cameraX < 0) cameraX = 0;
if (cameraX > WORLD_W - MAP_W) cameraX = WORLD_W - MAP_W;
gotoxy(0, 0);
cout << "============================================================\n";
cout << " 第" << day << "天 ";
if (isDay) cout << "[白天]";
else cout << "[夜晚]";
cout << " 生命:" << health << " 丧尸:" << zombies.size() << " 击杀:" << killCount << "\n";
cout << " 武器:" << getWeaponName() << " 庇护所:" << shelter << "级";
if (isNearShelter) cout << " [在家]";
cout << " 得分:" << score << "\n";
cout << "============================================================\n";
for (int y = 0; y < MAP_H && y < WORLD_H; y++) {
cout << "|";
for (int x = cameraX; x < cameraX + MAP_W && x < WORLD_W; x++) {
bool drawn = false;
for (int i = 0; i < (int)zombies.size(); i++) {
if (zombies[i].alive && zombies[i].x == x && zombies[i].y == y) {
cout << "Z";
drawn = true;
break;
}
}
if (!drawn) {
if (x == playerX && y == playerY) {
cout << "P";
} else {
char tile = worldMap[y][x];
if (tile == ' ') cout << " ";
else cout << tile;
}
}
}
cout << "|\n";
}
cout << "============================================================\n";
cout << " 木材[" << wood << "] 石头[" << stone << "] 金属[" << metal << "] 食物[" << food << "] 水[" << water << "]\n";
cout << " 工具:";
bool hasTool = false;
for (int i = 1; i < 8; i++) {
if (toolsList[i].owned) {
cout << toolsList[i].name << " ";
hasTool = true;
}
}
if (!hasTool) cout << "无";
cout << " 庇护所:" << shelter << "级 体力:" << energy << "\n";
cout << " 庇护所位置: x=" << SHELTER_X;
if (isNearShelter) cout << " [在家休息效果翻倍]";
else cout << " [按A向左走回家]";
cout << "\n";
cout << " 图例: P=你 Z=丧尸 H=庇护所 F=终点 T=树 S=石 M=金属 W=水 .=草地\n";
cout << " 按键: A/D移动 W跳跃 空格采集 J攻击 F换武器 B建造 Q菜单\n";
cout << " E吃 C喝 H狩猎 R休息\n";
cout << "============================================================\n";
}
// ============================================================
// 采集
// ============================================================
void collect() {
if (isInWater) {
cout << "\n [X] 在水里无法采集!\n";
Sleep(300);
return;
}
char tile = getTile(playerX, playerY);
if (energy < 15) {
cout << "\n [X] 体力不足!需要15点体力\n";
Sleep(300);
return;
}
energy -= 15;
int bonus = getHarvestBonus();
if (tile == 'T') {
int amount = (1 + rand() % 3) * bonus;
wood += amount;
cout << "\n [树木] 砍树获得 " << amount << " 木材!\n";
score += 5 * bonus;
worldMap[playerY][playerX] = ' ';
if (playerY > 0) worldMap[playerY-1][playerX] = ' ';
} else if (tile == 'S') {
int amount = (1 + rand() % 2) * bonus;
stone += amount;
cout << "\n [石头] 采集获得 " << amount << " 石头!\n";
score += 5 * bonus;
worldMap[playerY][playerX] = ' ';
} else if (tile == 'W') {
int amount = 2 + rand() % 3;
water += amount;
cout << "\n [水源] 收集到 " << amount << " 清水!\n";
score += 8;
} else if (tile == 'M') {
int amount = (1 + rand() % 2) * (bonus > 1 ? 2 : 1);
metal += amount;
cout << "\n [金属] 挖矿获得 " << amount << " 金属!\n";
score += 10 * bonus;
worldMap[playerY][playerX] = ' ';
} else if (tile == '.') {
if (rand() % 10 < 3) {
food += 1;
cout << "\n [草地] 找到野果!食物+1\n";
score += 3;
worldMap[playerY][playerX] = ' ';
} else {
cout << "\n [草地] 搜索了半天,什么都没有...\n";
}
} else if (tile == 'H') {
cout << "\n [庇护所] 这是你的家!在这里休息效果加倍!\n";
} else if (tile == 'F') {
cout << "\n [终点] 恭喜到达终点!\n";
score += 50;
gameOver = true;
} else {
cout << "\n [X] 这里没有可采集的资源!\n";
}
Sleep(300);
}
// ============================================================
// 攻击丧尸
// ============================================================
void attackZombie() {
if (isInWater) {
cout << "\n [X] 在水里无法攻击!\n";
Sleep(300);
return;
}
if (energy < 15) {
cout << "\n [X] 体力不足!需要15点体力\n";
Sleep(300);
return;
}
int targetIndex = -1;
for (int i = 0; i < (int)zombies.size(); i++) {
if (!zombies[i].alive) continue;
int dx = abs(zombies[i].x - playerX);
int dy = abs(zombies[i].y - playerY);
if (dx <= 2 && dy <= 1) {
targetIndex = i;
break;
}
}
if (targetIndex == -1) {
cout << "\n [X] 附近没有丧尸!需要靠近丧尸再攻击\n";
Sleep(300);
return;
}
energy -= 15;
int damage = getWeaponDamage();
zombies[targetIndex].health -= damage;
cout << "\n [攻击] 用" << getWeaponName() << "攻击丧尸!造成" << damage << "点伤害\n";
cout << " 丧尸剩余生命: " << zombies[targetIndex].health << "\n";
if (zombies[targetIndex].health <= 0) {
zombies[targetIndex].alive = false;
killCount++;
score += 20;
food += 1 + rand() % 2;
cout << " [!!] 丧尸死亡!获得食物!\n";
setColor(0x0E);
Sleep(100);
setColor(0x0A);
}
Sleep(300);
}
// ============================================================
// 生成丧尸
// ============================================================
void spawnZombie() {
if (zombies.size() >= 5) return;
int x, y;
int attempts = 0;
do {
x = 10 + rand() % (WORLD_W - 20);
y = 14;
attempts++;
if (attempts > 50) return;
} while (worldMap[y][x] != '=' && worldMap[y][x] != '.' && worldMap[y][x] != ' ');
if (abs(x - playerX) < 8) return;
if (abs(x - SHELTER_X) < 5) return;
Zombie z;
z.x = x;
z.y = 14;
z.health = 15 + day * 2;
z.damage = 5 + day / 3;
z.alive = true;
zombies.push_back(z);
}
// ============================================================
// 更新丧尸AI
// ============================================================
void updateZombies() {
for (int i = 0; i < (int)zombies.size(); i++) {
if (!zombies[i].alive) continue;
Zombie& z = zombies[i];
int dx = playerX - z.x;
int dy = playerY - z.y;
// 向玩家移动
if (abs(dx) > abs(dy)) {
if (dx > 0 && worldMap[z.y][z.x + 1] != ' ') z.x++;
else if (dx < 0 && worldMap[z.y][z.x - 1] != ' ') z.x--;
}
// 攻击玩家
if (abs(z.x - playerX) <= 1 && abs(z.y - playerY) <= 1) {
health -= z.damage;
cout << "\n [!!] 丧尸攻击你!生命-" << z.damage << "!\n";
setColor(0x0C);
Sleep(100);
setColor(0x0A);
if (health <= 0) {
health = 0;
gameOver = true;
}
}
// 掉坑
if (worldMap[z.y + 1][z.x] == ' ') {
z.alive = false;
cout << "\n 丧尸掉进坑里消失了!\n";
}
}
}
// ============================================================
// 狩猎
// ============================================================
void hunt() {
if (isInWater) {
cout << "\n [X] 在水里无法狩猎!\n";
Sleep(300);
return;
}
if (energy < 30) {
cout << "\n [X] 体力不足!需要30点体力\n";
Sleep(300);
return;
}
energy -= 30;
int success = rand() % 100;
cout << "\n [狩猎] 正在狩猎...\n";
Sleep(500);
if (success < 40) {
cout << " [X] 狩猎失败,什么也没抓到。\n";
} else if (success < 70) {
int gain = 2 + rand() % 3;
food += gain;
cout << " [OK] 狩猎成功!获得 " << gain << " 食物!\n";
score += 10;
} else {
int gain = 4 + rand() % 4;
food += gain;
cout << " [!!] 大丰收!获得 " << gain << " 食物!\n";
score += 20;
}
Sleep(300);
}
// ============================================================
// 休息
// ============================================================
void rest() {
if (isInWater) {
cout << "\n [X] 在水里无法休息!\n";
Sleep(300);
return;
}
int restore = 20 + shelter * 5;
if (isNearShelter) {
restore = restore * 1.5;
cout << "\n [庇护所] 在家里休息,效果更好!\n";
}
energy += restore;
if (energy > 100) energy = 100;
cout << "\n [休息] 体力恢复 " << restore << " 点\n";
Sleep(300);
}
// ============================================================
// 吃东西
// ============================================================
void eat() {
if (food <= 0) {
cout << "\n [X] 没有食物了!去狩猎或采集吧\n";
Sleep(300);
return;
}
food--;
hunger += 25;
if (hunger > 100) hunger = 100;
cout << "\n [进食] 饥饿恢复25点 (剩余食物:" << food << ")\n";
Sleep(300);
}
// ============================================================
// 喝水
// ============================================================
void drink() {
if (water <= 0) {
cout << "\n [X] 没有清水了!去水源收集吧\n";
Sleep(300);
return;
}
water--;
thirst += 25;
if (thirst > 100) thirst = 100;
cout << "\n [喝水] 口渴恢复25点 (剩余清水:" << water << ")\n";
Sleep(300);
}
// ============================================================
// 建造工具菜单
// ============================================================
void buildToolMenu() {
clearScreen();
cout << "============================================================\n";
cout << " 建 造 工 具 \n";
cout << "============================================================\n";
cout << "\n";
cout << " 当前资源: 木材[" << wood << "] 石头[" << stone << "] 金属[" << metal << "]\n";
cout << "\n";
cout << " 1. 木棍 (木材2) 攻击+5\n";
cout << " 2. 石斧 (木材2+石头2) 攻击+10 采集+3\n";
cout << " 3. 石镐 (木材2+石头3) 攻击+8 采集+4\n";
cout << " 4. 鱼竿 (木材3+石头1) 可钓鱼\n";
cout << " 5. 弓箭 (木材3+石头2+金属1) 攻击+15\n";
cout << " 6. 铁剑 (木材2+石头3+金属3) 攻击+20\n";
cout << " 7. 铁斧 (木材2+石头3+金属3) 攻击+18 采集+5\n";
cout << "\n";
cout << " 0. 返回\n";
cout << "============================================================\n";
cout << " 请选择 (0-7): ";
}
void buildTool(int choice) {
if (choice < 1 || choice > 7) return;
choice--;
Tool& t = toolsList[choice + 1];
if (t.owned) {
cout << "\n [X] 已经拥有" << t.name << "了!\n";
Sleep(500);
return;
}
if (wood < t.woodCost || stone < t.stoneCost || metal < t.metalCost) {
cout << "\n [X] 资源不足!\n";
cout << " 需要: 木材" << t.woodCost << " 石头" << t.stoneCost << " 金属" << t.metalCost << "\n";
Sleep(500);
return;
}
wood -= t.woodCost;
stone -= t.stoneCost;
metal -= t.metalCost;
t.owned = true;
score += 10;
cout << "\n [OK] 成功建造了" << t.name << "!\n";
Sleep(500);
}
// ============================================================
// 切换武器
// ============================================================
void switchWeapon() {
int oldWeapon = weapon;
do {
weapon = (weapon + 1) % 8;
} while (!toolsList[weapon].owned && weapon != oldWeapon);
if (toolsList[weapon].owned && weapon != 0) {
cout << "\n [切换] 当前武器: " << getWeaponName() << "\n";
} else {
weapon = oldWeapon;
cout << "\n [X] 没有其他武器了\n";
}
Sleep(300);
}
// ============================================================
// 主菜单
// ============================================================
void showFullMenu() {
clearScreen();
cout << "============================================================\n";
cout << " 荒 岛 生 存 \n";
cout << "============================================================\n";
cout << "\n";
cout << " 1. 返回地图探索\n";
cout << " 2. 建造工具\n";
cout << " 3. 升级庇护所 (木材5+石头3)\n";
cout << " 4. 查看详细状态\n";
cout << " 5. 查看游戏指南\n";
cout << " 6. 退出游戏\n";
cout << "\n";
cout << "============================================================\n";
cout << " 请选择 (1-6): ";
}
// ============================================================
// 升级庇护所
// ============================================================
void upgradeShelter() {
if (isInWater) {
cout << "\n [X] 在水里无法升级!\n";
Sleep(500);
return;
}
int costWood = 5 + shelter * 2;
int costStone = 3 + shelter * 2;
if (wood < costWood || stone < costStone) {
cout << "\n [X] 资源不足!\n";
cout << " 需要: 木材" << costWood << " 石头" << costStone << "\n";
Sleep(500);
return;
}
wood -= costWood;
stone -= costStone;
shelter++;
cout << "\n [庇护所] 升级到 " << shelter << " 级!\n";
cout << " 效果: 每日消耗-" << shelter << " 休息恢复+" << shelter*5 << "\n";
score += 25;
Sleep(500);
}
// ============================================================
// 显示状态
// ============================================================
void showStatus() {
clearScreen();
cout << "============================================================\n";
cout << " 详 细 状 态 \n";
cout << "============================================================\n";
cout << "\n";
cout << " 生存天数: " << day << " 天\n";
cout << " 生命值: " << health << "/100\n";
cout << " 饥饿值: " << hunger << "/100\n";
cout << " 口渴值: " << thirst << "/100\n";
cout << " 体力值: " << energy << "/100\n";
cout << "\n";
cout << " 木材: " << wood << " 石头: " << stone << " 金属: " << metal << "\n";
cout << " 食物: " << food << " 清水: " << water << "\n";
cout << " 庇护所: " << shelter << " 级\n";
cout << " 击杀丧尸: " << killCount << " 只\n";
cout << " 当前武器: " << getWeaponName() << "\n";
cout << " 得分: " << score << "\n";
cout << "\n";
cout << " 拥有的工具: ";
bool hasTool = false;
for (int i = 1; i < 8; i++) {
if (toolsList[i].owned) {
cout << toolsList[i].name << " ";
hasTool = true;
}
}
if (!hasTool) cout << "无";
cout << "\n";
cout << "============================================================\n";
system("pause");
}
// ============================================================
// 指南
// ============================================================
void showGuide() {
clearScreen();
cout << "============================================================\n";
cout << " 游 戏 指 南 \n";
cout << "============================================================\n";
cout << "\n";
cout << " 【地图符号】\n";
cout << " P=你 Z=丧尸 H=庇护所 F=终点\n";
cout << " T=树木 S=石头 M=金属 W=水源 .=草地 ==地面\n";
cout << "\n";
cout << " 【按键操作】\n";
cout << " A/D=移动 W=跳跃 空格=采集\n";
cout << " J=攻击丧尸 F=切换武器 B=建造工具\n";
cout << " E=吃东西 C=喝水 H=狩猎 R=休息 Q=菜单\n";
cout << "\n";
cout << " 【生存技巧】\n";
cout << " 1. 采集木材和石头,建造工具\n";
cout << " 2. 在庇护所附近休息恢复更多\n";
cout << " 3. 遇到丧尸用J键攻击\n";
cout << " 4. 到达右边终点(F)通关\n";
cout << "============================================================\n";
system("pause");
}
// ============================================================
// 每天结束
// ============================================================
void endDay() {
cout << "\n============================================================\n";
cout << " 第 " << day << " 天 结 束 \n";
cout << "============================================================\n";
int hungerLoss = 8 - shelter * 1;
int thirstLoss = 8 - shelter * 1;
if (hungerLoss < 3) hungerLoss = 3;
if (thirstLoss < 3) thirstLoss = 3;
hunger -= hungerLoss;
thirst -= thirstLoss;
cout << " 消耗 " << hungerLoss << " 点饥饿\n";
cout << " 消耗 " << thirstLoss << " 点口渴\n";
if (hunger <= 0) {
health -= 10;
hunger = 0;
cout << " [!!] 饥饿过度!生命-10\n";
}
if (thirst <= 0) {
health -= 10;
thirst = 0;
cout << " [!!] 口渴过度!生命-10\n";
}
int restAmount = 15 + shelter * 5;
energy += restAmount;
if (energy > 100) energy = 100;
cout << " 夜间休息,体力恢复 " << restAmount << " 点\n";
if (rand() % 3 == 0) {
spawnZombie();
cout << " [!!] 夜晚有丧尸出现了!\n";
}
if (health <= 0) {
health = 0;
gameOver = true;
cout << "\n [XX] 你没能活过第 " << day << " 天...\n";
}
day++;
score += 10;
cout << "============================================================\n";
system("pause");
}
// ============================================================
// 检查状态
// ============================================================
void checkSurvival() {
if (hunger <= 0) cout << " [!!]饥饿归零!";
if (thirst <= 0) cout << " [!!]口渴归零!";
if (health <= 20) cout << " [!!]生命危险!";
if (energy <= 20) cout << " [!!]体力不足!";
}
// ============================================================
// 结局
// ============================================================
void showEnding() {
clearScreen();
cout << "============================================================\n";
cout << " 游 戏 结 束 \n";
cout << "============================================================\n";
if (playerX >= WORLD_W - 5 && getTile(playerX, playerY) == 'F') {
cout << "\n [!!] 恭喜到达终点!\n";
cout << " 你成功穿越了荒岛!\n";
} else if (day >= 31) {
cout << "\n [!!] 恭喜生存30天!\n";
cout << " 救援队发现了你!\n";
} else if (health <= 0) {
cout << "\n [XX] 你没能活过第 " << day << " 天...\n";
} else {
cout << "\n 游戏提前结束。\n";
}
cout << "\n============================================================\n";
cout << " 最终统计:\n";
cout << " 生存天数: " << (day - 1) << " 天\n";
cout << " 最终得分: " << score << "\n";
cout << " 击杀丧尸: " << killCount << " 只\n";
cout << " 庇护所等级: " << shelter << " 级\n";
cout << "============================================================\n";
system("pause");
}
// ============================================================
// 移动
// ============================================================
void movePlayer(int dx, int dy) {
if (isInWater) return;
int newX = playerX + dx;
int newY = playerY + dy;
if (newX < 0 || newX >= WORLD_W || newY < 0 || newY >= WORLD_H) {
return;
}
char tile = getTile(newX, newY);
if (tile == ' ' || tile == '.' || tile == 'T' || tile == 'S' ||
tile == 'W' || tile == '^' || tile == 'M' || tile == 'F' || tile == 'H') {
playerX = newX;
playerY = newY;
return;
}
if (tile == '=' || tile == '#') {
if (dy == 0 && getTile(newX, playerY - 1) == ' ') {
playerX = newX;
playerY = playerY - 1;
return;
}
}
}
// ============================================================
// 重力
// ============================================================
void applyGravity() {
if (isInWater) {
findSafeGround();
return;
}
char below = getTile(playerX, playerY + 1);
if (below == ' ' || below == '.' || below == '^') {
playerY++;
if (playerY >= WORLD_H - 1) {
playerY = WORLD_H - 1;
isInWater = true;
health -= 5;
cout << "\n [!!] 掉进海里!生命-5,传送中...\n";
Sleep(500);
findSafeGround();
return;
}
}
}
// ============================================================
// 主循环
// ============================================================
void gameLoop() {
bool inMap = true;
bool menuOpen = false;
int moveCounter = 0;
while (!gameOver && day <= 30) {
if (inMap) {
applyGravity();
checkShelter();
showMap();
checkSurvival();
if (_kbhit()) {
char key = _getch();
if (key == 224) {
key = _getch();
if (key == 75) movePlayer(-1, 0);
else if (key == 77) movePlayer(1, 0);
else if (key == 72) {
if (!isInWater && (getTile(playerX, playerY + 1) == '=' ||
getTile(playerX, playerY + 1) == '#')) {
playerY -= 2;
}
}
} else {
if (key == 'a' || key == 'A') movePlayer(-1, 0);
else if (key == 'd' || key == 'D') movePlayer(1, 0);
else if (key == 'w' || key == 'W') {
if (!isInWater && (getTile(playerX, playerY + 1) == '=' ||
getTile(playerX, playerY + 1) == '#')) {
playerY -= 2;
}
}
else if (key == ' ') collect();
else if (key == 'h' || key == 'H') hunt();
else if (key == 'r' || key == 'R') rest();
else if (key == 'e' || key == 'E') eat();
else if (key == 'c' || key == 'C') drink();
else if (key == 'j' || key == 'J') attackZombie();
else if (key == 'f' || key == 'F') switchWeapon();
else if (key == 'b' || key == 'B') {
buildToolMenu();
char choice = _getch();
if (choice >= '0' && choice <= '7') {
buildTool(choice - '0');
}
}
else if (key == 'q' || key == 'Q') {
inMap = false;
menuOpen = true;
}
}
}
updateZombies();
for (int i = (int)zombies.size() - 1; i >= 0; i--) {
if (!zombies[i].alive) {
zombies.erase(zombies.begin() + i);
}
}
if (getTile(playerX, playerY) == 'F') {
gameOver = true;
break;
}
if (health <= 0) {
gameOver = true;
break;
}
moveCounter++;
if (moveCounter > 25) {
moveCounter = 0;
isDay = false;
endDay();
isDay = true;
}
} else if (menuOpen) {
showFullMenu();
char choice = _getch();
if (choice == '1') {
inMap = true;
} else if (choice == '2') {
buildToolMenu();
char c = _getch();
if (c >= '0' && c <= '7') {
buildTool(c - '0');
}
} else if (choice == '3') {
upgradeShelter();
} else if (choice == '4') {
showStatus();
} else if (choice == '5') {
showGuide();
} else if (choice == '6') {
cout << "\n 确定退出?(Y/N): ";
char confirm = _getch();
if (confirm == 'y' || confirm == 'Y') {
gameOver = true;
break;
}
}
}
Sleep(50);
}
}
// ============================================================
// 主函数
// ============================================================
int main() {
setChinese();
hideCursor();
setColor(0x0A);
srand((unsigned)time(0));
clearScreen();
cout << "============================================================\n";
cout << " 荒 岛 生 存 \n";
cout << "============================================================\n";
cout << "\n";
cout << " 你被困在荒岛上,这里充满了丧尸!\n";
cout << " 采集资源,建造工具,生存下去!\n";
cout << "\n";
cout << " 【按键说明】\n";
cout << " A/D=移动 W=跳跃 空格=采集\n";
cout << " J=攻击丧尸 F=切换武器 B=建造工具\n";
cout << " E=吃东西 C=喝水 H=狩猎 R=休息 Q=菜单\n";
cout << "\n";
cout << " 目标: 到达右边终点(F) 或 生存30天\n";
cout << "\n";
cout << " 按任意键开始...\n";
_getch();
generateMap();
playerX = 10;
playerY = 14;
isInWater = false;
gameLoop();
showEnding();
return 0;
}
这里空空如也




















有帮助,赞一个