c语言迷宫闯关游戏大全,C语言实现迷宫小游戏

C大程的一个作业,实在无聊也可以用来娱乐~

//maze.h

//

//maze.h

#include #include #include #define Wall 1

#define Road 0

#define Start 2

#define End 3

#define Up 1

#define Down 2

#define Left 3

#define Right 4

#define n1 25/*定义行范围*/

#define n2 25/*定义列范围*/

int Length, Width;

//创建一个结构体存储数组信息

typedef struct node

{

int x;

int y;

int c;

}

linkstack;

int map[n1][n2]; /*创建一个二维数组*/

linkstack top[n1 * n2]; /*创建一个N*N的栈*/

//移动坐标

void gotoxy(int x, int y);

//隐藏光标

void hidden();

//随机生成迷宫

void create(int x, int y);

//接收按键

int get_key();

//画迷宫

void paint(int x, int y);

//控制

void game();

//maze.c

//

//maze.c

#include "maze.h"

//移动坐标

void gotoxy(int x, int y)

{

COORD pos = { x, y };

HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);/* 获取标准输出设备句柄*/

SetConsoleCursorPosition(hOut, pos);/*两个参数分别是指定哪个窗体,具体位置*/

}

//隐藏光标

void hidden()

{

HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);

CONSOLE_CURSOR_INFO cci;

GetConsoleCursorInfo(hOut, &cci);

cci.bVisible = 0;/*赋1为显示,赋0为隐藏*/

SetConsoleCursorInfo(hOut, &cci);

}

//随机生成迷宫

void create(int x, int y)

{

int c[4][2]={0,1,1,0,0,-1,-1,0};

int i, j, t;

for(i=0;i<4;i++)

{

j=rand()%4;

t=c[i][0];c[i][0]=c[j][0];c[j][0]=t;

t=c[i][1];c[i][1]=c[j][1];c[j][1]=t;

}

map[x][y]=Road;

for(i=0;i<4;i++)

if(map[x + 2 * c[i][0]][y + 2 * c[i][1]]==Wall)

{

map[x + c[i][0]][y + c[i][1]]=Road;

create(x+2*c[i][0], y+2*c[i][1]);

}

map[2][1]=Start;/*给定入口*/

map[Width - 1][Length]=End;/*给定出口*/

}

//接收按键

int get_key()

{

char c;

while (c = getch())

{

c = getch();

if (c == 72) return Up;/*上*/

if (c == 80) return Down;/*下*/

if (c == 75) return Left;/*左*/

if (c == 77) return Right;/*右*/

}

return 0;

}

//画迷宫

void paint(int x, int y)

{

int i;

gotoxy(2 * y + 1, x + 7);

switch (map[x][y])

{

case Start:

printf("入"); break;/*画入口*/

case End:

printf("出"); break;/*画出口*/

case Wall:

printf("%c%c", 0xa8, 0x80); break; /*画墙*/

case Road:

printf("※"); break;/*画路*/

}

}

//控制

void game()

{

int x = 2, y = 1;/*玩家当前位置,刚开始在入口处*/

int c;/*用来接收按键*/

while (1)

{

gotoxy(2 * y + 1, x + 7);

printf("★\n");/*画出玩家当前位置*/

if (map[x][y] == End)/*判断是否到达出口*/

{

gotoxy(50, 15);

printf("到达终点,按任意键结束");

gotoxy(2 * y + 1, x + Width - 13);

break;

}

c = get_key();

switch (c)

{

case Up:/*向上走*/

if (map[x - 1][y] != Wall)

{

paint(x, y);

x--;

}

break;

case Down:/*向下走*/

if (map[x + 1][y] != Wall)

{

paint(x, y);

x++;

}

break;

case Left:/*向左走*/

if (map[x][y - 1] != Wall)

{

paint(x, y);

y--;

}

break;

case Right: /*向右走*/

if (map[x][y + 1] != Wall)

{

paint(x, y);

y++;

}

break;

}

}

}

//main.c

//

//main.c

#include "maze.h"

int main()

{

system("COLOR 00");

int i, j, w;

printf("欢迎来到迷宫小游戏!\n");

printf("生成迷宫请按:1\n");

printf("退出游戏请按:2\n");

printf("\n");

printf("输入您的选择:");

scanf("%d", &w);

switch (w)/*若输入的W为1或2,则继续程序*/

{

case 1:

printf("输入迷宫宽度(提示:不能大于25且必须为奇数):");/*W为1时*/

scanf("%d", &Width);

printf("输入迷宫长度(提示:不能大于25且必须为奇数):");/*W为1时*/

scanf("%d", &Length);

printf(" ");

for (i = 65; i <= (Length + 64); i++)

printf("%c ", i);

printf("\n");

for (i = 65; i <= (Length + 64); i++)

printf(" %c\n", i);

system("COLOR 70");

srand((unsigned)time(NULL)); /*初始化随机种子*/

hidden();/*隐藏光标*/

for (i = 0; i <= Length + 1; i++)

for (j = 0; j <= Width + 1; j++)

if (i == 0 || i == Length + 1 || j == 0 || j == Width + 1) /*初始化迷宫*/

map[i][j] = Road;

else map[i][j] = Wall;

create(2 * (rand() % (Length / 2) + 1), 2 * (rand() % (Width / 2) + 1)); /*从随机一个点开始生成迷宫,该点行列都为偶数*/

for (i = 0; i <= Length + 1; i++)/*边界处理*/

{

map[i][0] = Wall;

map[i][Width + 1] = Wall;

}

for (j = 0; j <= Width + 1; j++)/*边界处理*/

{

map[0][j] = Wall;

map[Length + 1][j] = Wall;

}

for (i = 1; i <= Length; i++)

for (j = 1; j <= Width; j++)/*画出迷宫*/

paint(i, j);

game();/*开始游戏*/

getch();

return 0;

break;

case 2:

printf("欢迎下次使用!");

break;

default: break;

}

}

你可能感兴趣的:(c语言迷宫闯关游戏大全)