C语言沈梦溪推箱子小游戏
Posted 流楚丶格念
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C语言沈梦溪推箱子小游戏相关的知识,希望对你有一定的参考价值。
效果图
项目链接
https://download.csdn.net/download/weixin_45525272/21106512
代码
#include<stdio.h>
#include<conio.h>
#include<graphics.h> //图形库头文件
#pragma comment(lib,"winmm.lib") //多媒体库
IMAGE img[8];//图片数组
int x1, y1; //定义主角坐标
int loop=0;//重置开关
//函数申明
void Find(int map[9][9]);
void Print(int map[9][9]);
void Princes(int x, int y);
void Boom(int x, int y);
void Wolf(int x, int y);
void Bomb(int x, int y);
void Man(int x, int y);
void Blank(int x, int y);
int Victory(int map[9][9]);
void Move(int map[9][9], int x, int y);
void GameMap();
int PlayGame(int map[9][9]);
void Loading();
void GiniGame();
void GiniGame()//窗体初始化
{
//创建窗口
initgraph(540, 540);
loadimage(NULL, "./游戏素材/图片素材/317760.jpg");// ./ 表示当前路径
Sleep(3000);
cleardevice();//刷新界面
}
void Loading()
{
loadimage(&img[0], "./游戏素材/图片素材/0.jpg");
loadimage(&img[1], "./游戏素材/图片素材/1.jpg");
loadimage(&img[2], "./游戏素材/图片素材/2.jpg");
loadimage(&img[3], "./游戏素材/图片素材/3.jpg");
loadimage(&img[4], "./游戏素材/图片素材/4.jpg");
loadimage(&img[5], "./游戏素材/图片素材/5.jpg");
loadimage(&img[6], "./游戏素材/图片素材/6.jpg");
loadimage(&img[7], "./游戏素材/图片素材/7.jpg");
//mciSendString("open ./游戏素材/音乐素材/别看我只是一只羊.mp3 alias BGM",0,0,0);
//mciSendString("play BGM", 0, 0, 0);
}
//3.游戏关卡
void GameMap()
{
//3.1导入所有地图
int gamemap[5][9][9] =
{
{{4, 4, 6, 6, 6, 4, 4, 4, 4 },
{4, 4, 6, 3, 6, 4, 4, 4, 4 },
{4, 4, 6, 0, 6, 6, 6, 6, 4 },
{6, 6, 6, 2, 0, 2, 3, 6, 4 },
{6, 3, 0, 2, 1, 6, 6, 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 3, 0, 6, 4 },
{6, 0, 0, 2, 2, 0, 6, 6, 4 },
{6, 6, 6, 0, 6, 3, 6, 4, 4 },
{4, 4, 6, 0, 0, 0, 6, 4, 4 },
{4, 4, 6, 6, 6, 6, 6, 4, 4 },
{4, 4, 4, 4, 4, 4, 4, 4, 4 },}
};
int map[9][9]; //加载当前地图
for (int i = 0; i < 5; i++)
{
do
{
loop = 0;
for (int j = 0; j < 9; j++)
{
for (int k = 0; k < 9; k++)
{
//3.2获取当前地图
map[j][k] = gamemap[i][j][k];
}
}
//3.3玩游戏的逻辑
PlayGame(map);
} while (loop);
}
}
//3.3玩游戏的逻辑
int PlayGame(int map[9][9])
{
Print(map); //3.3.1游戏界面贴图调用
Find(map); //3.3.2游戏角色定位调用
//角色操作
do
{
switch (_getch())
{
case 72: Move(map, -1, 0); break; //向上
case 75: Move(map, 0, -1); break; //向左
case 77: Move(map, 0, 1); break; //向右
case 80: Move(map, 1, 0); break; //向下
case 27: exit(0); break; //退出
case 32: loop = 1; return 0; //空格恢复原状态
}
} while (Victory(map)); //
Princes(60 * x1, 60 * y1);//胜利后贴美羊羊庆祝通关
MessageBox(NULL,"成功击败狗子们" , "恭喜通关", MB_SYSTEMMODAL);//MB_SYSTEMMODAL使得弹框最前置
return 0;
}
void Move(int map[9][9], int x, int y)
{
//喜洋洋前面的位置是否是空地或者是狗子
if (map[x1+x][y1+y]==0|| map[x1 + x][y1 + y] == 3)
{
map[x1][y1]--;//使得数字数组里的喜洋洋变为空白
map[x1 + x][y1 + y]++;//使得数字数组里的空白变为喜洋洋对应数值
for (int i = 0; i < 60; i++)
{
Blank(60*x1+i*x,60*y1+i*y);//在移动前喜洋洋位置重绘空白
Man(60 * x1 + (i + 1)*x, 60 * y1 + (i + 1)*y);//在移动后喜洋洋位置重绘空
}
if (map[x1][y1]==3)//c重绘狗子
{
Wolf(60 * x1, 60 * y1);
}
//数值数组位置移动
x1 += x;
y1 += y;
}
else if ((map[x1 + x][y1 + y]==2|| map[x1 + x][y1 + y]==5以上是关于C语言沈梦溪推箱子小游戏的主要内容,如果未能解决你的问题,请参考以下文章