c语言小白,借助推箱子练习下easyx库的使用。还有一些细节没完善,正常运行没问题
前言:用到了easyx库
#include
#include
#include
#include
#define width 10 //乘于图片大小=游戏框宽度
#define high 7 //高度
IMAGE img[8];
int map[high][width]=
{
1,1,1,1,1,1,1,1,1,1,
1,1,1,1,0,0,0,0,1,1,
1,1,3,0,4,1,1,0,1,1,
1,3,3,4,0,4,0,5,1,1,
1,3,3,0,4,0,4,0,1,1,
1,1,1,1,1,1,0,0,1,1,
1,1,1,1,1,1,1,1,1,1
}; //地图,1:墙 3:目的地 4:箱子 5、8:人 7:箱子到达目的地
void show()
{
int i, j;
int x, y;
for (i = 0; i < high; i++)
{
for (j = 0; j < width; j++)
{
x = 64 * j;
y = 64 * i;
switch (map[i][j])
{
case 0:
putimage(x, y, &img[0]);
break;
case 1:
putimage(x, y, &img[1]); //墙
break;
case 3:
putimage(x, y, &img[2]); //目的地
break;
case 4:
putimage(x, y, &img[3]); //箱子
break;
case 5:
case 8:
putimage(x, y, &img[4]); //人
break;
case 7:
putimage(x, y, &img[5]); //目的地
break;
}
}
}
//printf("得分为:%d", score);
}
void updateinput()
{
int i, j;
char input;
input = _getch();
for (i = 0; i < width; i++) //找到人物所在位置
{
for (j = 0; j < high; j++)
if (map[i][j] == 5 || map[i][j] == 8)
{
break;
}
if (map[i][j] == 5 || map[i][j] == 8)
{
break;
}
}
switch (input)
{
case 'w':
if (map[i - 1][j] == 0 || map[i - 1][j] == 3) //上方空或目的地
{
map[i - 1][j] += 5;
map[i][j] -= 5;
}
if (map[i - 1][j] == 4 || map[i - 1][j] == 7) //上方为箱子
{
if (map[i - 2][j] == 0 || map[i - 2][j] == 3) //箱子上面为空或目的地
{
map[i - 2][j] += 4;
map[i - 1][j] += 1;
map[i][j] -= 5;
}
}
break;
case'a':
if (map[i][j-1] == 0 || map[i][j-1] == 3)
{
map[i][j - 1] += 5;
map[i][j] -= 5;
}
if (map[i][j - 1] == 4 || map[i][j-1] == 7)
{
if (map[i][j-2] == 0 || map[i][j - 2] == 3)
{
map[i][j - 2] += 4;
map[i][j - 1] += 1;
map[i][j] -= 5;
}
}
break;
case 's':
if (map[i + 1][j] == 0 || map[i + 1][j] == 3)
{
map[i + 1][j] += 5;
map[i][j] -= 5;
}
if (map[i + 1][j] == 4 || map[i + 1][j] == 7)
{
if (map[i + 2][j] == 0 || map[i + 2][j] == 3)
{
map[i + 2][j] += 4;
map[i + 1][j] += 1;
map[i][j] -= 5;
}
}
break;
case 'd':
if (map[i][j + 1] == 0 || map[i][j + 1] == 3)
{
map[i][j + 1] += 5;
map[i][j] -= 5;
}
if (map[i][j + 1] == 4 || map[i][j + 1] == 7)
{
if (map[i][j + 2] == 0 || map[i][j + 2] == 3)
{
map[i][j + 2] += 4;
map[i][j + 1] += 1;
map[i][j] -= 5;
}
}
break;
}
}
int main()
{
initgraph(64 * width, 64 * high); //画面大小
setbkcolor(BLACK);
cleardevice();
loadimage(&img[0],L"0.bmp"); //获取图片
loadimage(&img[1], L"1.bmp"); //注意这里的图片根据个人图片位置而选址地址
loadimage(&img[2], L"3.bmp");
loadimage(&img[3], L"4.bmp");
loadimage(&img[4], L"5.bmp");
loadimage(&img[5], L"7.bmp");
while (1)
{
show(); //显示
updateinput(); //用户输入
cleardevice();
}
_getch();
closegraph();
return 0;
}