推箱子小游戏

利用vs 2019和easyx图形库完成推箱子小游戏

需要的工具:
  • Win 10
  • VS 2019
  • EasyX
main.cpp
#include<graphics.h>
#include<iostream>
#include<conio.h>
#include<iomanip>
#include<windows.h>
#define Interval 100
using namespace std;

int map[3][8][8] =
{
{
	{0,0,1,1,1,0,0,0},
	{0,0,1,4,1,0,0,0},
	{0,0,1,0,1,0,0,0},
	{0,1,1,3,1,1,1,1},
	{0,1,4,3,0,3,4,1},
	{0,1,1,1,3,1,1,1},
	{0,0,0,1,4,1,0,0},
	{0,0,0,1,1,1,0,0}
},
{
	{ 1,1,1,1,1,1,1,1.},
	{ 1,0,4,0,0,0,0,1.},
	{ 1,0,3,0,0,0,0,1.},
	{ 1,0,3,0,0,0,4,1.},
	{ 1,0,0,0,0,0,0,1.},
	{ 1,0,0,0,3,0,0,1.},
	{ 1,4,0,3,0,0,4,1.},
	{ 1,1,1,1,1,1,1,1.},
},
{
	{ 0,0,0,0,0,0,0,0.},
	{ 0,1,1,1,1,1,1,0.},
	{ 0,1,4,1,1,1,1,0.},
	{ 0,1,0,0,3,0,1,0.},
	{ 0,1,0,1,0,0,1,0.},
	{ 0,1,0,0,0,0,1,0.},
	{ 0,1,1,1,1,1,1,0.},
	{ 0,0,0,0,0,0,0,0.},
}
};
int Pmap[3][8][8];
IMAGE image[6];
int x = 4, y = 4, num = 0, n = 0, choose = 0;

