[c language] push box

Required knowledge: C language enumeration, array, for loop, while loop, switch, case statement, graphics library related functions

1. Adjust the console window size

#define  _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>

#include <stdio.h>
int main()
{
    
    


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

Insert image description here

2. Clear the words on the console screen

#define  _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>

#include <stdio.h>
int main()
{
    
    


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

Insert image description here

3. Enumeration types define open spaces, walls, destinations, boxes, and players in the map

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

4. Define a two-dimensional array to make a map, and print it out to see the effect.

//定义一个二维数组,做地图 空地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");




}






}

Call printmap() in main
Insert image description here

In order to prevent the program from exiting after entering characters, add a while loop

int main()
{
    
    

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

5. Modify the printmap function to the gamedraw() function

In order to ensure the beauty of the game, we convert the corresponding numbers into beautiful patterns
using a two-layer loop to traverse the two-dimensional array. After using the switch, we have replaced the corresponding numbers with patterns. At this time we need to download the Sogou input method.
Insert image description here

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");
	}





}

Corresponding numbers correspond to enumeration variable types. PLAYER+DEST means that if the player appears at the destination, the corresponding number is 6. BOX+DEST means that the box is at the destination. Replace the printmap in the main function with gamedraw().
Insert image description here

6. Button control movement

If you want to make the player move, you must first determine the player's coordinates. By traversing the two-dimensional array, find the number in the array equal to 4 and 4+2. The latter indicates that the player is at the destination. You must also obtain the coordinates. When the player coordinates are found When, you need to jump out of the loop, and break can only jump out of the loop once, so using the goto function, you can jump out of multi-layer loops directly.

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:;//找到直接来这里

After using the _getch() function to put the key information into the ch character variable, if we don’t know the key values ​​corresponding to up, down, left, and right, we can print out ch to see. We encapsulate these into a function keyevent() to obtain the player coordinates. , and read the information of keyboard press

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

















}

Through printf("%d %c", ch, ch);,
the virtual key value is //w 119 a 97 s 115 d 100, then comment out printf("%d %c", ch, ch); and then through The swich and case statements handle the processing after the up, down, left, and right buttons are pressed respectively.

	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;
	}

For example, if the w key is pressed, if there is an open space or a destination above the player, the player can move directly there because there are no obstacles. If the player's coordinates are map[i][j]; then the coordinates above the player are What map[i-1][j]; does is

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



		 }

If there are only players in map[i][j], moving the player up will result in map[i][j]=0, and the position becomes an open space. If map[i][j] is a player plus a destination, moving the player up will Map[i][j]=DEST will become a simple destination.
If the position above the player is a box or a box plus a destination, it depends on what is above the player. If it is an open space, or The destination can be pushed. map[i - 2][j] is the coordinates above the player. What to do is

 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;//玩家消失在原来位置





			 }



		 }

Key on the processed function

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;

Insert image description here
Push it up. If you understand how to move the up key, you will be able to handle the
Insert image description here
rest. Overall 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. Production of multiple sets of maps and modification of map arrays

Define the global variable level=0; to represent the level number,
and change the two-dimensional array to a three-dimensional array. Each element of the three-dimensional array is a two-dimensional array, which is a map.

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}
	}
};

Change all map[][] to map[level][][]; each time a level is passed, level++;

8. Customs clearance judgment

Loop through the two-dimensional array. If there is map[level][i][j]==BOX, it means that there is a box that has not been pushed to the destination, and returns false. If there is no box at the end of the loop, it means that this level has been passed, and returns true. This judgment Function returns a value of type Boolean

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;
			}
		}

The main function adds that if jude() returns 1, then the level is cleared, and then level++;
becomes the second element in the three-dimensional array, that is, the map is changed. When level>2, it means the level is cleared, because only three maps are set.

9. Program source code (without graphics library)

#define  _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <conio.h>
#include <stdio.h>
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.Demo 1

20231002_124830

11. Add graphics library version

12.Header file added

#include <graphics.h >

13. Define classes to save images of open spaces, destinations, players, boxes, walls, and boxes pushed to destinations.

IMAGE  ima_all[6];

14. Place the image file in the same directory as the .cpp file

Insert image description here
The image folder was created by myself and is used to store the materials for sokoban, which are pictures. You can find pictures of sokoban on the Internet.
Insert image description here

15. Load image function

void loadimg()
{
    
    
	
	for (int i = 0; i < 6; i++)
	{
    
    
		char file[20] = "";
		sprintf(file,"./images/%d.png", i);
		loadimage(ima_all + i,file, 40, 40);
		

	}





}

Why load pictures like this? Here we name the photos 0, 1, 2, 3, 4, 5. To load all six photos, only the photo names are different in the relative paths of the six photos. We can loop through Put the relative path string corresponding to each photo into the file string array file, and use sprintf. If you don’t know how to use sprintf here, you can check out my article on file operations. File operations, use the loadimage function to Six pictures are loaded in. The first parameter of loadimage is the first address of the picture. The second parameter is the relative path of the picture. The third and fourth parameters are the resolution, that is, the size. The last two parameters can Insert image description here
be When you see 42 41, here we use 40 40;

16. Modification of gamedraw function

Because if you use the graphics library, you don't need to use the previous gamedraw function to print the pattern, but paste the picture.

