解决了路变墙 墙变路的问题
void onechange(ExMessage* msg)
{
if (msg->message == WM_LBUTTONDOWN && msg->x >= 50 && msg->x <= 410 && msg->y >= 50 && msg->y <= 410)
{
//printf("鼠标位置:x:%d y:%d\n", msg->x, msg->y);
int indexx = (msg->x - 50) / 30;//表示b
int indexy = (msg->y - 50) / 30;//表示a
//printf(" %d %d\n", indexy, indexx);
if (map[indexy][indexx])
{
map[indexy][indexx] = 0;
//printf("ok");
}
else
{
map[indexy][indexx] = 1;
}
//printf(" 地图:%d ", map[indexy][indexx]);
}
}
然后就是倒计时的问题
设置一个全局变量 来记录我开始的时间,然后一个结束时间在每一关当中,两个相减得到一个计时的数据,然后用30也就是我们倒计时的时间来减去这个数据,就可以得到一个倒计时
//倒计时
char str[100];
int countdown;
clock_t Starttime;//获取开始时间 结束时间在每一个关卡里面
//第一关倒计时
void onetime()
{
int Endtime = clock();
countdown = (int)((Endtime - Starttime) / CLOCKS_PER_SEC);
countdown = 10 - countdown;//控制几秒倒计时
sprintf_s(str, "%d s", countdown);
printf("%d ", countdown);
}
//倒计时结束的处理
if (countdown < 0)
{
zero = 1;
one = 0;
loser = 1;
printf("\n%d %d %d %d\n", zero, one, loser, countdown);
}
然后还解决了一下倒计时结束的问题,倒计时结束就会跳转到这个页面
然后 点击上面这个”四肢!栓栓的“就是再来一次 点击下面”脑袋!空空的“就是跳转到主界面
//输掉的函数
void losedraw()
{
//切换图片加上双缓冲
BeginBatchDraw();
IMAGE img;
loadimage(&img, "./lose.jpg", 500, 500);
putimage(0, 0, &img);
FlushBatchDraw();
}
void backoragain(ExMessage* msg)
{
printf("鼠标位置:x:%d y:%d\n", msg->x, msg->y);
if (msg->message== WM_LBUTTONDOWN && msg->x >= 220 && msg->x <= 490 && msg->y >= 60 && msg->y <= 120)//再来一次
{
//判断是第几关 用一个level来判断
switch (level)
{
case 1:
one = 1;
loser = 0;
level = 0;
Starttime = clock();
case 2:
two = 1;
loser = 0;
case 3:
three = 1;
loser = 0;
}
printf(" 第一关%d\n", one);
}
if (msg->message == WM_LBUTTONDOWN && msg->x >= 15 && msg->x <= 380 && msg->y >= 270 && msg->y <= 430)//返回主菜单
{
loser = 0;
zero = 0;
}
}
void lose()
{
losedraw();
ExMessage msg;
if (peekmessage(&msg, EM_MOUSE))
{
backoragain(&msg);
}
}
今天的代码
#include <stdio.h>
#include <easyx.h>
#include <graphics.h>
#include <math.h>
#include <conio.h>
#include <time.h>
int one,zero,two,three;
int level;
int winer, loser;
//倒计时
char str[100];
int countdown;
clock_t Starttime;//获取开始时间 结束时间在每一个关卡里面
//输掉的函数
void losedraw()
{
//切换图片加上双缓冲
BeginBatchDraw();
IMAGE img;
loadimage(&img, "./lose.jpg", 500, 500);
putimage(0, 0, &img);
FlushBatchDraw();
}
void backoragain(ExMessage* msg)
{
printf("鼠标位置:x:%d y:%d\n", msg->x, msg->y);
if (msg->message== WM_LBUTTONDOWN && msg->x >= 220 && msg->x <= 490 && msg->y >= 60 && msg->y <= 120)//再来一次
{
//判断是第几关 用一个level来判断
switch (level)
{
case 1:
one = 1;
loser = 0;
level = 0;
Starttime = clock();
case 2:
two = 1;
loser = 0;
case 3:
three = 1;
loser = 0;
}
printf(" 第一关%d\n", one);
}
if (msg->message == WM_LBUTTONDOWN && msg->x >= 15 && msg->x <= 380 && msg->y >= 270 && msg->y <= 430)//返回主菜单
{
loser = 0;
zero = 0;
}
}
void lose()
{
losedraw();
ExMessage msg;
if (peekmessage(&msg, EM_MOUSE))
{
backoragain(&msg);
}
}
//第一关
int a = 1, b = 1;//控制鼠鼠的坐标,把x y看成数组下标!然后通过数组下标来画鼠鼠
int aa, bb;
int map[12][12] =//第一个地图 1为可以走,0为不能走(这样子可以不用搞最外面的边界)
{
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,1,1,0,1,1,1,0,0,0,0,0},
{0,0,1,0,0,0,1,0,1,1,1,0},
{0,1,1,1,1,0,1,1,0,0,1,0},
{0,1,0,0,1,0,0,0,1,1,1,0},
{0,1,1,1,1,0,1,1,0,0,1,0},
{0,0,0,0,1,0,1,0,1,0,1,0},
{0,1,1,0,1,0,1,0,1,0,1,0},
{0,1,1,1,1,1,1,1,1,0,1,0},
{0,0,0,0,0,0,0,0,1,1,1,0},
{0,0,1,1,1,1,1,1,1,0,1,0},
{0,0,0,0,0,0,0,0,0,0,0,0}
};
//到达终点 倒计时结束判断
void shuexport()
{
if (a == 10 && b == 10)
{
int is_ok = MessageBox(GetHWnd(), "提示", "你真的牛", MB_OKCANCEL);
}
//倒计时结束的处理
if (countdown < 0)
{
zero = 1;
one = 0;
loser = 1;
printf("\n%d %d %d %d\n", zero, one, loser, countdown);
}
}
//画图
void onedraw()
{
//画图要在这个双缓冲里面
BeginBatchDraw();
//加载背景图片
IMAGE img;
loadimage(&img, "./shu.jpg", 500, 500);
putimage(0, 0, &img);
//绘制返回按钮
settextstyle(30, 0, "华文彩云");
outtextxy(20, 15, "返回");
//下面是地图的绘画
//鼠鼠的绘制
setfillcolor(BLACK);
if (a >= 0 && a < 12 && b >= 0 && b < 12 && map[a][b])//如果这个点在地图上为ok那么我们就画
{
int drawmousex = b;
int drawmousey = a;
fillrectangle(50 + (30 * drawmousex), 50 + (30 * drawmousey), 50 + (30 * drawmousex) + 30, 50 + (30 * drawmousey) + 30);//把它看作鼠鼠
aa = a;
bb = b;
}
else
{
int drawmousex = bb;
int drawmousey = aa;
fillrectangle(50 + (30 * drawmousex), 50 + (30 * drawmousey), 50 + (30 * drawmousex) + 30, 50 + (30 * drawmousey) + 30);
a = aa;
b = bb;
}
setfillcolor(RED);
//定义墙壁的图片 墙壁的绘制
IMAGE wall;
loadimage(&wall, "./wall.jpg", 30, 30);
for (int i = 0; i < 12; i++)
{
for (int j = 0; j < 12; j++)
{
if (!map[i][j])//j表示x i表示yf
{
int drawx = j;
int drawy = i;
putimage(50 + (drawx * 30), 50 + (drawy * 30), &wall);
//fillrectangle(50+(30*j), 50 + (30 * i), 50 + (30 * j)+30, 50 + (30 * i)+30);
}
//printf("%d %d %d\n ",i,j,map[i][j]);
//illrectangle(i , j , i + 20, j + 20);
}
}
//倒计时的绘制
setbkmode(TRANSPARENT);
settextstyle(30, 0, "华文行楷");
settextcolor(BLACK);
//printf("%s", str);
outtextxy(420, 420, str);
FlushBatchDraw();
}
//鼠鼠的移动
void yidong()//通过全局变量x y来确定鼠鼠的移动
{
//改变的是二维数组的地方 然后我们画图再重新定义两个变量map[a][b]
if (_kbhit() != 0)//判断是否有键盘输入,防止_getch()导致函数卡着不动
{
char key = _getch();
switch (key)
{
case 72:
a -= 1;
//yy += 1;
printf("上键\n");
break;
case 80:
a += 1;
//yy -= 1;
printf("下键\n");
break;
case 75:
b -= 1;
//xx += 1;
printf("左键\n");
break;
case 77:
b += 1;
//xx -= 1;
printf("右键\n");
break;
}
}
}
//第一关返回主菜单按钮
void oneback(ExMessage* msg)
{
//printf("鼠标位置:x:%d y:%d\n", msg->x, msg->y);
if (msg->message == WM_LBUTTONDOWN && msg->x >= 20 && msg->x <= 80 && msg->y >= 15 && msg->y <= 45)
{
one = 0;
zero = 0;
}
//printf("%d %d ", one, zero);
}
//第一关的路变墙 墙变路
void onechange(ExMessage* msg)
{
if (msg->message == WM_LBUTTONDOWN && msg->x >= 50 && msg->x <= 410 && msg->y >= 50 && msg->y <= 410)
{
//printf("鼠标位置:x:%d y:%d\n", msg->x, msg->y);
int indexx = (msg->x - 50) / 30;//表示b
int indexy = (msg->y - 50) / 30;//表示a
//printf(" %d %d\n", indexy, indexx);
if (map[indexy][indexx])
{
map[indexy][indexx] = 0;
//printf("ok");
}
else
{
map[indexy][indexx] = 1;
}
//printf(" 地图:%d ", map[indexy][indexx]);
}
}
//第一关倒计时
void onetime()
{
int Endtime = clock();
countdown = (int)((Endtime - Starttime) / CLOCKS_PER_SEC);
countdown = 10 - countdown;//控制几秒倒计时
sprintf_s(str, "%d s", countdown);
printf("%d ", countdown);
}
//第一关的主函数
void easy()
{
level = 1;
//画图
onedraw();
//倒计时
onetime();
//控制返回主界面 墙变路 路变墙
ExMessage msg;
if (peekmessage(&msg, EM_MOUSE))
{
//printf("88888");
//返回主菜单
oneback(&msg);
//路变墙 墙变路
onechange(&msg);
}
//控制鼠鼠移动
yidong();
//printf("%d %d\n", a, b);
//判断鼠鼠有没有到出口的函数 以及时间到达的处理
shuexport();
}
//主菜单
//主菜单通往各个界面
void mousedown(ExMessage* msg)
{
if (msg->message == WM_LBUTTONDOWN && msg->x >= 200 && msg->x <= 290 && msg->y >= 150 && msg->y <= 180)
{
one = 1;
zero = 1;
Starttime = clock();
}
}//主菜单的
void home()
{
//背景图
IMAGE img;
loadimage(&img, "./shu.jpg", 500, 500);
putimage(0, 0, &img);
//第一关图片
BeginBatchDraw();//双缓冲
setbkmode(TRANSPARENT);
settextstyle(30, 0, "华文彩云");
outtextxy(200, 150, "走迷宫");
//fillrectangle(200, 150, 290, 180);
ExMessage msg;
if (peekmessage(&msg, EM_MOUSE))
{
mousedown(&msg);
}
FlushBatchDraw();
}//主菜单
int main()
{
initgraph(500, 500, SHOWCONSOLE);
while (true)
{
if (zero == 0)
{
home();
}
if (one == 1)
{
easy();//简单的关卡
}
if (loser == 1)
{
lose();
}
}
getchar();
}
明天就是做个外挂键,按一下直接出现最短到终点的路,还要搞一下终点的图形,然后再按照我现在写的再做几个地图就ok
下班下班o(* ̄▽ ̄*)ブ