手机版 欢迎访问创业网 - 全球最顶尖的创业信息平台(91cyl.com)网站

当前位置: 主页 > 运营

【c语言】推箱子

时间:2024-03-31 11:03|来源:创业网 - 全球最顶尖的创业信息平台|作者:创业网|点击:

所需知识:c语言枚举,数组,for循环,while循环,switch,case语句,图形库相关函数

1.调整控制台窗口大小

#define  _CRT_SECURE_NO_WARNINGS
#include 

#include 
int main()
{


	system("mode con lines=15 cols=25");//调整窗口大小
	return 0;
}

2.清掉控制台屏幕上的字

#define  _CRT_SECURE_NO_WARNINGS
#include 

#include 
int main()
{


	system("mode con lines=15 cols=25");
	system("cls");//清屏操作
	getchar();//不让程序退出,等待读字符
	return 0;
}

3. 枚举类型定义地图中空地,墙,目的地,箱子,玩家

enum  Mine
{
	SPACE,  //空地
	WALL,//墙
	DEST,  //目的地
	BOX,  //箱子
	PLAYER//玩家
};

4.定义二维数组做地图,并且打印出来看看效果

//定义一个二维数组,做地图 空地0  墙1  目的地2  箱子3  玩家4    箱子在目的地 5  玩家在目的地6,与枚举类型对应上了
int map[10][10] =
{
	{0,0,0,0,0,0,0,0,0,0},
	{0,0,0,1,1,1,0,0,0,0},
	{0,0,0,1,2,1,1,1,1,0},
	{0,1,1,1,3,0,3,2,1,0},
	{0,1,2,3,4,0,1,1,1,0},
	{0,1,1,1,1,3,1,0,0,0},
	{0,0,0,0,1,2,1,0,0,0},
	{0,0,0,0,1,1,1,0,0,0},
	{0,0,0,0,0,0,0,0,0,0},
	{0,0,0,0,0,0,0,0,0,0}
};

void printmap()
{for(int i=0;i<10;i++)
{
	for (int j = 0; j < 10; j++)
	{
		printf("%d ", map[i][j]);





	}
	printf("\n");




}






}

将printmap()在main中调用

为了让程序不会输入字符后退出,加上while循环

int main()
{

	while (1)
	{
		system("mode con lines=15 cols=25");
		system("cls");//清屏操作
		printmap();
		getchar();//不让程序退出,等待读字符
	}
	return 0;
}

5.修改printmap函数为gamedraw()函数

为了保证游戏的美观性,我们将对应的数字转化为好看的图案
使用两层循环遍历二维数组,在使用switch已经将对应的数字用图案代替,此时我们要下载搜狗输入法

void gamedraw()
{
	for (int i = 0; i < 10; i++)
	{
		for (int j = 0; j < 10; j++)
		{
			 
				switch (map[i][j])
				{case SPACE://如果二维数组元素为0
					printf("  ");  //空地        //一个中文字符相当于二个英文字符
					break;
				case WALL://如果二维数组元素为1
					printf("■");//墙
					break;
				case DEST://如果二维数组元素为2
					printf("☆");//目的地
					break;
				case BOX://如果二维数组元素为3
					printf("□");//箱子
					break;
				case PLAYER://如果二维数组元素为4
					printf("♀");//玩家
					break;
				case PLAYER+DEST://如果二维数组元素为6
					printf("♂");//玩家在目的地
					break;
				case BOX+DEST://如果二维数组元素为5
					printf("★");箱子在目的地
					break;
                }






		}
				
printf("\n");
	}





}

对应数字和枚举变量类型对应上,PLAYER+DEST表示玩家如果出现在目的地的话对应数字为6,BOX+DEST表示箱子在目的地,将主函数中的printmap用gamedraw()换掉。

6.按键控制移动

要想使玩家移动,就先得确定玩家的坐标,通过遍历二维数组,找到数组中数字等于4的和4+2的,后者表示玩家在目的地,也要获取坐标,当找到玩家坐标时,要跳出循环,而break只能跳出一次循环,所以使用goto函数,可以直接跳出多层循环.

