迷宫-1代c++代码
2025-08-07 09:03:39
发布于:广东
#include <iostream>
#include <windows.h>
#include <conio.h>
#include <ctime>
#include <vector>
#include <algorithm>
using namespace std;
const int WIDTH = 20;
const int HEIGHT = 40;
enum Tile { WALL=1, PATH=2, END=3, KEY=4, MONSTER=5, TREASURE=6, TRAP=7 };
struct Position {
int x, y;
bool operator==(const Position& other) const {
return x == other.x && y == other.y;
}
};
struct GameState {
int map[WIDTH][HEIGHT];
Position player;
vector<Position> monsters;
vector<Position> treasures;
vector<Position> traps;
Position exit;
bool hasKey;
int score;
int level;
int lives;
};
GameState game;
int monsterSpeed = 2;
bool gameOver = false;
void initGame() {
game.player = {1, 1};
game.hasKey = false;
game.score = 0;
game.level = 1;
game.lives = 3;
}
void generateMap() {
// 清空地图
for(int i=0; i<WIDTH; i++) {
for(int j=0; j<HEIGHT; j++) {
game.map[i][j] = (i==0 || i==WIDTH-1 || j==0 || j==HEIGHT-1) ? WALL : PATH;
}
}
// 生成复杂迷宫结构
for(int i=2; i<WIDTH-2; i+=2) {
for(int j=2; j<HEIGHT-2; j+=2) {
game.map[i][j] = WALL;
int dir = rand()%4;
switch(dir) {
case 0: if(i>2) game.map[i-1][j] = WALL; break;
case 1: if(i<WIDTH-3) game.map[i+1][j] = WALL; break;
case 2: if(j>2) game.map[i][j-1] = WALL; break;
case 3: if(j<HEIGHT-3) game.map[i][j+1] = WALL; break;
}
}
}
// 放置出口
game.exit = {WIDTH-2, HEIGHT-2};
game.map[game.exit.x][game.exit.y] = END;
// 放置钥匙
Position keyPos;
do {
keyPos = {rand()%(WIDTH-4)+2, rand()%(HEIGHT-4)+2};
} while(game.map[keyPos.x][keyPos.y] != PATH);
game.map[keyPos.x][keyPos.y] = KEY;
// 生成怪物(数量随关卡增加)
game.monsters.clear();
for(int i=0; i<game.level; i++) {
Position monster;
do {
monster = {rand()%(WIDTH-4)+2, rand()%(HEIGHT-4)+2};
} while(game.map[monster.x][monster.y] != PATH ||
abs(monster.x-game.player.x) < 5 ||
abs(monster.y-game.player.y) < 5);
game.monsters.push_back(monster);
}
// 生成宝藏和陷阱
game.treasures.clear();
game.traps.clear();
for(int i=0; i<3; i++) {
Position treasure;
do {
treasure = {rand()%(WIDTH-4)+2, rand()%(HEIGHT-4)+2};
} while(game.map[treasure.x][treasure.y] != PATH);
game.treasures.push_back(treasure);
Position trap;
do {
trap = {rand()%(WIDTH-4)+2, rand()%(HEIGHT-4)+2};
} while(game.map[trap.x][trap.y] != PATH);
game.traps.push_back(trap);
}
}
void drawMap() {
system("cls");
cout << "Level: " << game.level << " Score: " << game.score
<< " Lives: " << game.lives << (game.hasKey ? " KEY" : "") << endl;
for(int i=0; i<WIDTH; i++) {
for(int j=0; j<HEIGHT; j++) {
Position p = {i,j};
if(p == game.player) cout << 'P';
else if(find(game.monsters.begin(), game.monsters.end(), p) != game.monsters.end()) cout << 'M';
else if(find(game.treasures.begin(), game.treasures.end(), p) != game.treasures.end()) cout << '$';
else if(find(game.traps.begin(), game.traps.end(), p) != game.traps.end()) cout << 'X';
else {
switch(game.map[i][j]) {
case WALL: cout << '#'; break;
case PATH: cout << ' '; break;
case END: cout << 'E'; break;
case KEY: cout << 'K'; break;
}
}
}
cout << endl;
}
cout << "WASD移动 Q退出 R重玩当前关" << endl;
}
void movePlayer(int dx, int dy) {
Position newPos = {game.player.x+dx, game.player.y+dy};
// 检查边界和墙壁
if(newPos.x<0 || newPos.x>=WIDTH || newPos.y<0 || newPos.y>=HEIGHT ||
game.map[newPos.x][newPos.y] == WALL) return;
// 检查陷阱
auto trapIt = find(game.traps.begin(), game.traps.end(), newPos);
if(trapIt != game.traps.end()) {
game.lives--;
game.traps.erase(trapIt);
if(game.lives <= 0) {
gameOver = true;
return;
}
}
// 检查宝藏
auto treasureIt = find(game.treasures.begin(), game.treasures.end(), newPos);
if(treasureIt != game.treasures.end()) {
game.score += 50;
game.treasures.erase(treasureIt);
}
// 检查钥匙
if(game.map[newPos.x][newPos.y] == KEY) {
game.hasKey = true;
game.map[newPos.x][newPos.y] = PATH;
}
// 检查出口
if(game.map[newPos.x][newPos.y] == END && game.hasKey) {
game.level++;
game.score += 100 * game.level;
initGame();
generateMap();
return;
}
game.player = newPos;
}
void updateMonsters() {
static int counter = 0;
if(++counter % monsterSpeed != 0) return;
for(auto& monster : game.monsters) {
// 简单AI: 50%概率随机移动,50%概率追踪玩家
if(rand()%2 == 0) {
int dx = (game.player.x > monster.x) ? 1 : (game.player.x < monster.x) ? -1 : 0;
int dy = (game.player.y > monster.y) ? 1 : (game.player.y < monster.y) ? -1 : 0;
Position newPos = {monster.x+dx, monster.y+dy};
if(game.map[newPos.x][newPos.y] != WALL &&
find(game.monsters.begin(), game.monsters.end(), newPos) == game.monsters.end()) {
monster = newPos;
}
} else {
int dir = rand()%4;
Position newPos = monster;
switch(dir) {
case 0: newPos.x--; break;
case 1: newPos.x++; break;
case 2: newPos.y--; break;
case 3: newPos.y++; break;
}
if(game.map[newPos.x][newPos.y] != WALL &&
find(game.monsters.begin(), game.monsters.end(), newPos) == game.monsters.end()) {
monster = newPos;
}
}
// 检查是否碰到怪物
if(monster == game.player) {
game.lives--;
if(game.lives <= 0) {
gameOver = true;
return;
}
// 玩家重生
game.player = {1,1};
}
}
}
int main() {
srand(time(0));
initGame();
generateMap();
while(!gameOver) {
drawMap();
if(_kbhit()) {
char input = _getch();
switch(tolower(input)) {
case 'w': movePlayer(-1, 0); break;
case 'a': movePlayer(0, -1); break;
case 's': movePlayer(1, 0); break;
case 'd': movePlayer(0, 1); break;
case 'q': gameOver = true; break;
case 'r': generateMap(); break;
}
}
updateMonsters();
Sleep(150);
}
system("cls");
cout << "游戏结束!最终得分: " << game.score << " 通过关卡: " << game.level+1<< endl;
return 0;
}
这里空空如也
有帮助,赞一个