数据结构课设——推箱子

/*推箱子游戏
* 1.二维数组
* 2.贴图技术
* 3.按键交互
* ■ 墙 1
* ☆ 目的地 3
* ★ 箱子 4
* ○ 箱子到达目的: 3+4=7
* ♀ 人 5和3+5=8 人在目的地上也是显示人
* 空格 路 0
*/
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<conio.h> //getch():按键交互用到的一个函数
#include<graphics.h> //图形库插件
//1.定义图片变量
//2.加载资源
//3.显示图片
IMAGE img[6];
int imgIndex[6] = { 0,1,3,4,5,7 };
int cas = 0;
int boxsum[2] = { 1,3 };
int map[2][8][8] = {
{1, 1, 1, 1, 1, 1, 1, 1,
1, 3, 1, 0, 1, 1, 0, 1,
1, 0, 1, 0, 0, 0, 0, 1,
1, 0, 1, 5, 4, 0, 0, 1,
1, 0, 0, 0, 0, 1, 0, 1,
1, 0, 0, 0, 1, 1, 0, 1,
1, 1, 1, 1, 1, 1, 0, 1,
1, 1, 1, 1, 1, 1, 1, 1,
} ,
{
1, 1, 1, 1, 1, 1, 1, 1,
1, 0, 0, 0, 0, 0, 0, 1,
1, 3, 1, 0, 1, 1, 3, 1,
1, 4, 0, 0, 4, 0, 3, 1,
1, 0, 1, 0, 1, 1, 4, 1,
1, 0, 0, 5, 0, 0, 0, 1,
1, 1, 0, 1, 1, 1, 0, 1,
1, 1, 1, 1, 1, 1, 1, 1,
} };
void loadResource()
{
for (int i = 0; i < 6; i++)
{
char fileName[20] = "";
sprintf(fileName, "%d.png", imgIndex[i]);
loadimage(img + i, fileName);
}
}
void drawMap()
{
int x, y;
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
x = j * 64;//到可视化窗口的一个转变
y = i * 64;
switch (map[cas][i][j])
{
case 0:
putimage(x, y, img + 0);
//printf(" ");//两个空格,表示路
break;
case 1:
putimage(x, y, img + 1);
//printf("■");//墙
break;
case 3:
putimage(x, y, img + 2);
//printf("☆");//目的地
break;
case 4:
putimage(x, y, img + 3);
//printf("★");//箱子
break;
case 5:
case 8:
putimage(x, y, img + 4);
//printf("♀");//人
break;
case 7:
putimage(x, y, img + 5);
//printf("○");//箱子推进了目的地
break;
}
}
printf("\n");
}
}
//按键交互
void keyDown()
{
//找到人
int i, j;
for (i = 0; i < 8; i++)
{
for (j = 0; j < 8; j++)
{
if (map[cas][i][j] == 5 || map[cas][i][j] == 8)
break;
}
if (map[cas][i][j] == 5 || map[cas][i][j] == 8)
break;
}
//找到人后去控制人
char userKey = _getch();
switch (userKey)
{
case'w':
case'W':
case 72: // 也可以用上下左右小键盘键码值:72, 80, 75, 77
//1.人走的原理
if (map[cas][i - 1][j] == 0 || map[cas][i - 1][j] == 3)
{
map[cas][i - 1][j] += 5;
map[cas][i][j] -= 5;
}
//2.人推箱子走
if (map[cas][i - 1][j] == 4 || map[cas][i - 1][j] == 7)
{
//箱子移动的条件
if (map[cas][i - 2][j] == 0 || map[cas][i - 2][j] == 3)
{
map[cas][i - 2][j] += 4;//箱子来了
map[cas][i - 1][j] += 1;//箱子走了,人来了
map[cas][i][j] -= 5;//人走了
}
}
break;
case's':
case'S':
case 80:
//1.人走的原理
if (map[cas][i + 1][j] == 0 || map[cas][i + 1][j] == 3)
{
map[cas][i + 1][j] += 5;
map[cas][i][j] -= 5;
}
//2.人推箱子走
if (map[cas][i + 1][j] == 4 || map[cas][i + 1][j] == 7)
{
//箱子移动的条件
if (map[cas][i + 2][j] == 0 || map[cas][i + 2][j] == 3)
{
map[cas][i + 2][j] += 4;//箱子来了
map[cas][i + 1][j] += 1;//箱子走了,人来了
map[cas][i][j] -= 5;//人走了
}
}
break;
case'a':
case'A':
case 75:
//1.人走的原理
if (map[cas][i][j - 1] == 0 || map[cas][i][j - 1] == 3)
{
map[cas][i][j - 1] += 5;
map[cas][i][j] -= 5;
}
//2.人推箱子走
if (map[cas][i][j - 1] == 4 || map[cas][i][j - 1] == 7)
{
//箱子移动的条件
if (map[cas][i][j - 2] == 0 || map[cas][i][j - 2] == 3)
{
map[cas][i][j - 2] += 4;//箱子来了
map[cas][i][j - 1] += 1;//箱子走了,人来了
map[cas][i][j] -= 5;//人走了
}
}
break;
case'd':
case'D':
case 77:
//1.人走的原理
if (map[cas][i][j + 1] == 0 || map[cas][i][j + 1] == 3)
{
map[cas][i][j + 1] += 5;
map[cas][i][j] -= 5;
}
//2.人推箱子走
if (map[cas][i][j + 1] == 4 || map[cas][i][j + 1] == 7)
{
//箱子移动的条件
if (map[cas][i][j + 2] == 0 || map[cas][i][j + 2] == 3)
{
map[cas][i][j + 2] += 4;//箱子来了
map[cas][i][j + 1] += 1;//箱子走了,人来了
map[cas][i][j] -= 5;//人走了
}
}
break;
}
}
int gameOver()
{
int count = 0;
int i, j;
for (i = 0; i < 8; i++)
for (j = 0; j < 8; j++)
if (map[cas][i][j] == 7)
count++;
return count;
}
int main()
{
initgraph(64 * 8, 64 * 8);
while (1)
{
loadResource();
drawMap();
if (gameOver() == boxsum[cas])
{
cas++;
if (cas == 2)
break;
}
keyDown();
system("cls");
}
_getch();
closegraph();
system("pause");
return 0;
}

总结:推箱子和其他题目相比个人觉得还是比较简单的,主要就是三个部分:①三维数组创建地图 ②用_getch函数进行按键交互 ③贴图技术,实现游戏页面的可视化

最后,运行结果如下:

提醒:这些图片素材需要自己找然后截成一样大小然后放在这个源程序的目录下哦~(我是都截成64*64位的)

有啥不李姐的,欢迎在评论区留言哦~😉

posted @   Athena-ydy  阅读(289)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
点击右上角即可分享
微信分享提示