int i = 0; int j = 0;//定义不要在for循环里面,要不然出作用域就会被销毁
	for (i = 0; i < 10; i++)
	{
		for (j = 0; j < 10; j++)
		{
			if (map[i][j] == PLAYER||map[i][j] == PLAYER+DEST)
			{
				goto end;
			}
		}
	}
end:;//找到直接来这里

在使用_getch()函数将按键的信息放入ch字符变量中,如果不知道上下左右对应的键值,我们可以打印出ch看看,我们把这些都封装成一个函数 keyevent()获取玩家坐标,并且读取键盘按下的信息

void keyevent()
{
	int i = 0; int j = 0;
	for (i = 0; i < 10; i++)
	{
		for (j = 0; j < 10; j++)
		{
			if (map[i][j] == PLAYER || map[i][j] == PLAYER + DEST)
			{
				goto end;
			}
		}
	}
end:;
	char ch = _getch();
	printf("%d    %c", ch, ch);//w 119 a 97 s 115  d 100

















}

通过printf("%d %c", ch, ch);
得到虚拟键值为//w 119 a 97 s 115 d 100,然后注释掉printf("%d %c", ch, ch);然后通过swich,case 语句分别处理上下左右按键按下后的处理,

	switch (ch)
	{
	case 119:
	case 'w ':
	case 'W':
		break;
	case 97:
	case 'a ':
	case'A':
		break;
	case 115:
	case 's ':
	case'S':
		break;
	case 100:
	case 'd ':
	case'D':
		break;
	}

比如说按下w键,如果玩家上面是空地或者是目的地的话,玩家可以直接挪过去,因为没有障碍物阻碍,如果玩家的坐标为map[i][j];则玩家上面的的坐标就是map[i-1][j];要做的是

 if (map[i - 1][j] == SPACE||map[i - 1][j] == DEST)
		 {
			 map[i - 1][j] += PLAYER;
			 map[i][j] -= PLAYER;



		 }

如果map[i][j]只有玩家的话,上移动玩家就会map[i][j]=0,该位置变为空地,如果map[i][j]是玩家加目的地,上移动玩家就会map[i][j]=DEST,变为单纯的目的地.
如果玩家上面一个位置是箱子或者是箱子加目的地,就要看玩家上面的上面是什么了,如果是空地,或者是目的地,就可以推动,map[i - 2][j]是玩家上面的上面的坐标要做的是

 else  if(map[i-1][j]==BOX||map[i-1][j]==BOX+DEST)
		 {
			 if (map[i - 2][j] == SPACE || map[i - 2][j] == DEST)
			 {//完成玩家上面有箱子,箱子的上面是空地或者是目的地都可以推动
				 map[i - 2][j] += BOX;//玩家上面的上面加一个箱子
				 map[i - 1][j] = map[i - 1][j] - BOX + PLAYER;//玩家的上面减去一个箱子加上一个玩家
				 map[i][j] -= PLAYER;//玩家消失在原来位置





			 }



		 }

处理完的函数上键

case 119:
	case 'w ':
	case 'W':
		if (map[i - 1][j] == SPACE || map[i - 1][j] == DEST)
		{
			map[i - 1][j] += 4;
			map[i][j] -= 4;



		}
		else  if (map[i - 1][j] == BOX || map[i - 1][j] == BOX + DEST)
		{
			if (map[i - 2][j] == SPACE || map[i - 2][j] == DEST)
			{
				map[i - 2][j] += BOX;
				map[i - 1][j] = map[i - 1][j] - BOX + PLAYER;
				map[i][j] -= PLAYER;//玩家消失在原来位置





			}



		}
		break;


向上推了一下,如果懂了上键怎么移动,别的也就会处理了

整体的keyevent()