void Menu()
{
	cout << "恭喜你,通过了本关卡" << endl;
	cout << "请输入您的选择" << endl;
	cout << "1.开始下一关" << endl;
	cout << "2.退出游戏" << endl;
	cout << "3.重玩本关卡" << endl;
}
void Setimage()
{
	loadimage(&image[0], "nbsp.png", 100, 100);
	loadimage(&image[1], "wall.png", 100, 100);
	loadimage(&image[2], "user.png", 100, 100);
	loadimage(&image[3], "boxs.png", 100, 100);
	loadimage(&image[4], "goal.png", 100, 100);
	loadimage(&image[5], "wins.png", 100, 100);
}
void GetsourceMap()
{
	for (size_t i = 0; i < 3; i++)
	{

		for (size_t j = 0; j < 8; j++)
		{
			for (size_t k = 0; k < 8; k++)
			{
				Pmap[i][j][k] = map[i][j][k];
			}
		}
	}
}
void Drawmap()
{
	map[n][x][y] = 2;

	for (int i = 0; i < 8; i++)
	{
		for (int j = 0; j < 8; j++)
		{
			switch (map[n][i][j])
			{
			case 0:					//空白
				cout << setw(2) << " ";
				break;
			case 1:					//墙壁
				cout << setw(2) << "*";
				break;
			case 2:					//人物
				cout << setw(2) << "O";
				break;
			case 3:					//箱子
				cout << setw(2) << "?";
				break;
			case 4:					//目的地
				cout << setw(2) << "&";
				break;
			case 5:
				cout << setw(2) << "!";
				break;
			default:
				break;
			}
		}
		cout << endl;
	}
}
void Draweasyxmap()
{
	map[n][x][y] = 2;

	for (int i = 0; i < 8; i++)
	{
		for (int j = 0; j < 8; j++)
		{
			switch (map[n][i][j])
			{
			case 0:					//空白
				putimage(j * Interval, i * Interval, &image[0]);
				break;
			case 1:					//墙壁
				putimage(j * Interval, i * Interval, &image[1]);
				break;
			case 2:					//人物
				putimage(j * Interval, i * Interval, &image[2]);
				break;
			case 3:					//箱子
				putimage(j * Interval, i * Interval, &image[3]);
				break;
			case 4:					//目的地
				putimage(j * Interval, i * Interval, &image[4]);
				break;
			case 5:
				putimage(j * Interval, i * Interval, &image[5]);
				break;
			default:
				break;
			}
		}
		cout << endl;
	}
}
void GetControl()
{
	switch (_getch())
	{
	case 75:
	case 'a':
		if (map[n][x][y - 1] == 1 || map[n][x][y - 1] == 5 || (map[n][x][y - 1] == 3 && map[n][x][y - 2] == 1) || (map[n][x][y - 1] == 3 && map[n][x][y - 2] == 3))
		{
			break;
		}
		else if (map[n][x][y - 1] == 3 && map[n][x][y - 2] != 4)
		{
			map[n][x][y] = 0;
			map[n][x][y - 2] = map[n][x][y - 1];
			y -= 1;
		}
		else if (map[n][x][y - 1] == 0)
		{
			map[n][x][y] = 0;
			y -= 1;
		}
		else if (map[n][x][y - 1] == 3 && map[n][x][y - 2] == 4)
		{
			map[n][x][y] = 0;
			map[n][x][y - 2] = 5;
			num -= 1;
			y -= 1;
		}
		break;

	case 80:
	case 's':
		if (map[n][x + 1][y] == 1 || map[n][x + 1][y] == 5 || (map[n][x + 1][y] == 3 && map[n][x + 2][y] == 1) || (map[n][x + 1][y] == 3 && map[n][x + 2][y] == 3))
		{
			break;
		}
		else if (map[n][x + 1][y] == 3 && map[n][x + 2][y] != 4)
		{
			map[n][x][y] = 0;
			map[n][x + 2][y] = map[n][x + 1][y];
			x += 1;
		}
		else if (map[n][x + 1][y] == 0)
		{
			map[n][x][y] = 0;
			x += 1;
		}
		else if (map[n][x + 1][y] == 3 && map[n][x + 2][y] == 4)
		{
			map[n][x][y] = 0;
			map[n][x + 2][y] = 5;
			num -= 1;
			x += 1;
		}
		break;

	case 77:
	case 'd':
		if (map[n][x][y + 1] == 1 || map[n][x][y + 1] == 5 || (map[n][x][y + 1] == 3 && map[n][x][y + 2] == 1) || (map[n][x][y + 1] == 3 && map[n][x][y + 2] == 3))
		{
			break;
		}
		else if (map[n][x][y + 1] == 3 && map[n][x][y + 2] != 4)
		{
			map[n][x][y] = 0;
			map[n][x][y + 2] = map[n][x][y + 1];
			y = y + 1;
		}
		else if (map[n][x][y + 1] == 0)
		{
			map[n][x][y] = 0;
			y = y + 1;
		}
		else if (map[n][x][y + 1] == 3 && map[n][x][y + 2] == 4)
		{
			map[n][x][y] = 0;
			map[n][x][y + 2] = 5;
			num -= 1;
			y = y + 1;
		}
		break;

	case 72:
	case 'w':
		if (map[n][x - 1][y] == 1 || map[n][x - 1][y] == 5 || (map[n][x - 1][y] == 3 && map[n][x - 2][y] == 1) || (map[n][x - 1][y] == 3 && map[n][x - 2][y] == 3))
		{
			break;
		}
		else if (map[n][x - 1][y] == 3 && map[n][x - 2][y] != 4)
		{
			map[n][x][y] = 0;
			map[n][x - 2][y] = map[n][x - 1][y];
			x -= 1;
		}
		else if (map[n][x - 1][y] == 0)
		{
			map[n][x][y] = 0;
			x -= 1;
		}
		else if (map[n][x - 1][y] == 3 && map[n][x - 2][y] == 4)
		{
			map[n][x][y] = 0;
			map[n][x - 2][y] = 5;
			num -= 1;
			x -= 1;
		}
		break;

	default:
		break;
	}
}
void Getnum()
{
	num = 0;

	for (size_t i = 0; i < 8; i++)
	{
		for (size_t j = 0; j < 8; j++)
		{
			if (map[n][i][j] == 3)
			{
				num += 1;
			}
		}
	}
}
void GamePlay()
{
	Getnum();

	while (num)
	{
		Draweasyxmap(); Drawmap();
		GetControl();
		system("cls");
	};
	Draweasyxmap();
}

int main()
{
	Setimage();
	initgraph(800, 800, 1);
	GetsourceMap();
	GamePlay();
	while (1)
	{
		cin >> choose;
		system("cls");

		switch (choose)
		{
		case 1:
			n += 1; x = 4; y = 4;
			if (n == 3)
			{
				cout << "您已经通过了所有关卡" << endl;
				return 0;
			}
			GamePlay();
			break;
		case 2:
			return 0;
		case 3:
			x = 4; y = 4;
			for (size_t i = 0; i < 8; i++)
			{
				for (size_t j = 0; j < 8; j++)
				{
					map[n][i][j] = Pmap[n][i][j];
				}
			}
			GamePlay();
		default:
			break;
		}
	}
	system("pause");
	return 0;
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

T h a t

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

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

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

打赏作者

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

抵扣说明:

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

余额充值