C/C++推箱子游戏

推箱子游戏

  • 项目名称:推箱子
  • 所用知识点:C/C++ 基础知识
  • 所用工具:vs2013&Easyx

程序思路分析
在这里插入图片描述
(效果图)
在这里插入图片描述

程序思路:
第一步:创建窗口

#include<graphics.h>
#include<stdio..h>
int main()
{
initgraph(400, 400);//创建400px * 400px的窗口
system ("pause");
return 0;
}

第二步:贴图(贴图三部曲:定义图片,加载图片,显示图片)

#include<stdio.h>
#include<graphics.h>
#include<conio.h>
int main()
{
initgraph(400, 400);//创建400px * 400px的窗口
IMAGE  b;//定义图片
loadimage(&b, "b.bmp", 50, 50);//加载图片
putimage(j * 50, i * 50, &b);//显示图片
	
system ("pause");
return 0;
}`


第三步:箱子的移动(图片的移动)

{
	int x, y;//表示人物所在位置的下标
	for (int j = 0; j < 8; j++)
	{
		for (int i = 0; i < 8; i++)
		{
			if (Map[j][i] == 5 || Map[j][i] == 8)
			{

				
					x = j;
					y = i;

				
					
			}
		}
	}

	char key = _getch();
	//Sleep(30);
	switch (key)//获取按键
	{

	
		//向上
	case 72:
	case 'w':
	case 'W':
		if (Map[x - 1][y] == 0 || Map[x - 1][y] == 3)
		{
			Map[x - 1][y] += 5;
			Map[x][y] -= 5;
		}
		else if (Map[x - 1][y] == 4 || Map[x - 1][y] == 7)
			if (Map[x - 2][y] == 0 || Map[x - 1][y] == 3)
			{
				Map[x - 2][y] += 4;
				Map[x - 1][y] += 1;
				Map[x][y] -= 5;
			}

		break;
	case 80:
	case 's':
	case 'S':
		if (Map[x + 1][y] == 0 || Map[x + 1][y] == 3)
		{
			Map[x + 1][y] += 5;
			Map[x][y] -= 5;
		}
		else if (Map[x + 1][y] == 4 || Map[x + 1][y] == 7)
			if (Map[x + 2][y] == 0 || Map[x + 1][y] == 3)
			{
				Map[x + 2][y] += 4;
				Map[x + 1][y] += 1;
				Map[x][y] -= 5;
			}
		break;
	case 77:
	case 'd':
	case 'D':
		if (Map[x][y + 1] == 0 || Map[x][y + 1] == 3)
		{
			Map[x][y + 1] += 5;
			Map[x][y] -= 5;
		}
		else if (Map[x][y + 1] == 4 || Map[x][y + 1] == 7)
			if (Map[x][y + 2] == 0 || Map[x][y + 1] == 3)
			{
				Map[x][y + 2] += 4;
				Map[x][y + 1] += 1;
				Map[x][y] -= 5;
			}
		break;
	case 75:
	case 'a':
	case 'A':
		if (Map[x][y - 1] == 0 || Map[x][y - 1] == 3)
		{
			Map[x][y - 1] += 5;
			Map[x][y] -= 5;
		}
		else if (Map[x][y - 1] == 4 || Map[x][y - 1] == 7)
			if (Map[x][y - 2] == 0 || Map[x][y - 1] == 3)
			{
				Map[x][y - 2] += 4;
				Map[x][y - 1] += 1;
				Map[x][y] -= 5;
			}
		break;
	}

程序的源代码


#include<stdio.h>
#include<graphics.h>
#include<conio.h>
IMAGE  a, aby, b, c, d, dby, e, f;
void InitImg()
{

	//1.加载背景
	loadimage(&f, "f.jpg", 400, 400);
	//加载小鸟
	loadimage(&a, "a.bmp", 50, 50);
	loadimage(&aby, "aby.bmp", 50, 50);

	loadimage(&b, "b.bmp", 50, 50);
	loadimage(&c, "c.bmp", 50, 50);
	loadimage(&d, "d.bmp", 50, 50);
	loadimage(&dby, "dby.bmp", 50, 50);
	loadimage(&e, "e.bmp", 50, 50);
}
int Map[8][8] = {
	{ 0, 0, 1, 1, 1, 0, 0, 0 },
	{ 0, 0, 1, 3, 1, 0, 0, 0 },
	{ 0, 0, 1, 0, 1, 1, 1, 1 },
	{ 1, 1, 1, 4, 0, 4, 3, 1 },
	{ 1, 3, 0, 4, 5, 1, 1, 1 },
	{ 1, 1, 1, 1, 4, 1, 0, 0 },
	{ 0, 0, 0, 1, 3, 1, 0, 0 },
	{ 0, 0, 0, 1, 1, 1, 0, 0 },

};
void DrawMap()
{

	putimage(0, 0, &f);
	int i, j;
	for (i = 0; i < 8; i++)
	{
		for (j = 0; j < 8; j++) {
			switch (Map[i][j]) {
			case 0:printf("  ");

				break;
			case 1:printf("@");
				putimage(j * 50, i * 50, &b);

				break;
			case 3:printf("#");
				putimage(j * 50, i * 50, &d, SRCAND);
				putimage(j * 50, i * 50, &dby, SRCPAINT);

				break;
			case 4:printf("$");
				putimage(j * 50, i * 50, &c);
				break;
			case 5:printf("%");
				putimage(j * 50, i * 50, &a, SRCAND);
				putimage(j * 50, i * 50, &aby, SRCPAINT);

				break;
				case 7:
					printf("^");
					putimage(j * 50, i * 50, &e);
					break;

			}

		}
		printf("\n");



	}
}
void key_Down()
{
	int x, y;//表示人物所在位置的下标
	for (int j = 0; j < 8; j++)
	{
		for (int i = 0; i < 8; i++)
		{
			if (Map[j][i] == 5 || Map[j][i] == 8)
			{

				
					x = j;
					y = i;

				
					
			}
		}
	}

	char key = _getch();
	//Sleep(30);
	switch (key)//获取按键
	{

	
		//向上
	case 72:
	case 'w':
	case 'W':
		if (Map[x - 1][y] == 0 || Map[x - 1][y] == 3)
		{
			Map[x - 1][y] += 5;
			Map[x][y] -= 5;
		}
		else if (Map[x - 1][y] == 4 || Map[x - 1][y] == 7)
			if (Map[x - 2][y] == 0 || Map[x - 1][y] == 3)
			{
				Map[x - 2][y] += 4;
				Map[x - 1][y] += 1;
				Map[x][y] -= 5;
			}

		break;
	case 80:
	case 's':
	case 'S':
		if (Map[x + 1][y] == 0 || Map[x + 1][y] == 3)
		{
			Map[x + 1][y] += 5;
			Map[x][y] -= 5;
		}
		else if (Map[x + 1][y] == 4 || Map[x + 1][y] == 7)
			if (Map[x + 2][y] == 0 || Map[x + 1][y] == 3)
			{
				Map[x + 2][y] += 4;
				Map[x + 1][y] += 1;
				Map[x][y] -= 5;
			}
		break;
	case 77:
	case 'd':
	case 'D':
		if (Map[x][y + 1] == 0 || Map[x][y + 1] == 3)
		{
			Map[x][y + 1] += 5;
			Map[x][y] -= 5;
		}
		else if (Map[x][y + 1] == 4 || Map[x][y + 1] == 7)
			if (Map[x][y + 2] == 0 || Map[x][y + 1] == 3)
			{
				Map[x][y + 2] += 4;
				Map[x][y + 1] += 1;
				Map[x][y] -= 5;
			}
		break;
	case 75:
	case 'a':
	case 'A':
		if (Map[x][y - 1] == 0 || Map[x][y - 1] == 3)
		{
			Map[x][y - 1] += 5;
			Map[x][y] -= 5;
		}
		else if (Map[x][y - 1] == 4 || Map[x][y - 1] == 7)
			if (Map[x][y - 2] == 0 || Map[x][y - 1] == 3)
			{
				Map[x][y - 2] += 4;
				Map[x][y - 1] += 1;
				Map[x][y] -= 5;
			}
		break;
	}



	}
	int main()
	{


		initgraph(400, 400);
		InitImg();
		DrawMap();
		while (1)
		{


			
			DrawMap();
			key_Down();
			system("cls");
		}
		getchar();
		system("pause");

	}



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

code.xinxixue.top

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值