void keyevent()
{
	int i = 0; int j = 0;
	for (i = 0; i < 10; i++)
	{
		for (j = 0; j < 10; j++)
		{
			if (map[i][j] == PLAYER || map[i][j] == PLAYER + DEST)
			{
				goto end;
			}
		}
	}
end:;
	char ch = _getch();
	switch (ch)
	{
	case 119:
	case 'w ':
	case 'W':
		if (map[i - 1][j] == SPACE || map[i - 1][j] == DEST)
		{
			map[i - 1][j] += PLAYER;
			map[i][j] -=  PLAYER;



		}
		else  if (map[i - 1][j] == BOX || map[i - 1][j] == BOX + DEST)
		{
			if (map[i - 2][j] == SPACE || map[i - 2][j] == DEST)
			{
				map[i - 2][j] += BOX;
				map[i - 1][j] = map[i - 1][j] - BOX + PLAYER;
				map[i][j] -= PLAYER;//玩家消失在原来位置





			}



		}
		break;
	case 97:
	case 'a ':
	case'A':
		if (map[i][j - 1] == SPACE || map[i][j - 1] == DEST)
		{
			map[i][j - 1] += PLAYER;
			map[i][j] -=  PLAYER;



		}
		else  if (map[i][j - 1] == BOX || map[i][j - 1] == BOX + DEST)
		{
			if (map[i][j - 2] == SPACE || map[i][j - 2] == DEST)
			{
				map[i][j - 2] += BOX;
				map[i][j - 1] = map[i][j - 1] - BOX + PLAYER;
				map[i][j] -= PLAYER;//玩家消失在原来位置





			}



		}
		break;
	case 115:
	case 's ':
	case'S':
		if (map[i + 1][j] == SPACE || map[i + 1][j] == DEST)
		{
			map[i + 1][j] += PLAYER;
			map[i][j] -= PLAYER;



		}
		else  if (map[i + 1][j] == BOX || map[i + 1][j] == BOX + DEST)
		{
			if (map[i + 2][j] == SPACE || map[i + 2][j] == DEST)
			{
				map[i + 2][j] += BOX;
				map[i + 1][j] = map[i + 1][j] - BOX + PLAYER;
				map[i][j] -= PLAYER;//玩家消失在原来位置





			}



		}
		break;
	case 100:
	case 'd ':
	case'D':
		if (map[i][j + 1] == SPACE || map[i][j + 1] == DEST)
		{
			map[i][j + 1] +=  PLAYER;
			map[i][j] -=  PLAYER;



		}
		else  if (map[i][j + 1] == BOX || map[i][j + 1] == BOX + DEST)
		{
			if (map[i][j + 2] == SPACE || map[i][j + 2] == DEST)
			{
				map[i][j + 2] += BOX;
				map[i][j + 1] = map[i][j + 1] - BOX + PLAYER;
				map[i][j] -= PLAYER;//玩家消失在原来位置





			}



		}
		break;
	}


}

7.多组地图的制作,修改map数组

定义全局变量level=0;表示关数,
将二维数组改为三维数组,三维数组的每一个元素就是二维数组,就是一个地图.

int map[3][10][10] =
{
	{
	{0,0,0,0,0,0,0,0,0,0},
	{0,0,0,1,1,1,0,0,0,0},
	{0,0,0,1,2,1,1,1,1,0},
	{0,1,1,1,3,0,3,2,1,0},
	{0,1,2,3,4,0,1,1,1,0},
	{0,1,1,1,1,3,1,0,0,0},
	{0,0,0,0,1,2,1,0,0,0},
	{0,0,0,0,1,1,1,0,0,0},
	{0,0,0,0,0,0,0,0,0,0},
	{0,0,0,0,0,0,0,0,0,0}
	},

	{
	{0,0,0,0,0,0,0,0,0,0},
	{0,0,1,1,0,0,1,1,0,0},
	{0,1,0,2,1,1,2,0,1,0},
	{1,0,0,0,3,0,0,0,0,1},
	{1,0,0,0,4,3,0,0,0,1},
	{0,1,0,0,3,3,0,0,1,0},
	{0,0,1,0,0,0,0,1,0,0},
	{0,0,0,1,2,2,1,0,0,0},
	{0,0,0,0,1,1,0,0,0,0},
	{0,0,0,0,0,0,0,0,0,0}
	},
	{
	{0,0,0,0,1,0,0,0,0,0},
	{0,0,0,1,0,1,0,0,0,0},
	{0,0,1,2,3,0,1,0,0,0},
	{0,1,0,0,0,0,0,1,0,0},
	{1,2,3,0,4,0,0,0,1,0},
	{0,1,0,0,0,0,0,3,2,1},
	{0,0,1,0,3,0,0,0,1,0},
	{0,0,0,1,2,0,0,1,0,0},
	{0,0,0,0,1,0,1,0,0,0},
	{0,0,0,0,0,1,0,0,0,0}
	}
};

