一共设计了三关
所有图像全部是用图片搞出来的
#include<stdio.h> #include<easyx.h> #include<conio.h> #include<graphics.h>
#include<cstring> #define _CRT_SECURE_NO_WARNINGS /*1.地图打印 * 2.角色移动 * 1.按键处理
(动的只能是人和被人推动的箱子) * 2.找到人的位置,人怎么动(数组操作) * 3.多关卡设置 * 1.关卡完成设置 * 2.进入下一关卡设置 *
4.图片设计 * 1.角色设置 4 * 2.箱子 2 * 3.目的 3 * 4.墙 1 * 5.地板 0 */ IMAGE img[6];//全局变量
HWND hnd = NULL; int cos = 0,grade=0;//关卡设置 int map[3][10][10]//地图 {
1,1,1,1,1,1,1,1,1,1, 1,0,0,0,3,0,0,0,0,1, 1,0,0,0,0,0,0,1,0,1,
1,0,1,0,0,0,1,0,0,1, 1,0,0,0,4,0,0,0,0,1, 1,0,1,0,0,0,0,1,0,1,
1,0,1,1,0,1,0,1,0,1, 1,0,2,0,0,0,0,0,0,1, 1,0,0,0,0,0,0,0,0,1,
1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,0,0,0,3,0,0,0,0,1,
1,0,2,0,0,0,0,1,0,1, 1,0,1,1,0,0,1,0,0,1, 1,0,0,0,4,0,0,0,0,1,
1,0,1,0,0,0,1,1,0,1, 1,0,1,1,0,1,1,1,0,1, 1,0,0,0,2,0,0,0,0,1,
1,3,0,0,0,0,0,0,0,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,
1,0,0,0,0,0,0,0,0,1, 1,0,0,2,0,0,0,1,3,1, 1,0,1,0,0,0,1,0,0,1,
1,3,0,0,4,0,0,0,0,1, 1,0,1,0,0,0,0,1,0,1, 1,0,1,1,0,1,2,1,0,1,
1,0,0,0,0,0,0,0,0,1, 1,0,0,0,0,0,0,0,0,1, 1,1,1,1,1,1,1,1,1,1 }; void
load()//加载图像 { for (int i = 0; i < 6; i++) { char s[20] = ""; sprintf(s,
"%d.jpg", i);//强制类型转换,只能读取字符串 loadimage(img+i,s,64,64);//64,64是缩放 } } void
drawmap()//画地图 { int x, y; for (int i = 0; i < 10; i++) { for (int j = 0; j <
10; j++) { //贴照片与坐标有关 x = 64 * j; y = 64 * i; switch (map[cos][i][j]) { case
0://地板 putimage(x, y, img + 0); break; case 1://墙 putimage(x, y, img + 1);
break; case 2://箱子 putimage(x, y, img + 2); break; case 3://目的地 putimage(x, y,
img + 3); break; case 4://人物 putimage(x, y, img + 4); break; case 7://人站在目的地
putimage(x, y, img + 4); break; case 5://箱子到达目的地 putimage(x, y, img + 5);
break; } } } } void keydown() { int x, y,i,j;//每一次都要找,找到人的坐标 for (x = 0; x <
10; x++) { for (y = 0; y < 10; y++) { if (map[cos][x][y] ==
4||map[cos][x][y]==7) { i = x; j = y; break; } if (map[cos][x][y] == 4 ||
map[cos][x][y] == 7) break; } } //按键处理 char userkey=0; userkey=_getch();//不可见输入
switch (userkey)//是地板或者是目的地的情况可以移动 { case 'w': case 'W': case 72: if
(map[cos][i - 1][j] == 0 || map[cos][i - 1][j] == 3) { map[cos][i][j] -= 4;
map[cos][i - 1][j] += 4; grade++; } if (map[cos][i - 1][j] == 2 )//移动箱子 { if
(map[cos][i - 2][j] == 0)//旁边是地板的情况 { map[cos][i - 2][j] += 2; map[cos][i -
1][j] += 2;//变成人 map[cos][i][j] -= 4;//人物变成地板 grade++; } if (map[cos][i - 2][j]
== 3)//旁边是目的地 { map[cos][i - 2][j] = 5; map[cos][i - 1][j] += 2; map[cos][i][j]
-= 4;//人物变成地板 grade++; } } break; case 's': case 'S': case 80: if (map[cos][i +
1][j] == 0 || map[cos][i + 1][j] == 3)//人移动 { map[cos][i][j] -= 4; map[cos][i +
1][j] += 4; grade++; } if (map[cos][i + 1][j] == 2)//移动箱子 { if (map[cos][i +
2][j] == 0)//旁边是地板的情况 { map[cos][i + 2][j] += 2; map[cos][i + 1][j] += 2;
map[cos][i][j] -= 4;//人物变成地板 grade++; } if (map[cos][i+2][j ] == 3)//旁边是目的地 {
map[cos][i+2][j] = 5; map[cos][i+1][j] += 2; map[cos][i][j] -= 4;//人物变成地板
grade++; } } break; case 'a': case 'A': case 75: if (map[cos][i][j - 1] == 0 ||
map[cos][i][j - 1] == 3) { map[cos][i][j] -= 4; map[cos][i][j - 1] += 4;
grade++; } if (map[cos][i ][j-1] == 2)//移动箱子 { if (map[cos][i ][j-2] ==
0)//旁边是地板的情况 { map[cos][i][j-2] += 2; map[cos][i][j-1] += 2; map[cos][i][j] -=
4;//人物变成地板 grade++; } if (map[cos][i][j - 2] == 3)//旁边是目的地 { map[cos][i][j - 2]
= 5; map[cos][i][j - 1] += 2; map[cos][i][j] -= 4;//人物变成地板 grade++; } } break;
case 'd': case 'D': case 77: if (map[cos][i][j + 1] == 0 || map[cos][i][j + 1]
== 3) { map[cos][i][j] -= 4; map[cos][i][j + 1] += 4; grade++; } if
(map[cos][i][j+1] == 2)//移动箱子 { if (map[cos][i][j+2] == 0)//旁边是地板的情况 {
map[cos][i][j +2] += 2; map[cos][i][j + 1] += 2; map[cos][i][j] -= 4;//人物变成地板
grade++; } if (map[cos][i][j + 2] == 3)//旁边是目的地 { map[cos][i][j + 2] = 5;
map[cos][i][j + 1] += 2; map[cos][i][j] -= 4;//人物变成地板 grade++; } } break; } }
int gameover() { int i, j,sign=0; for (i = 0; i < 10; i++) { for (j = 0; j <
10; j++) { if (map[cos][i][j] == 3) { sign = 1; return 1; } } } return 0; }
void show()//显示分数 { char str[10000];//窗口只能展示字符串,将整型转化成字符串 sprintf(str, "%d",
grade); setbkmode(TRANSPARENT);//设成透明模式 settextstyle(15, 10, "宋体");//设置字体
settextcolor(BLACK); SetWindowText(hnd, "推箱子"); outtextxy(540, 20, "步数:");
outtextxy(600, 20, str); } void show1()//弹出窗口 { if (cos < 2) { MessageBox(NULL,
"恭喜你,你赢了,请按空格键继续。", "推箱子", MB_OKCANCEL); } if (cos == 2) { MessageBox(NULL,
"恭喜你,你通关了", "推箱子", MB_OKCANCEL); } } int main() { initgraph(640, 640);
cleardevice(); while (1) { load(); drawmap(); show(); if (gameover() == 0) {
show1(); cos++; grade = 0; if (cos == 3) break; } keydown(); } }