
逃出惊魂夜
禁盗!禁盗!禁盗!禁盗!禁盗!
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <windows.h>
using namespace std;
// 游戏配置
const int MAP_W = 30;
const int MAP_H = 20;
const int MACHINE_COUNT = 5;
const int REPAIR_THRESHOLD = 3; // 修好3台即可开门
struct Point {
int x, y;
};
class Game {
private:
Point player;
Point hunter;
vector<Point> machines;
vector<bool> repaired;
Point exitGate;
bool gateOpen;
bool isRunning;
public:
Game() : gateOpen(false), isRunning(true) {
srand(time(0));
player = {2, 2};
hunter = {MAP_W - 3, MAP_H - 3};
exitGate = {MAP_W / 2, MAP_H - 2};
// 生成机器位置
for (int i = 0; i < MACHINE_COUNT; ++i) {
Point m;
do {
m.x = rand() % (MAP_W - 4) + 2;
m.y = rand() % (MAP_H - 4) + 2;
} while (isOccupied(m));
machines.push_back(m);
repaired.push_back(false);
}
}
bool isOccupied(Point p) {
if (p.x == player.x && p.y == player.y) return true;
if (p.x == hunter.x && p.y == hunter.y) return true;
if (p.x == exitGate.x && p.y == exitGate.y) return true;
for (auto& m : machines) {
if (m.x == p.x && m.y == p.y) return true;
}
return false;
}
void draw() {
system("cls");
cout << "=== 逃出惊魂夜 ===" << endl;
cout << "WASD移动, E交互(修机/开门)" << endl;
for (int y = 0; y < MAP_H; ++y) {
for (int x = 0; x < MAP_W; ++x) {
if (x == player.x && y == player.y) cout << "\033[32mP\033[0m";
else if (x == hunter.x && y == hunter.y) cout << "\033[31mH\033[0m";
else if (x == exitGate.x && y == exitGate.y) {
if (gateOpen) cout << "\033[33mE\033[0m";
else cout << "#";
} else {
bool isMach = false;
for (int i = 0; i < MACHINE_COUNT; ++i) {
if (machines[i].x == x && machines[i].y == y) {
isMach = true;
if (repaired[i]) cout << "O";
else cout << "\033[36mX\033[0m";
break;
}
}
if (!isMach) cout << ".";
}
}
cout << endl;
}
int count = 0;
for (bool b : repaired) if (b) count++;
cout << "进度: " << count << "/" << REPAIR_THRESHOLD << endl;
if (gateOpen) cout << "大门已开! 快去E点逃生!" << endl;
}
void update(char key) {
if (!isRunning) return;
Point next = player;
if (key == 'w' || key == 'W') next.y--;
if (key == 's' || key == 'S') next.y++;
if (key == 'a' || key == 'A') next.x--;
if (key == 'd' || key == 'D') next.x++;
// 边界检查
if (next.x >= 0 && next.x < MAP_W && next.y >= 0 && next.y < MAP_H) {
if (key == 'e' || key == 'E') {
// 交互逻辑
bool interacted = false;
// 修机
for (int i = 0; i < MACHINE_COUNT; ++i) {
if (abs(player.x - machines[i].x) <= 1 && abs(player.y - machines[i].y) <= 1) {
if (!repaired[i]) {
repaired[i] = true;
interacted = true;
}
}
}
// 开门
if (abs(player.x - exitGate.x) <= 1 && abs(player.y - exitGate.y) <= 1) {
if (gateOpen) {
cout << "逃生成功!" << endl;
isRunning = false;
return;
} else {
cout << "门还锁着." << endl;
interacted = true;
}
}
if (!interacted) {
// 如果没交互到,允许移动
player = next;
}
} else {
// 移动逻辑
player = next;
}
}
// 检查开门状态
int count = 0;
for (bool b : repaired) if (b) count++;
if (count >= REPAIR_THRESHOLD) gateOpen = true;
// 追捕者AI
if (hunter.x < player.x) hunter.x++;
else if (hunter.x > player.x) hunter.x--;
if (hunter.y < player.y) hunter.y++;
else if (hunter.y > player.y) hunter.y--;
// 碰撞检测
if (player.x == hunter.x && player.y == hunter.y) {
cout << "被抓住了! 游戏结束." << endl;
isRunning = false;
}
}
bool running() { return isRunning; }
};
int main() {
// 启用虚拟终端处理以支持颜色
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD dwMode = 0;
GetConsoleMode(hOut, &dwMode);
dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
SetConsoleMode(hOut, dwMode);
Game game;
while (game.running()) {
game.draw();
if (_kbhit()) {
char key = _getch();
game.update(key);
}
Sleep(100); // 控制帧率
}
cout << "按任意键退出...";
_getch();
return 0;
}