将所有map[][]改成map[level][][];每通过一关,level++;

8.通关判断

循环遍历二维数组,如果有map[level][i][j]==BOX,说明有箱子没有推到目的地,返回false ,循环结束如果没有的话,说明通过此关,返回true,该判断函数返回布尔类型的值

bool jude()
{

	for (int i = 0; i < 10; i++)
	{
		for (int j = 0; j < 10; j++)
		{
			if (map[level][i][j] == BOX)
			{
				return false;
			}
		}
	}






}

	if (jude())
		{
			level++;
			if (level > 2)
			{
				
				printf("oioioioioioioioi奥哈呦学妹你通过了!");
			
				_getch();
				break;
			}
		}

主函数添加如果jude()返回1,然后就是通关,然后level++;
变成三维数组中的第二个元素,也就是换了地图,当level>2,表示通关,因为只设置了三个图

9.程序源码(无图形库)

#define  _CRT_SECURE_NO_WARNINGS
#include 
#include 
#include 
enum  Mine
{
	SPACE,  //空地
	WALL,//墙
	DEST,  //目的地
	BOX,  //箱子
	PLAYER//玩家





};
int level = 0;
//定义一个二维数组,做地图 空地0  墙1  目的地2  箱子3  玩家4    箱子在目的地 5  玩家在目的地6,与枚举类型对应上了
int map[3][10][10] =
{
	{
	{0,0,0,0,0,0,0,0,0,0},
	{0,0,0,1,1,1,0,0,0,0},
	{0,0,0,1,2,1,1,1,1,0},
	{0,1,1,1,3,0,3,2,1,0},
	{0,1,2,3,4,0,1,1,1,0},
	{0,1,1,1,1,3,1,0,0,0},
	{0,0,0,0,1,2,1,0,0,0},
	{0,0,0,0,1,1,1,0,0,0},
	{0,0,0,0,0,0,0,0,0,0},
	{0,0,0,0,0,0,0,0,0,0}
	},

	{
	{0,0,0,0,0,0,0,0,0,0},
	{0,0,1,1,0,0,1,1,0,0},
	{0,1,0,2,1,1,2,0,1,0},
	{1,0,0,0,3,0,0,0,0,1},
	{1,0,0,0,4,3,0,0,0,1},
	{0,1,0,0,3,3,0,0,1,0},
	{0,0,1,0,0,0,0,1,0,0},
	{0,0,0,1,2,2,1,0,0,0},
	{0,0,0,0,1,1,0,0,0,0},
	{0,0,0,0,0,0,0,0,0,0}
	},
	{
	{0,0,0,0,1,0,0,0,0,0},
	{0,0,0,1,0,1,0,0,0,0},
	{0,0,1,2,3,0,1,0,0,0},
	{0,1,0,0,0,0,0,1,0,0},
	{1,2,3,0,4,0,0,0,1,0},
	{0,1,0,0,0,0,0,3,2,1},
	{0,0,1,0,3,0,0,0,1,0},
	{0,0,0,1,2,0,0,1,0,0},
	{0,0,0,0,1,0,1,0,0,0},
	{0,0,0,0,0,1,0,0,0,0}
	}
};

