#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<mmsystem.h>//媒体音乐
#pragma comment(lib,"winmm.lib")
#define sizes 60//正方形图片的像素大小
#define point 5//5个目的地
//0背景//1墙//2箱子//3空地//4人//5目的地
//===============================地图一 ===============================-=
#define rows 7//行
#define cols 10//列
int rowNum = 3, colNum = 8;//地图一小人的位置
int mapIndex[rows][cols] =
{
0,0,0,1,1,1,1,1,1,0,
0,1,1,1,3,3,3,3,1,0,
1,1,5,3,2,1,1,3,1,1,
1,5,5,2,3,2,3,3,4,1,
1,5,5,3,2,3,2,3,1,1,
1,1,1,1,1,1,3,3,1,0,
0,0,0,0,0,1,1,1,1,0
};
//==================================地图二 ===================================
/*
#define rows 10//行
#define cols 12//列
int rowNum = 8, colNum = 4;//地图二小人的位置
int mapIndex[rows][cols] =
{
0,0,0,0,0,0,0,0,1,1,1,1,
0,0,0,0,0,0,1,1,1,3,3,1,
1,1,1,1,1,1,1,3,3,3,3,1,
1,3,3,3,2,3,3,3,3,3,3,1,
1,3,3,3,2,1,1,1,3,1,1,1,
1,1,1,5,2,5,5,5,3,5,1,0,
0,0,1,3,2,1,3,3,3,3,1,0,
0,0,1,3,2,3,1,1,1,1,1,0,
0,0,1,3,4,3,1,0,0,0,0,0,
0,0,1,1,1,1,1,0,0,0,0,0
};
*/
IMAGE backimg;
IMAGE wall;
IMAGE box;
IMAGE grass;
IMAGE end;
IMAGE wpeople;
IMAGE apeople;
IMAGE speople;
IMAGE dpeople;
int a[point][2] = { 0 }; //五个目的地点,定义一个目的地的数组下标
int b[point][2] = { 0 };//五个目的地点,定义一个目的地的坐标数组
int x = colNum * sizes;
int y = rowNum * sizes;
void LoadImg()
{
/* loadimage(&backimg, "res/background.jpg", size, size);
loadimage(&wall, "res/wall.jpg", size, size);
loadimage(&box, "res/box.jpg", size, size);
loadimage(&grass, "res/grass.jpg", size, size);
loadimage(&end, "res/end.jpg", size, size);
loadimage(&wpeople, "res/toppeople.jpg", size, size);
loadimage(&speople, "res/downpeople.jpg", size, size);
loadimage(&apeople, "res/leftpeople.jpg", size, size);
loadimage(&dpeople, "res/rightpeople.jpg", size, size);*/
loadimage(&backimg, _T("IMAGE"),_T("background"), sizes, sizes);
loadimage(&wall, _T("IMAGE"),_T("wall"), sizes, sizes);
loadimage(&box, _T("IMAGE"),_T("box"), sizes, sizes);
loadimage(&grass, _T("IMAGE"),_T("grass"), sizes, sizes);
loadimage(&end, _T("IMAGE"),_T("end"), sizes, sizes);
loadimage(&wpeople, _T("IMAGE"),_T("toppeople"), sizes, sizes);
loadimage(&speople, _T("IMAGE"),_T("downpeople"), sizes, sizes);
loadimage(&apeople, _T("IMAGE"),_T("leftpeople"), sizes, sizes);
loadimage(&dpeople, _T("IMAGE"),_T("rightpeople"), sizes, sizes);
}
/*================================贴图的原理============================
(通过改变数组中图片对应的数字来达到移动贴图的效果)
1、人每走一步,就要贴一次人的图片,同时要改变人在mapIndex数组中对应的值:
空地->人
人->空地
2、每走一步时,对走的方向进行判断,是否是墙、箱子,同时改变mapIndex数组中对应的值。
如果人前面一格有箱子,则
箱子前的空地->箱子,
箱子->人
人->空地
3、如果前面是墙,或者前面有两个箱子,则continue掉循环,人不动,不贴图,不改值。
4、如果小人走是目的地,判断方法为:(通过判断图片对应坐标),
判断小人现在的x,y坐标是否为目的地的坐标,是则画目的地的图:
步骤: (1)获取地图数组里数值为5的坐标,塞到二维数组b[5][2]里;
(2)判断小人当前坐标是否等于b数组中的x,y坐标,等于则只需要在小人走过的地方,贴目的地图片;
(3)改变mapIndex地图数组中对应的值
注意:目的地,贴完图后不需要赋值5。
5、判断游戏结束,
步骤:(1)定义一个二维数组a[5][2],记录目的地坐标;
(2)判断目的地坐标中值是否等于箱子的值2,如果全都等于2,则游戏结束,播放BGM提示游戏成功。
*/
void InitGame()
{
LoadImg();
for (int rowNum = 0; rowNum < rows; rowNum++)//绘制图片
{
for (int colNum = 0; colNum < cols; colNum++)
{
switch (mapIndex[rowNum][colNum])
{
case 0://背景
putimage(colNum * sizes, rowNum * sizes, &backimg);
break;
case 1://墙壁
putimage(colNum * sizes, rowNum * sizes, &wall);
break;
case 2://箱子
putimage(colNum * sizes, rowNum * sizes, &box);
break;
case 3://空地
putimage(colNum * sizes, rowNum * sizes, &grass);
break;
case 4://人物
putimage(colNum * sizes, rowNum * sizes, &speople);
break;
case 5://目的地
putimage(colNum * sizes, rowNum * sizes, &end);
break;
case 6:
default:
break;
}
}
}
}
void judge(int x, int y)//判断x,y是否属于目的地坐标
{
//判断x,y是否满足在目的地的坐标,,,满足则画目的地的图
if ((x == b[0][0] && y == b[0][1]) ||
(x == b[1][0] && y == b[1][1]) ||
(x == b[2][0] && y == b[2][1]) ||
(x == b[3][0] && y == b[3][1]) ||
(x == b[4][0] && y == b[4][1]) //五个目的地
)
{
putimage(x, y, &end);
}
else
{
putimage(x, y, &grass);
}
}
//0背景//1墙//2箱子//3空地//4人//5目的地
void playGame()
{
char input;
putimage(x, y, sizes, sizes, &speople, 0, 0);
while (1)
{
//0背景//1墙//2箱子//3空地//4人//5目的地
if (kbhit())//判断是否有输入
{
input = getch();
if (input == 's')//y变,坐标加
{
if (mapIndex[rowNum + 1][colNum] == 1 ||
(mapIndex[rowNum + 2][colNum] == 2 && mapIndex[rowNum + 1][colNum] == 2) ||
(mapIndex[rowNum + 2][colNum] == 1 && mapIndex[rowNum + 1][colNum] == 2)
)
{//人不动的三种情况:前面两个箱;前面箱子和墙;前面墙
continue;
}
else
{//人箱都动的情况
if (mapIndex[rowNum + 1][colNum] == 2)//else则都动,
{
putimage(x, y + sizes + sizes, &box);
mapIndex[rowNum][colNum] = 3;//人变空地 ,数值4变3
mapIndex[rowNum + 1][colNum] = 4;//箱变人 ,数值2变4
mapIndex[rowNum + 2][colNum] = 2;//空地变箱 ,数值3变2
PlaySound(TEXT("res/箱子动.wav"), NULL, SND_SYNC | SND_FILENAME | SND_ASYNC);
}
else
{//只有人动的情况
mapIndex[rowNum + 1][colNum] = 4;//空地变人 ,数值3变4
mapIndex[rowNum][colNum] = 3;//人变空地 ,数值4变3
PlaySound(TEXT("res/箭击中3.wav"), NULL, SND_SYNC | SND_FILENAME | SND_ASYNC);
}
putimage(x, y + sizes, &speople);
judge(x, y);
y = y + sizes;
rowNum++;
}
}
//=========后面按键同s按键的方法=========//
else if (input == 'w')//y变,减
{
if (mapIndex[rowNum - 1][colNum] == 1 ||
(mapIndex[rowNum - 2][colNum] == 2 && mapIndex[rowNum - 1][colNum] == 2) ||
(mapIndex[rowNum - 2][colNum] == 1 && mapIndex[rowNum - 1][colNum] == 2)
)//人箱不动
{
continue;
}
//人箱移动
else
{
if (mapIndex[rowNum - 1][colNum] == 2)//人箱都动
{
putimage(x, y - sizes - sizes, &box);
mapIndex[rowNum][colNum] = 3;//人变空地 4变3
mapIndex[rowNum - 1][colNum] = 4;//箱变人 2变4
mapIndex[rowNum - 2][colNum] = 2;//空地变箱 3变2
PlaySound(TEXT("res/箱子动.wav"), NULL, SND_SYNC | SND_FILENAME | SND_ASYNC);
}
else//人动
{
mapIndex[rowNum][colNum] = 3;//人变空地 4变3
mapIndex[rowNum - 1][colNum] = 4;//空地变人 3变4
PlaySound(TEXT("res/箭击中3.wav"), NULL, SND_SYNC | SND_FILENAME | SND_ASYNC);
}
putimage(x, y - sizes, &wpeople);
judge(x, y);
y = y - sizes;
rowNum--;
}
}
else if (input == 'a')//x变,坐标减
{
if (mapIndex[rowNum][colNum - 1] == 1 ||
(mapIndex[rowNum][colNum - 2] == 2 && mapIndex[rowNum][colNum - 1] == 2) ||
(mapIndex[rowNum][colNum - 2] == 1 && mapIndex[rowNum][colNum - 1] == 2)
)//人箱不动
{
continue;
}
else
{
if (mapIndex[rowNum][colNum - 1] == 2)//人箱都动
{
putimage(x - sizes - sizes, y, &box);//画箱子
mapIndex[rowNum][colNum - 2] = 2;
mapIndex[rowNum][colNum - 1] = 4;
mapIndex[rowNum][colNum] = 3;
PlaySound(TEXT("res/箱子动.wav"), NULL, SND_SYNC | SND_FILENAME | SND_ASYNC);
}
else //人动
{
mapIndex[rowNum][colNum] = 3;
mapIndex[rowNum][colNum - 1] = 4;
PlaySound(TEXT("res/箭击中3.wav"), NULL, S
C语言图形界面的推箱子easyx版(VC6.0)
需积分: 49 79 浏览量
2018-10-17
23:55:53
上传
评论 8
收藏 4.67MB ZIP 举报
盼盼法式小面包0
- 粉丝: 48
- 资源: 7
最新资源
- 杨辉三角(Pascal's Triangle)是一个在数学中非常著名的数列,它以一种特定的模式排列数字,内容以5种代码实例实现
- yolov5数据集:CT图像肾脏、结石检测(2类别,包含训练集、验证集)
- 电力102规约请求报文实录
- RTL8723 linux+安卓驱动 2020版本
- ISO 26262 道路车辆-功能安全-系统级产品开发
- 数据挖掘期末题 选择填空简答
- EKF在CTRV模型上的建模推导
- 用于:https://laoshifu.blog.csdn.net/article/details/138191428 多层创建
- 电力102规约(电能累计量的传输规约)培训
- 「常用」E9外部流程接口
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