#include <stdio.h>
#include <stdlib.h>
#include <getch.h>
#include <time.h>
int main(int argc,const char* argv[])
{
// 0代表路,1代表墙,2代表老鼠
char map[15][15] = {
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,2,1,0,1,0,1,1,1,0,1,1,1,1,1},
{1,0,1,0,1,0,0,0,1,0,0,0,0,0,1},
{1,0,1,0,1,0,1,0,1,0,1,0,1,0,1},
{1,0,0,0,1,0,1,0,1,0,1,0,1,0,1},
{1,0,1,0,1,0,1,1,1,0,1,1,1,0,1},
{1,0,1,1,1,0,1,0,1,0,1,0,0,0,1},
{1,0,1,0,1,0,1,0,1,0,0,0,1,0,1},
{1,0,0,0,1,0,1,0,1,0,1,1,1,0,1},
{1,0,1,0,1,0,0,0,1,0,0,0,1,0,1},
{1,0,1,0,0,0,1,0,1,0,1,0,1,0,1},
{1,0,1,1,1,0,1,0,1,0,1,0,1,0,1},
{1,0,1,0,1,0,1,0,0,0,1,0,1,0,1},
{1,0,0,0,1,0,1,0,1,1,1,0,1,0,0},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
};
// 记录老鼠位置的变量
char mr = 1, mc = 1;
// 记录游戏的开始时间
time_t start_time = time(NULL);
// 进入游戏
for(;;)
{
// 清理屏幕
system("clear");
// 显示地图
for(int r=0; r<15; r++)
{
for(int c=0; c<15; c++)
{
switch(map[r][c])
{
case 0:
printf(" ");
break;
case 1:
printf("#");
break;
case 2:
printf("@");
break;
}
}
printf("\n");
}
if(13 == mr && 14 == mc)
{
printf("恭喜你走出迷宫,共用时%ld秒!\n",
time(NULL)-start_time);
break;
}
switch(getch())
{
case 183:
if(0 == map[mr-1][mc])
{
map[mr][mc] = 0;
map[--mr][mc] = 2;
}
break;
case 184:
if(0 == map[mr+1][mc])
{
map[mr][mc] = 0;
map[++mr][mc] = 2;
}
break;
case 185:
if(0 == map[mr][mc+1])
{
map[mr][mc] = 0;
map[mr][++mc] = 2;
}
break;
case 186:
if(0 == map[mr][mc-1])
{
map[mr][mc] = 0;
map[mr][--mc] = 2;
}
break;
}
}
}
小鼠走迷宫(C语言)
于 2023-07-27 15:12:25 首次发布
该程序是一个使用C语言编写的迷宫游戏,玩家控制老鼠在由1(墙)、0(路)和2(老鼠)表示的地图中移动。玩家通过键盘输入控制老鼠上下左右移动,目标是将老鼠从起点移动到终点。游戏记录开始时间,当老鼠到达终点时,显示所用时间。
摘要由CSDN通过智能技术生成