C语言+EasyX编写推箱子游戏

今天编写一个简单的推箱子小游戏,只有一个关卡,想加其他关卡也很简单,只要将数组地图修改就行。
游戏中没有加上背景音乐,加也很简单,前面写打字母游戏中有用到,可以直接那代码过来,换上自己喜欢的背景音乐。
#include
#include
#include
#include
const int rows = 9, cols = 9;//初始化游戏界面大小
int i = 0, j = 0;
IMAGE box, dbox, character, blank, ball, wall, background;
//0:空地,1:墙,3:目的地,4:箱子,5:人
//初始化游戏初始界面
int map[rows][cols]=
{
	{1,1,1,1,1,0,0,0,0},
	{1,5,0,0,1,0,0,0,0},
	{1,0,4,4,1,0,1,1,1},
	{1,0,4,0,1,0,1,3,1},
	{1,1,1,0,1,1,1,3,1},
	{0,1,1,0,0,0,0,3,1},
	{0,1,0,0,0,1,0,0,1},
	{0,1,0,0,0,1,1,1,1},
	{0,1,1,1,1,1,0,0,0}
};
//打印游戏初始界面
void InitGame()
{
	int x, y;
	for (int i = 0; i < 9; i++)
	{
		for (int j = 0; j < 9; j++)
		{
			x = j * 64;
			y = i * 64;
			switch (map[i][j])
			{
			case 0:
				putimage(x,y,&blank);
				break;
			case 1:
				putimage(x,y,&wall);
				break;
			case 3:
				putimage(x,y,&ball);
				break;
			case 4:
				putimage(x,y,&box);
				break;
			case 5:
				putimage(x,y,&character);
				break;
			case 7:
				putimage(x,y,&dbox);
				break;
			case 8:
				putimage(x,y,&character);
				break;
			}
		}
	}
}
//游戏操作
void PlayGmae()
{
	char cUser;
	while (1)
	{
		//读取角色位置
		for (i = 0; i < 9; i++)
		{
			for (j = 0; j < 9; j++)
			{
				if (map[i][j] == 5 || map[i][j] == 8)
					break;
			}
			if (map[i][j] == 5 || map[i][j] == 8)
				break;
		}
		//游戏通关提示
		if (map[i][j + 1] == 7 && map[i - 1][j + 1] == 7 && map[i - 2][j + 1] == 7)
		{
			loadimage(&background, "background.jpg", rows * 64, cols * 64);
			cleardevice();
			putimage(0, 0, &background);
			settextcolor(RGB(0, 128, 255));
			setbkmode(TRANSPARENT);
			settextstyle(50, 0, "华文琥珀");
			outtextxy(120, 200, "恭喜你!过关了!");
			outtextxy(220, 260, "你真棒!");
			break;
		}
		//调用游戏初始函数
		InitGame();
		//录入用户按键
		cUser = _getch();
		//判断按键
		switch (cUser)
		{
			//上键
		case 72:
		case 'w':
		case 'W':
			if (map[i - 1][j] == 0 || map[i - 1][j] == 3)
			{
				map[i - 1][j] += 5;
				map[i][j] -= 5;
			}
			else 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 80:
		case 's':
		case 'S':
			if (map[i + 1][j] == 0 || map[i + 1][j] == 3)
			{
				map[i + 1][j] += 5;
				map[i][j] -= 5;
			}
			else 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 75:
		case 'a':
		case 'A':
			if (map[i ][j-1] == 0 || map[i ][j - 1] == 3)
			{
				map[i ][j - 1] += 5;
				map[i][j] -= 5;
			}
			else 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 77:
		case 'd':
		case 'D':
			if (map[i][j + 1] == 0 || map[i][j + 1] == 3)
			{
				map[i][j + 1] += 5;
				map[i][j] -= 5;
			}
			else 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(rows*64,cols*64);//初始画布大小
	loadimage(&background,"background.jpg",rows*64,cols*64);//设置游戏背景
	putimage(0,0,&background);
	//加载图片
	loadimage(&box, "box.jpg",  64, 64);
	loadimage(&dbox, "Darkbox.jpg",  64,  64);
	loadimage(&blank, "blank.jpg",  64,  64);
	loadimage(&wall, "Wall.jpg",  64, 64);
	loadimage(&character, "Character.jpg",  64,  64);
	loadimage(&ball, "EndPoint.jpg",  64,  64);
	//操作函数的调用
	PlayGmae();
	//防止闪屏
	system("pause");
	closegraph();
	return 0;
}
图片资源:
白色背景,白块64*64
         

你可能感兴趣的:(C语言+EasyX编写推箱子游戏)