Project address -> Github

Project address -> Code cloud

  • Effect preview:

  • Guide package
#include <stdio.h>
#include <stdlib.h>
#include 
       
         // A dialog box is displayed
       
Copy the code
  • Map design
/* 0: empty space 1: destination ☆ 2: wall ■ 4: Box
int map[13] [14] = {{0.0.0.0.2.2.2.2.2.2.0.0.0.0},
    {0.0.0.0.2.0.0.0.0.2.0.0.0.0},
    {0.2.2.2.2.0.0.4.4.2.2.2.2.2},
    {0.2.0.0.0.2.0.0.0.0.0.0.0.2},
    {0.2.0.4.0.0.0.4.0.0.0.4.0.2},
    {0.2.0.0.0.2.2.0.2.2.2.2.2.2},
    {2.2.2.2.4.2.2.0.0.0.0.0.0.2},
    {2.0.0.0.4.0.2.0.0.0.8.0.0.2},
    {2.0.4.0.4.0.2.0.2.2.2.2.2.2},
    {2.2.0.0.4.0.2.0 ,2.1.1.1.2.0},
    {0.2.0.2.0.0.0.0.0.1.1.1.2.0},
    {0.2.0.0.0.0.2.2.1.1.1.1.2.0},
    {0.2.2.2.2.2.2.2.2.2.2.2.2.0}};Copy the code
  • Store the location information of all nodes of type 1, as long as these nodes are set to 16, the game is over
/ / target
int tarPos[][2] = {{9.9},
	{9.10},
	{9.11},
	{10.9},
	{10.10},
	{10.11},
	{11.8},
	{11.9},
	{11.10},
	{11.11}};Copy the code
  • The required variables
char key = 0;	// Store the user's keys
int row = 0, column = 0;	// Where are the characters in the row and column
int oldType = 0;	// What ground does the character stand on
int oldBoxType = 0;	// What ground did the box stand on
Copy the code
  • Start drawing a map
// Draw a map
void DrawMap(a) {
    for (int i = 0; i < sizeof(map) / sizeof(map[0]); i++)
    {
        for (int j = 0; j < sizeof(map[0) /sizeof(map[0] [0]); j++)
        {
        	switch (map[i][j])
        	{
        	case 0:
        		printf("");
        		break;
        	case 1:
        		printf("Do");
        		break;
        	case 2:
        		printf("■");
        		break;
        	case 4:
        		printf("▓");
        		break;
        	case 8:
        		printf("♀");
        		row = i;
        		column = j;
        		break;
        	case 16:
        		printf("★");
        		break; }}printf("\n"); }}Copy the code
  • The method of moving characters and boxes according to the position information transmitted
void Move(int r, int c)
{
	if (map[row + r][column + c] ! =2)	// If the front is not a wall, it can be moved
	{
		if (map[row + r][column + c] == 4 || map[row + r][column + c] == 16)	// If you encounter the box handling method
		{
			if (map[row + 2 * r][column + 2* c] ! =2 && map[row + 2 * r][column + 2* c] ! =4 && map[row + 2 * r][column + 2* c] ! =16)	// The box is not behind the wall and box
			{
				// Move the box
				if (map[row + r][column + c] == 16)
				{
					oldBoxType = 1;
				}
				else if (map[row + r][column + c] == 4)
				{
					oldBoxType = 0;
				}
				map[row + r][column + c] = oldBoxType;
				if (map[row + 2 * r][column + 2 * c] == 1)
				{
					map[row + 2 * r][column + 2 * c] = 16;
				}
				else
				{
					map[row + 2 * r][column + 2 * c] = 4; }}else {
				return;	// If there is a wall behind the box, stop moving}}map[row][column] = oldType;	// Set the area the character walks through to the original ground type
		oldType = map[row + r][column + c];	// Store the type of ground where the character will walk next
		map[row + r][column + c] = 8;	// The position of the person moving changes}}Copy the code
  • Check whether it is complete
void resultCheck(a)
{
	bool result = true;
	for (int i = 0; i < sizeof(tarPos) / sizeof(tarPos[0]); i++)
	{
		if (map[tarPos[i][0]][tarPos[i][1]] != 16)
		{
			result = false; }}if (result)
	{
		MessageBox(NULL, TEXT("You win!!"), TEXT("Game over."), MB_OK);
		exit(0); }}Copy the code
  • The main function
int main(void)
{
	DrawMap();
	while (true)
	{
		key = _gettch();	//_gettch() can be used to listen for keyboard keys
		switch (key)
		{
		case 'a':
			Move(0.- 1);
			break;
		case 'd':
			Move(0.1);
			break;
		case 'w':
			Move(- 1.0);
			break;
		case 's':
			Move(1.0);
			break;
		}
		resultCheck();  // Check if it is complete
		system("cls");	// Clear the screen
		DrawMap();  // Redraw the diagram
	}
	return 0;
}
Copy the code