c语言走迷宫游戏代码
时间: 2023-12-19 17:29:54 浏览: 139
以下是一个简单的C语言走迷宫游戏代码,具体实现方法如下:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#define ROW 10
#define COL 10
int maze[ROW][COL] = {
{1,1,1,1,1,1,1,1,1,1},
{1,0,0,1,0,0,0,1,0,1},
{1,0,0,1,0,0,0,1,0,1},
{1,0,0,0,0,1,1,0,0,1},
{1,0,1,1,1,0,0,0,0,1},
{1,0,0,0,1,0,0,0,0,1},
{1,0,1,0,0,0,1,0,0,1},
{1,0,1,1,1,0,1,1,0,1},
{1,1,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1}
};
void print_maze() {
int i, j;
for (i = 0; i < ROW; i++) {
for (j = 0; j < COL; j++) {
if (maze[i][j] == 1) {
printf("#");
} else if (maze[i][j] == 0) {
printf(" ");
} else if (maze[i][j] == 2) {
printf("@");
}
}
printf("\n");
}
}
int main() {
int x = 1, y = 1;
int start_time, end_time;
char ch;
srand((unsigned)time(NULL));
start_time = time(NULL);
while (1) {
system("cls");
print_maze();
if (x == 8 && y == 8) {
end_time = time(NULL);
printf("Congratulations! You win!\n");
printf("Time used: %d seconds\n", end_time - start_time);
break;
}
ch = getch();
switch (ch) {
case 'w':
if (maze[x-1][y] == 0) {
maze[x][y] = 0;
x--;
maze[x][y] = 2;
}
break;
case 's':
if (maze[x+1][y] == 0) {
maze[x][y] = 0;
x++;
maze[x][y] = 2;
}
break;
case 'a':
if (maze[x][y-1] == 0) {
maze[x][y] = 0;
y--;
maze[x][y] = 2;
}
break;
case 'd':
if (maze[x][y+1] == 0) {
maze[x][y] = 0;
y++;
maze[x][y] = 2;
}
break;
default:
break;
}
}
return 0;
}
```
阅读全文