void gamedraw()
{
	for (int i = 0; i < 10; i++)
	{
		for (int j = 0; j < 10; j++)
		{
			 
				switch (map[level][i][j])
				{case SPACE:
					printf("  ");          //一个中文字符相当于二个英文字符
					break;
				case WALL:
					printf("■");
					break;
				case DEST:
					printf("☆");
					break;
				case BOX:
					printf("□");
					break;
				case PLAYER:
					printf("♀");
					break;
				case PLAYER+DEST:
					printf("♂");
					break;
				case BOX+DEST:
					printf("★");
					break;
                }






		}
				
printf("\n");
	}





}
void keyevent()
{
	int i = 0; int j = 0;
	for (i = 0; i < 10; i++)
	{
		for (j = 0; j < 10; j++)
		{
			if (map[level][i][j] == PLAYER || map[level][i][j] == PLAYER + DEST)
			{
				goto end;
			}
		}
	}
end:;
	char ch = _getch();
	switch (ch)
	{
	case 119:
	case 'w ':
	case 'W':
		if (map[level][i - 1][j] == SPACE || map[level][i - 1][j] == DEST)
		{
			map[level][i - 1][j] += 4;
			map[level][i][j] -= 4;



		}
		else  if (map[level][i - 1][j] == BOX || map[level][i - 1][j] == BOX + DEST)
		{
			if (map[level][i - 2][j] == SPACE || map[level][i - 2][j] == DEST)
			{
				map[level][i - 2][j] += BOX;
				map[level][i - 1][j] = map[level][i - 1][j] - BOX + PLAYER;
				map[level][i][j] -= PLAYER;//玩家消失在原来位置





			}



		}
		break;
	case 97:
	case 'a ':
	case'A':
		if (map[level][i][j - 1] == SPACE || map[level][i][j - 1] == DEST)
		{
			map[level][i][j - 1] += 4;
			map[level][i][j] -= 4;



		}
		else  if (map[level][i][j - 1] == BOX || map[level][i][j - 1] == BOX + DEST)
		{
			if (map[level][i][j - 2] == SPACE || map[level][i][j - 2] == DEST)
			{
				map[level][i][j - 2] += BOX;
				map[level][i][j - 1] = map[level][i][j - 1] - BOX + PLAYER;
				map[level][i][j] -= PLAYER;//玩家消失在原来位置





			}



		}
		break;
	case 115:
	case 's ':
	case'S':
		if (map[level][i + 1][j] == SPACE || map[level][i + 1][j] == DEST)
		{
			map[level][i + 1][j] += PLAYER;
			map[level][i][j] -= PLAYER;



		}
		else  if (map[level][i + 1][j] == BOX || map[level][i + 1][j] == BOX + DEST)
		{
			if (map[level][i + 2][j] == SPACE || map[level][i + 2][j] == DEST)
			{
				map[level][i + 2][j] += BOX;
				map[level][i + 1][j] = map[level][i + 1][j] - BOX + PLAYER;
				map[level][i][j] -= PLAYER;//玩家消失在原来位置





			}



		}
		break;
	case 100:
	case 'd ':
	case'D':
		if (map[level][i][j + 1] == SPACE || map[level][i][j + 1] == DEST)
		{
			map[level][i][j + 1] += 4;
			map[level][i][j] -= 4;



		}
		else  if (map[level][i][j + 1] == BOX || map[level][i][j + 1] == BOX + DEST)
		{
			if (map[level][i][j + 2] == SPACE || map[level][i][j + 2] == DEST)
			{
				map[level][i][j + 2] += BOX;
				map[level][i][j + 1] = map[level][i][j + 1] - BOX + PLAYER;
				map[level][i][j] -= PLAYER;//玩家消失在原来位置





			}



		}
		break;
	}


}
bool jude()
{

	for (int i = 0; i < 10; i++)
	{
		for (int j = 0; j < 10; j++)
		{
			if (map[level][i][j] == BOX)
			{
				return false;
			}
		}
	}






}








int main()
{
	system("mode con lines=15 cols=25");
	//system("cls");//清屏操作
	while (1)
	{
		
	
		
		gamedraw();
		
		//_getch();
		if (jude())
		{
			level++;
			if (level > 2)
			{
				
				printf("oioioioioioioioi奥哈呦学妹你通过了!");
			
				_getch();
				break;
			}
		}keyevent();
		
	}
	getchar();//不让程序退出,等待读字符
	return 0;
}

10.演示1

20231002_124830

Copyright © 2005-2024 创业网 版权所有 网站地图