制作迷宫游戏的方法有很多种,这里提供几种不同的实现方式:
1. 使用二维数组表示迷宫
你可以使用二维数组来表示迷宫的结构,其中0代表墙壁,1代表路径,2代表终点。以下是一个简单的示例代码:
```cpp
include using namespace std; void displayMaze(int maze[], int rows, int cols) { for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if (maze[i][j] == 0) { cout << ""; } else if (maze[i][j] == 1) { cout << " "; } else if (maze[i][j] == 2) { cout << "E"; } } cout << endl; } } int main() { int maze = { {0, 1, 0, 1, 0, 1, 0, 1, 0, 1}, {0, 1, 0, 1, 0, 1, 0, 1, 0, 1}, {0, 1, 0, 1, 0, 1, 0, 1, 0, 1}, {0, 1, 0, 1, 0, 1, 0, 1, 0, 1}, {0, 1, 0, 1, 0, 1, 0, 1, 0, 1}, {0, 1, 0, 1, 0, 1, 0, 1, 0, 1}, {0, 1, 0, 1, 0, 1, 0, 1, 0, 1}, {0, 1, 0, 1, 0, 1, 0, 1, 0, 1}, {0, 1, 0, 1, 0, 1, 0, 1, 0, 1}, {0, 1, 0, 1, 0, 1, 0, 1, 0, 1}, {0, 1, 0, 1, 0, 1, 0, 1, 0, 2} }; int playerX = 1; int playerY = 1; while (true) { displayMaze(maze, 10, 10); char direction; cin >> direction; if (direction == 'w' && maze[playerX - 1][playerY] != 0) { playerX--; } else if (direction == 's' && maze[playerX + 1][playerY] != 0) { playerX++; } else if (direction == 'a' && maze[playerX][playerY - 1] != 0) { playerY--; } else if (direction == 'd' && maze[playerX][playerY + 1] != 0) { playerY++; } if (maze[playerX][playerY] == 2) { cout << "You reached the exit!" << endl; break; } } return 0; } ``` 2. 使用3D引擎生成迷宫 你可以使用3D引擎如Ursina来生成和显示迷宫。以下是一个使用Ursina和numpy生成随机迷宫的示例: