推箱子小游戏

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

#include 
#include //图像处理头文件
#include 
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;
}

 

 运行结果:

推箱子小游戏_第1张图片

多关卡:

#include 
#include //图像处理头文件
#include 
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;
}

 

 

你可能感兴趣的:(C++应用,c++,开发语言,1024程序员节)