手写c语言推箱子

手写c语言推箱子

#include<stdio.h>
#include<easyx.h>
#include<conio.h>
int life=1;//判断是否继续
int map[10][10]//图域 0空地,1围墙,2人,3箱子,4目的地
{
	  1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
		1, 4, 0, 0, 0, 0, 0, 1, 0, 1,
		1, 0, 0, 0, 0, 0, 0, 0, 0, 1,
		1, 4, 0, 0, 4, 0, 4, 0, 0, 1,
		1, 0, 0, 0, 0, 0, 0, 3, 1, 1,
		1, 3, 0, 0, 3, 0, 3, 0, 1, 1,
		1, 0, 0, 1, 0, 0, 0, 0, 1, 1,
		1, 0, 0, 1, 0, 0, 0, 0, 1, 1,
		1, 0, 0, 1, 2, 0, 0, 0, 0, 1,
		1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
		

};
IMAGE img[6];//图组
int arr[10][10];
int level = 0;
void init()
{
	for (int i = 0; i < 10; i++)
	{
		for (int j = 0; j < 10; j++)
		{
			arr[j][j] = map[i][j];
		}
	}
}
void DrawMap()
{
	BeginBatchDraw();//开始批量贴图
	putimage(0, 0, &img[1]);//大背景
	for (int i = 0; i < 10; i++)
	{
		for (int j = 0; j < 10; j++)
		{
			switch (map[i][j])
			{
			case 1:
				putimage(j * 70, i * 70, &img[2]);//图形库位置为数组位置颠倒再扩倍 贴围墙
				break;
			case 2:
				putimage(j * 70, i * 70, &img[0]);//贴人
				break;
			case 3:
				putimage(j * 70, i * 70, &img[4]);//箱子
				break;
			case 4:
				putimage(j * 70, i * 70, &img[3]);//目的地
				break;
			case 6:
				putimage(j * 70, i * 70, &img[0]);//人在目的地上
				break;
			case 7:
				putimage(j * 70, i * 70, &img[5]);//箱子在目的地上

			}

		}
		EndBatchDraw();//结束批量贴图
	}
}
void Play()
{
	int x,y;//人的位置
	for (int i = 0; i < 10; i++)
	{
		for (int j = 0; j < 10; j++)
		{
			if (map[i][j] == 2||map[i][j] == 6)
			{
				x = i;
				y = j;//获取人的位置
			}
		}
	}
	switch (_getch())
	{
	     case 'w':
			 if (map[x - 1][y] == 0 || map[x - 1][y] == 4)
			 {
				 map[x - 1][y] += 2;
				 map[x][y] -= 2;
			 }
			 else if (map[x-1][y] == 3)
			 {
				 if (map[x-2][y] == 0)
				 {
					 map[x-2][y] += 3;
					 map[x-1][y] -= 1;
					 map[x][y] -= 2;
				 }
				 else if (map[x - 2][y] == 4)
				 {
					 map[x - 2][y] += 3;
					 map[x - 1][y] -= 1;
					 map[x][y] -= 2;
				 }
			 }
			 else if (map[x - 1][y] == 7)
			 {
				 if (map[x - 2][y] == 0)
				 {
					 map[x - 2][y] += 3;
					 map[x - 1][y] -= 1;
					 map[x][y] -= 2;
				  }

			 }
			 break;
		 case 's':
			 if (map[x + 1][y] == 0 || map[x + 1][y] == 4)
			 {
				 map[x + 1][y] += 2;
				 map[x][y] -= 2;
			 }
			 else if (map[x +1][y] == 3)
			 {
				 if (map[x + 2][y] == 0)
				 {
					 map[x + 2][y] += 3;
					 map[x + 1][y] -= 1;
					 map[x][y] -= 2;
				 }
				 else if (map[x + 2][y] == 4)
				 {
					 map[x + 2][y] += 3;
					 map[x + 1][y] -= 1;
					 map[x][y] -= 2;
				 }
			 }
			 else if (map[x + 1][y] == 7)
			 {
				 if (map[x + 2][y] == 0)
				 {
					 map[x + 2][y] += 3;
					 map[x + 1][y] -= 1;
					 map[x][y] -= 2;
				 }

			 }
			 break;
		 case 'a':
			 if (map[x][y - 1] == 0 || map[x][y - 1] == 4)
			 {
				 map[x][y - 1] += 2;
				 map[x][y] -= 2;
			 }
			 else if (map[x][y - 1] == 3)
			 {
			 if (map[x][y - 2] == 0)
			 {
				 map[x][y - 2] += 3;
				 map[x][y - 1] -= 1;
				 map[x][y] -= 2;
			 }
			 else if (map[x][y - 2] == 4)
			 {
				 map[x][y - 2] += 3;
				 map[x][y - 1] -= 1;
				 map[x][y] -= 2;
			 }
			 }
			 else if (map[x][y - 1] == 7)
			 {
			 if (map[x][y - 2] == 0)
			 {
				 map[x][y - 2] += 3;
				 map[x][y - 1] -= 1;
				 map[x][y] -= 2;
			 }

			 }
			 break;
		 case 'd':
			 if (map[x][y + 1] == 0 || map[x][y + 1] == 4)
			 {
				 map[x][y + 1] += 2;
				 map[x][y] -= 2;
			 }
			 else if (map[x][y + 1] == 3)
			 {
				 if (map[x][y + 2] == 0)
				 {
					 map[x][y + 2] += 3;
					 map[x][y + 1] -= 1;
					 map[x][y] -= 2;
				 }
				 else if (map[x][y + 2] == 4)
				 {
					 map[x][y + 2] += 3;
					 map[x][y + 1] -= 1;
					 map[x][y] -= 2;
				 }
			 }
			 else if (map[x][y + 1] == 7)
			 {
				 if (map[x][y + 2] == 0)
				 {
					 map[x][y + 2] += 3;
					 map[x][y + 1] -= 1;
					 map[x][y] -= 2;
				 }

			 }
			 break;
	}
}
void Win()
{
	int number = 0;//假设么得箱子了
	for (int i = 0; i < 10; i++)
	{
		for (int j = 0; j < 10; j++)
		{
			if (map[i][j] == 3)
			{
				number++;
			}
		}

	}
	for (int i = 0; i < 10; i++)
	{
		for (int j = 0; j < 10; j++)
		{
			if (arr[i][j] == 3)
			{
				if (arr[i - 1][j] == 1 || arr[i + 1][j] == 1)
				{
					if (arr[i][j - 1] == 1 || arr[i][j + 1] == 1)
					{
						life = 2;//失败条件
					}
				}
					
			}
		}

	}
	if (number == 0)
	{
		life = 0;//胜利条件

	}

}
int main()
{
	loadimage(&img[0], "人.jpg", 70, 70);
	loadimage(&img[1], "背景.jpg", 700, 700);
	loadimage(&img[2], "围墙.jpg", 70, 70);
	loadimage(&img[3], "目的地.jpg", 70, 70);
	loadimage(&img[4], "箱子.jpg", 70, 70);
	loadimage(&img[5], "箱子到达目的地.jpg", 70, 70);
	initgraph(700, 700);//窗口
	while (1)
	{
		Play();
		DrawMap();
		Win();

		if (life == 0)
		{
			if (MessageBox(GetHWnd(), "游戏胜利!", "是否要继续?", MB_YESNO) == IDYES)
			{
			  life = 1;
			  init();
			  DrawMap();
			}
			else
			{
				break;
			}


		}
		else if(life==2)
		{
			if (MessageBox(GetHWnd(), "游戏失败", "是否要重新开始?", MB_YESNO) == IDYES)
			{
				life = 1;
				init();
				DrawMap();
			}
			else
			{
				break;
			}
		}
	}
	getchar();
	closegraph();
	return 0;
}

本人在初学之初走了很多弯路,所以为了方便广大转行群众特地整理了自学资料 从书籍电子版到编译器使用再打到试题应有尽有 有兴趣的小伙伴们可以 嘉裙 907 729 554

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值