项目: 推箱子图形化游戏 C++ / C

Posted 辉小歌

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了项目: 推箱子图形化游戏 C++ / C相关的知识,希望对你有一定的参考价值。

码文不易,望点赞、收藏、留言、打赏随意

最终的游戏效果

代码

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<graphics.h>
#include<Windows.h>
#include<conio.h>
#include<MMSystem.h>
#pragma comment(lib,"Winmm.lib")
//定义图形变量
//加载图片
//显示图片
#define n 6    //关卡数 
#define m 10   //地图大小 
int index=0;     //当前关卡 
IMAGE img[6];定义图形变量
int imgIndex[6]={0,1,3,4,5,7};
int map[n][m][m]={
 {
	1,1,1,1,1,1,1,1,1,1,
	1,0,0,0,1,0,0,0,0,1,
	1,0,1,3,0,0,1,3,1,1,
	1,0,0,0,0,4,0,0,0,1,
	1,1,0,1,5,0,1,0,0,1,
	1,0,0,0,0,1,0,0,0,1,
	1,1,4,1,0,0,1,0,0,1,
	1,0,0,1,4,0,0,0,3,1,
	1,0,0,0,0,0,0,1,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,1,0,0,0,0,1,
	1,0,1,0,0,0,1,0,1,1,
	1,0,0,0,1,0,4,4,0,1,
	1,3,3,1,0,0,1,0,1,1,
	1,3,3,0,0,4,0,4,0,1,
	1,1,0,1,0,0,0,0,0,1,
	1,0,0,1,0,1,1,5,1,1,
	1,0,0,0,0,1,0,1,0,1,
	1,1,1,1,1,1,1,1,1,1,
 },
 {
	1,1,1,1,1,1,1,1,1,1,
	1,3,0,1,0,0,0,0,3,1,
	1,0,1,0,0,0,1,0,0,1,
	1,0,0,0,1,4,0,4,0,1,
	1,0,0,1,0,0,1,0,0,1,
	1,0,0,0,3,4,0,4,0,1,
	1,1,0,1,0,0,0,1,0,1,
	1,0,0,1,4,0,1,5,1,1,
	1,3,0,0,0,0,0,0,3,1,
	1,1,1,1,1,1,1,1,1,1,
 },
{
	1,1,1,1,1,1,1,1,1,1,
	1,0,1,3,0,1,1,0,3,1,
	1,0,3,1,0,0,1,0,0,1,
	1,0,0,0,0,0,0,0,0,1,
	1,0,4,0,3,0,4,3,1,1,
	1,0,1,1,0,0,0,1,0,1,
	1,0,4,0,0,4,3,0,0,1,
	1,1,0,0,4,0,1,5,0,1,
	1,0,0,3,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,3,0,0,3,0,3,0,0,1,
	1,0,0,0,1,4,1,1,0,1,
	1,0,0,3,0,0,0,3,0,1,
	1,0,4,1,3,0,0,1,0,1,
	1,0,4,0,0,1,4,0,0,1,
	1,0,0,1,0,0,0,1,1,1,
	1,1,0,4,0,1,4,0,1,1,
	1,5,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,5,1,0,3,0,0,3,0,1,
	1,7,0,4,0,0,1,0,0,1,
	1,0,0,0,0,1,0,0,0,1,
	1,0,1,0,4,0,4,0,0,1,
	1,0,0,1,0,0,0,1,1,1,
	1,1,0,0,4,0,1,1,0,1,
	1,1,0,1,0,0,0,0,0,1,
	1,0,3,1,0,0,0,3,0,1,
	1,1,1,1,1,1,1,1,1,1,
 } 
};
void loadResoure()
//函数作用:加载图片
{
	int i=0;
	for(i=0;i<6;i++)
	{
		char filename[20]="";
		sprintf(filename,"%d.bmp",imgIndex[i]);
		loadimage(img+i,filename);//加载图片
		//如果错误,就将项目的属性里的-》常规-》字符串设置为多字符
	}
}
void printf_map(int map[][m][m])
//函数作用:初始化地图
{
	int i,j;
	int x,y;
	for(i=0;i<m;i++)
	{
		
		for(j=0;j<m;j++)
		{
			x=j*64;//
			y=i*64;//
			switch(map[index][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("\\n");
	}
}
//按键交互
void keydown()
{
	int i,j;
	//找到人的位置
	for(i=0;i<m;i++)
	{
		for(j=0;j<m;j++)
		{
			if(map[index][i][j]==5||map[index][i][j]==8)
			{
				break;
			}
		}
		if(map[index][i][j]==5||map[index][i][j]==8)
		{
			break;
		}
	}
	char userkey=_getch();
	switch(userkey)
	{
		case'w':
		case'W':
		case 72://小键盘的键值
			//只是人走
			if(map[index][i-1][j]==0||map[index][i-1][j]==3)
			{
				map[index][i-1][j]+=5;//移动到的位置加5
				map[index][i][j]-=5;以上是关于项目: 推箱子图形化游戏 C++ / C的主要内容,如果未能解决你的问题,请参考以下文章

项目: 打字母游戏图形化C++ / C

c语言之推箱子功能汇总

C语言沈梦溪推箱子小游戏

C语言沈梦溪推箱子小游戏

项目: 推箱子游戏c/c++

项目: 用C语言写一个图形化的音乐播放器 C++ / C