void gamedraw()
{
    
    
	for (int i = 0; i < 10; i++)
	{
    
    
		for (int j = 0; j < 10; j++)
		{
    
    
			int x = j * 40;
			int y = i * 40;

			switch (map[level][i][j])
			{
    
    
			case SPACE:
				putimage(x, y, ima_all+2);         //一个中文字符相当于二个英文字符
				break;
			case WALL:
				putimage(x, y, ima_all+1);
				break;
			case DEST:
				putimage(x, y, ima_all+4);
				break;
			case BOX:
				putimage(x, y, ima_all+3);
				break;
			case PLAYER:
				putimage(x, y, ima_all);
				break;
			case PLAYER + DEST:
				putimage(x, y, ima_all);
				break;
			case BOX + DEST://就是箱子推到目的地
				putimage(x, y, ima_all+5);
				break;
			}






		}

		
	}





}

Insert image description here

As can be seen from the above figure, i, j and x, y are reversed. Use the putimage function to paste each picture. The first two parameters of putimage are the coordinates of the upper left corner of the interface where the picture is to be pasted. The third parameter is the picture to be pasted. The corresponding address, the previously named photo is in the array ima_all[6];, which means that the address of ima_all+3 corresponds to the corresponding picture.
Insert image description here

17. Main function modification

int main()
{
    
    
	initgraph(400, 400);
	loadimg();
	//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;
}

Do not use the console to display the map, initialize the interface, and display the map on the interface. Since a picture is 40 by 40 and the two-dimensional array is 10 by 10, the rows and columns of the interface should be 400, so initgraph(400, 400); Since there is no console, there is no need to use the printf function, there is no need to change the size of the console, and there is no need to clear the screen.
When you start compiling and running, the following error
Insert image description here
solution will appear: Debugging->Properties->Advanced- 》Character set-》Multiple character sets

18.Game clearance display

if level>2

settextcolor(BLACK);//字体颜色
				settextstyle(25, 0, "微软雅黑");//字体风格
				setbkmode(TRANSPARENT);//字体背景透明
				
				outtextxy(100, 100, "oioioioioioioioi奥哈呦学妹你通过了!");//字体显示位置,以及内容
				_getch();
				break;

19. Program source code (with graphics library version)

#define  _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>//_getch()函数头文件
#include <stdbool.h>//bool类型的头函数
#include <graphics.h >//图形库头文件
//定义一个二维数组,做地图
//空地0  墙1  目的地2  箱子3  玩家4    箱子在目的地 5  玩家在目的地6
IMAGE  ima_all[6];
int level = 0;
void loadimg()
{
    
    
	
	for (int i = 0; i < 6; i++)
	{
    
    
		char file[20] = "";
		sprintf(file,"./images/%d.png", i);
		loadimage(ima_all + i,file, 40, 40);
		

	}





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





};
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++)
		{
    
    
			int x = j * 40;
			int y = i * 40;

			switch (map[level][i][j])
			{
    
    
			case SPACE:
				putimage(x, y, ima_all+2);         //一个中文字符相当于二个英文字符
				break;
			case WALL:
				putimage(x, y, ima_all+1);
				break;
			case DEST:
				putimage(x, y, ima_all+4);
				break;
			case BOX:
				putimage(x, y, ima_all+3);
				break;
			case PLAYER:
				putimage(x, y, ima_all);
				break;
			case PLAYER + DEST:
				putimage(x, y, ima_all);
				break;
			case BOX + DEST:
				putimage(x, y, ima_all+5);
				break;
			}






		}

		
	}





}
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();
	//printf("%d    %c", ch, ch);//w 119 a 97 s 115  d 100
	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] += 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 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()
{
    
    
	initgraph(10 * 40, 10 * 40);
	loadimg();
	system("mode con lines=15 cols=25");//调整窗口大小
	
	while (1)
	{
    
    
		//system("cls");
		gamedraw();
		if (jude())
		{
    
    
			level++;
			if (level > 2)
			{
    
    
				settextcolor(BLACK);
				settextstyle(25, 0, "微软雅黑");
				setbkmode(TRANSPARENT);
				
				outtextxy(100, 100, "oioioioioioioioi奥哈呦学妹你通过了!");
				_getch();
				break;
			}
		}
			keyevent();
			
		}
	getchar();//不让程序退出
	return 0;
		
	}

20.Demo 2

20231002_153153

21. Addition of levels

You only need to change the defined global variable map[3][10][10] and 3 to the number of levels you want, and then add a two-dimensional array like the one above to the three-dimensional array. The levels required to clear all levels > The number of levels is -1

22.Level reopening

You need to define another array that is the same as the map[][][] three-dimensional array to save the initial situation of each level resetmap[][][],

int mapreset[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}
	}
};

Note that it is still a global variable. We set it to reset the level when the r key is pressed, so we need to modify the keyevent() function. When r is pressed, we traverse the map array and assign it to the original map.

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();
	//printf("%d    %c", ch, ch);//w 119 a 97 s 115  d 100
	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] += 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 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;
	case 'r':///新增
	case'R' :///新增
		for (int i = 0; i < 10; i++)///新增
		{
    
    ///新增
			for (int j = 0; j < 10; j++)///新增
			{
    
    
				map[level][i][j] = mapreset[level][i][j];///新增




			}///新增
		}///新增
		break;///新增








	}///新增



}

Guess you like

Origin blog.csdn.net/yyqzjw/article/details/133484321