此迷宫通过键盘上的方向键控制小人方向,直到小人吃掉所有数字,这一关会结束,程序会随机生成另一个地图
- 接下来我来介绍一下各个函数的功能
- 设置光标出现位置
void SetPos(int x, int y)
{
COORD pos = { x, y };
HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hout, pos);
}
- 游戏开始界面
void GameExplain()
{
SetPos(25, 8);
printf("欢迎来到来到迷宫小游戏!!!");
SetPos(25, 11);
printf("使用‘↑ ↓ ← →’来控制小人移动");
SetPos(25, 12);
printf("你要在吃掉7个数字,并且找到迷宫出口");
SetPos(25, 13);
printf("祝你好运");
SetPos(25, 23);
system("pause");
}
- 用来输出方块或者数字
void SetCode(int num)
{
if (num == 0)
printf("■");
else
{
if (num == -1)
printf(" ");
else
printf("%d ", num);
}
}
- 生成随机的数字
void SetNum()
{
int i, j, k = 0;
srand((unsigned)time(NULL));
while (k<7)
{
i = rand() % 17;
j = rand() % 24;
if (code[i][j] == -1)
{
k++;
code[i][j] = k;
}
}
}
- 遍历数组,在(1,1)位置输出玩家头像
void SetWindow()
{
int i, j;
for (i = 0; i<17; i++)
{
for (j = 0; j<24; j++)
{
if (i == 1 && j == 1)
{
printf("\1 ");
}
else
SetCode(code[i][j]);
}
printf("\n");
}
}
- 在(x,y)坐标处输出用户头像
void GoToXY(int x, int y)
{
COORD pos = { x, y };
HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hout, pos);
printf("\1 ");
}
- 用户在走动的过程中,消除之前的位置处的痕迹
void GoToXYSpace(int x, int y)
{
COORD pos = { x, y };
HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hout, pos);
printf(" ");
}
- 下面的四个函数用来控制小人上下左右移动
void LeftKey()
{
if (code[x][y - 1] != 0)
{
GoToXYSpace(y * 2, x);
y--;
GoToXY(y * 2, x);
if (code[x][y]>0)
{
code[x][y] = -1;
num--;
}
}
}
void UpKey()
{
if (code[x - 1][y] != 0)
{
GoToXYSpace(y * 2, x);
x--;
GoToXY(y * 2, x);
if (code[x][y]>0)
{
num--;
code[x][y] = -1;
}
}
}
void DownKey()
{
if (code[x + 1][y] != 0)
{
GoToXYSpace(y * 2, x);
x++;
GoToXY(y * 2, x);
if (code[x][y]>0)
{
code[x][y] = -1;
num--;
}
}
}
void RightKey()
{
if (code[x][y + 1] != 0)
{
GoToXYSpace(y * 2, x);
y++;
GoToXY(y * 2, x);
if (code[x][y]>0)
{
code[x][y] = -1;
num--;
}
}
}
- 这个函数用来隐藏光标
void hidden()//隐藏光标
{
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cci;
GetConsoleCursorInfo(hOut, &cci);
cci.bVisible = 0;//赋1为显示,赋0为隐藏
SetConsoleCursorInfo(hOut, &cci);
}
- 初始化主体函数,设置玩家头像初始位置,调用SetNum函数
void Begin()
{
x = 1, y = 1;
system("CLS");
SetNum();
SetWindow();
GoToXY(y * 2, x);
num = 7;
while (1)
{
char direction;
direction = _getch();
if (direction == 27)
exit(0);
direction = _getch();
switch (direction)//判断方向键
{
case 72:
UpKey();
break;
case 80:
DownKey();
break;
case 75:
LeftKey();
break;
case 77:
RightKey();
break;
}
if (num == 0)
Begin();
}
}
- main 主函数
int main()
{
hidden();
GameExplain();
Begin();
return 0;
}
好了,所有代码介绍完毕,如有疏漏,还请指出,欢迎留言讨论。
最后附上程序所有源代码
#include<stdio.h>
#include<conio.h>
#include<time.h>
#include"windows.h"
int x, y;
int num;
int code[17][24] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, -1, -1, -1, 0, 0,
0, -1, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0,
0, -1, -1, -1, -1, -1, 0, -1, -1, -1, -1, -1, 0, -1, 0, -1, -1, -1, -1, -1, -1, -1, 0, 0,
0, -1, 0, 0, -1, 0, -1, 0, 0, 0, 0, -1, 0, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0,
0, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, -1, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0,
0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, -1, 0,
0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, 0, 0, -1, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, -1, 0, -1, 0, 0, -1, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, -1, 0, -1, -1, -1, -1, 0, -1, -1, 0,
0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, -1, 0, 0,
0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0,
0, 0, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0,
0, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
void SetPos(int x, int y)
{
COORD pos = { x, y };
HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hout, pos);
}
void GameExplain()
{
SetPos(25, 8);
printf("欢迎来到来到迷宫小游戏!!!");
SetPos(25, 11);
printf("使用‘↑ ↓ ← →’来控制小人移动");
SetPos(25, 12);
printf("你要在吃掉7个数字,并且找到迷宫出口");
SetPos(25, 13);
printf("祝你好运");
SetPos(25, 23);
system("pause");
}
void SetCode(int num)
{
if (num == 0)
printf("■");
else
{
if (num == -1)
printf(" ");
else
printf("%d ", num);
}
}
void SetNum()
{
int i, j, k = 0;
srand((unsigned)time(NULL));
while (k<7)
{
i = rand() % 17;
j = rand() % 24;
if (code[i][j] == -1)
{
k++;
code[i][j] = k;
}
}
}
void SetWindow()
{
int i, j;
for (i = 0; i<17; i++)
{
for (j = 0; j<24; j++)
{
if (i == 1 && j == 1)
{
printf("\1 ");
}
else
SetCode(code[i][j]);
}
printf("\n");
}
}
void GoToXY(int x, int y)
{
COORD pos = { x, y };
HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hout, pos);
printf("\1 ");
}
void GoToXYSpace(int x, int y)
{
COORD pos = { x, y };
HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hout, pos);
printf(" ");
}
void LeftKey()
{
if (code[x][y - 1] != 0)
{
GoToXYSpace(y * 2, x);
y--;
GoToXY(y * 2, x);
if (code[x][y]>0)
{
code[x][y] = -1;
num--;
}
}
}
void UpKey()
{
if (code[x - 1][y] != 0)
{
GoToXYSpace(y * 2, x);
x--;
GoToXY(y * 2, x);
if (code[x][y]>0)
{
num--;
code[x][y] = -1;
}
}
}
void DownKey()
{
if (code[x + 1][y] != 0)
{
GoToXYSpace(y * 2, x);
x++;
GoToXY(y * 2, x);
if (code[x][y]>0)
{
code[x][y] = -1;
num--;
}
}
}
void RightKey()
{
if (code[x][y + 1] != 0)
{
GoToXYSpace(y * 2, x);
y++;
GoToXY(y * 2, x);
if (code[x][y]>0)
{
code[x][y] = -1;
num--;
}
}
}
void hidden()//隐藏光标
{
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cci;
GetConsoleCursorInfo(hOut, &cci);
cci.bVisible = 0;//赋1为显示,赋0为隐藏
SetConsoleCursorInfo(hOut, &cci);
}
void Begin()
{
x = 1, y = 1;
system("CLS");
SetNum();
SetWindow();
GoToXY(y * 2, x);
num = 7;
while (1)
{
char direction;
direction = _getch();
if (direction == 27)
exit(0);
direction = _getch();
switch (direction)//判断方向键
{
case 72:
UpKey();
break;
case 80:
DownKey();
break;
case 75:
LeftKey();
break;
case 77:
RightKey();
break;
}
if (num == 0)
Begin();
}
}
int main()
{
hidden();
GameExplain();
Begin();
return 0;
}