推箱子小游戏

 贴图教程:(78条消息) C++贴图,图形化_SweetCinderella的博客-CSDN博客_c++贴图https://blog.csdn.net/qq_63982145/article/details/125149398

#include <iostream>
#include <graphics.h>//图像处理头文件
#include <conio.h>
using namespace std;
int map[8][8] =
{
	1,1,1,1,1,1,1,1,
	1,0,1,3,1,1,0,1,
	1,0,0,0,0,1,0,1,
	1,0,1,0,4,0,0,1,
	1,1,0,0,0,0,0,1,
	1,5,4,0,1,1,0,1,
	1,0,0,0,1,1,3,1,
	1,1,1,1,1,1,1,1,
};
IMAGE img[6];//图片命名
HWND hwnd;//窗口句柄
void loadresourse()//加载图片资源函数
{//若报错,项目属性改为多字节
	loadimage(img + 0, "0.png", 64, 64);
	loadimage(img + 1, "1.png", 64, 64);
	loadimage(img + 2, "3.png", 64, 64);
	loadimage(img + 3, "4.png", 64, 64);
	loadimage(img + 4, "5.png", 64, 64);
	loadimage(img + 5, "7.png", 64, 64);
}

void drawGraph()
{
	for (int i = 0; i < 8; i++)
	{
		for(int j=0;j<8;j++)
		{
			int x = 64 * j;
			int y = 64 * i;//贴图的位置坐标
			switch (map[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;
			}
		}
	}
}
int search_i()//找人位置的横坐标
{
	for (int i = 0; i < 8; i++)
		for (int j = 0; j < 8; j++)
			if (map[i][j] == 5 || map[i][j] == 8)
				return i;
	return -1;
}
int search_j()//找人位置的纵坐标
{
	for (int i = 0; i < 8; i++)
		for (int j = 0; j < 8; j++)
			if (map[i][j] == 5 || map[i][j] == 8)
				return j;
	return -1;
}
int gameover()//数组中不出现箱子(值为4),即游戏结束
{
	for (int i = 0; i < 8; i++)
		for (int j = 0; j < 8; j++)
			if (map[i][j] == 4)
				return 0;
	return 1;
}
void move()//人物的移动
{
	int i = search_i(); int j = search_j();
	char key = _getch();//按键输入
	switch(key)
	{
	case 'W':
	case 'w':
	case 72:
		if (map[i - 1][j] == 0 || map[i - 1][j] == 3)
		{
			map[i - 1][j] += 5;
			map[i][j] -= 5;
		}
		if(map[i-1][j]==4|| map[i - 1][j] == 7)
			if (map[i - 2][j] == 0 || map[i - 2][j] == 3)
			{
				map[i][j] -= 5;
				map[i - 1][j] += 1;
				map[i - 2][j] += 4;
			}
		break;
	case 'S':
	case 's':
	case 80:
		if (map[i + 1][j] == 0 || map[i +1][j] == 3)
		{
			map[i + 1][j] += 5;
			map[i][j] -= 5;
		}
		if (map[i + 1][j] == 4 || map[i + 1][j] == 7)
			if (map[i + 2][j] == 0 || map[i + 2][j] == 3)
			{
				map[i][j] -= 5;
				map[i + 1][j] += 1;
				map[i + 2][j] += 4;
			}
		break;
	case 'A':
	case 'a':
	case 75:
		if (map[i][j-1] == 0 || map[i][j-1] == 3)
		{
			map[i][j-1] += 5;
			map[i][j] -= 5;
		}
		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] -= 5;
				map[i][j - 1] += 1;
				map[i][j - 2] += 4;
			}
		break;
	case 'D':
	case 'd':
	case 77:
		if (map[i][j + 1] == 0 || map[i][j + 1] == 3)
		{
			map[i][j + 1] += 5;
			map[i][j] -= 5;
		}
		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] -= 5;
				map[i][j + 1] += 1;
				map[i][j + 2] += 4;
			}
		break;
	}
}

int main()
{
	hwnd=initgraph(64 * 8, 64 * 8);//初始化图形窗口
	loadresourse();
	while (1)
	{
		drawGraph();
		if (gameover())
		{
			MessageBox(hwnd, "win", "GameOver!", MB_OK);//对话框内容
				break;
		}
		move();
	}
	closegraph();//关闭图形系统
	return 0;
}

 

 运行结果:

fa7a6da7710f45c38826da56d923c6c1.png

多关卡:

#include <iostream>
#include <graphics.h>//图像处理头文件
#include <conio.h>
using namespace std;
int a=0;
int map[3][8][8] =
{
	1,1,1,1,1,1,1,1,
	1,0,1,3,1,0,0,1,
	1,0,0,0,0,0,0,1,
	1,0,0,0,4,3,0,1,
	1,1,0,0,0,0,1,1,
	1,5,4,0,1,1,0,1,
	1,0,0,0,1,1,0,1,
	1,1,1,1,1,1,1,1,

	1,1,1,1,1,1,1,1,
	1,0,1,3,1,1,0,1,
	1,0,0,0,0,1,0,1,
	1,0,1,0,4,0,0,1,
	1,1,0,0,0,0,0,1,
	1,5,4,0,1,1,0,1,
	1,0,0,0,1,1,3,1,
	1,1,1,1,1,1,1,1,

	1,1,1,1,1,1,1,1,
	1,0,1,0,1,1,0,1,
	1,3,0,0,0,1,0,1,
	1,0,1,0,4,0,0,1,
	1,1,0,0,0,0,0,1,
	1,5,4,0,1,1,0,1,
	1,0,0,0,1,1,3,1,
	1,1,1,1,1,1,1,1,
};
IMAGE img[6];//图片命名
HWND hwnd;//窗口句柄
void loadresourse()//加载图片资源函数
{//若报错,项目属性改为多字节
	loadimage(img + 0, "0.png", 64, 64);
	loadimage(img + 1, "1.png", 64, 64);
	loadimage(img + 2, "3.png", 64, 64);
	loadimage(img + 3, "4.png", 64, 64);
	loadimage(img + 4, "5.png", 64, 64);
	loadimage(img + 5, "7.png", 64, 64);
}

void drawGraph()
{
	for (int i = 0; i < 8; i++)
	{
		for(int j=0;j<8;j++)
		{
			int x = 64 * j;
			int y = 64 * i;//贴图的位置坐标
			switch (map[a][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;
			}
		}
	}
}
int search_i()//找人位置的横坐标
{
	for (int i = 0; i < 8; i++)
		for (int j = 0; j < 8; j++)
			if (map[a][i][j] == 5 || map[a][i][j] == 8)
				return i;
	return -1;
}
int search_j()//找人位置的纵坐标
{
	for (int i = 0; i < 8; i++)
		for (int j = 0; j < 8; j++)
			if (map[a][i][j] == 5 || map[a][i][j] == 8)
				return j;
	return -1;
}
int gameover()//数组中不出现箱子(值为4),即游戏结束
{
	for (int i = 0; i < 8; i++)
		for (int j = 0; j < 8; j++)
			if (map[a][i][j] == 4)
				return 0;
	return 1;
}
void move()//人物的移动
{
	int i = search_i(); int j = search_j();
	char key = _getch();//按键输入
	switch(key)
	{
	case 'W':
	case 'w':
	case 72:
		if (map[a][i - 1][j] == 0 || map[a][i - 1][j] == 3)
		{
			map[a][i - 1][j] += 5;
			map[a][i][j] -= 5;
		}
		if(map[a][i-1][j]==4|| map[a][i - 1][j] == 7)
			if (map[a][i - 2][j] == 0 || map[a][i - 2][j] == 3)
			{
				map[a][i][j] -= 5;
				map[a][i - 1][j] += 1;
				map[a][i - 2][j] += 4;
			}
		break;
	case 'S':
	case 's':
	case 80:
		if (map[a][i + 1][j] == 0 || map[a][i +1][j] == 3)
		{
			map[a][i + 1][j] += 5;
			map[a][i][j] -= 5;
		}
		if (map[a][i + 1][j] == 4 || map[a][i + 1][j] == 7)
			if (map[a][i + 2][j] == 0 || map[a][i + 2][j] == 3)
			{
				map[a][i][j] -= 5;
				map[a][i + 1][j] += 1;
				map[a][i + 2][j] += 4;
			}
		break;
	case 'A':
	case 'a':
	case 75:
		if (map[a][i][j-1] == 0 || map[a][i][j-1] == 3)
		{
			map[a][i][j-1] += 5;
			map[a][i][j] -= 5;
		}
		if (map[a][i][j - 1] == 4 || map[a][i][j - 1] == 7)
			if (map[a][i][j - 2] == 0 || map[a][i][j - 2] == 3)
			{
				map[a][i][j] -= 5;
				map[a][i][j - 1] += 1;
				map[a][i][j - 2] += 4;
			}
		break;
	case 'D':
	case 'd':
	case 77:
		if (map[a][i][j + 1] == 0 || map[a][i][j + 1] == 3)
		{
			map[a][i][j + 1] += 5;
			map[a][i][j] -= 5;
		}
		if (map[a][i][j + 1] == 4 || map[a][i][j + 1] == 7)
			if (map[a][i][j +2] == 0 || map[a][i][j +2] == 3)
			{
				map[a][i][j] -= 5;
				map[a][i][j + 1] += 1;
				map[a][i][j + 2] += 4;
			}
		break;
	}
}

int main()
{
	hwnd=initgraph(64 * 8, 64 * 8);//初始化图形窗口
	loadresourse();
	while (1)
	{
		drawGraph();
		if (gameover())
		{
			a++;
			if (a == 3)
			{
				MessageBox(hwnd, "win", "GameOver!", MB_OK);//对话框内容
				break;
			}
		}
		move();
	}
	closegraph();//关闭图形系统
	return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

SweetCinderella

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

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

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

打赏作者

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

抵扣说明:

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

余额充值