SoFunction
Updated on 2025-03-02

Write a box-pushing game based on C++

Write a box-pushing game based on C++

Updated: September 14, 2023 09:16:36 Author: Qiang Guohao, Lin Zhonglin
This article mainly introduces the detailed introduction of writing a box-pushing game based on C++. The sample code in the article is introduced in detail and has a certain reference value. Interested friends can refer to it.

Code above:

#include <>
#include <>
#include <>
int  map[2][7][8] =
{
	//0:Empty 1:■ :Wall	//3:☆ 4: ★ //Destination and box	//5: ※ //People	//7:⊙ //Purpose (3) and box (4) together	//8: ※ //People (5) and purpose (3) are together	// Use an algorithm for multiple situations	{
	1, 1, 1, 1, 1, 1, 1, 1,
	1, 0, 0, 0, 0, 0, 0, 1,
	1, 3, 1, 0, 1, 1, 3, 1,
	1, 4, 0, 0, 4, 0, 3, 1,
	1, 0, 1, 0, 1, 1, 4, 1,
	1, 0, 0, 5, 0, 0, 0, 1,
	1, 1, 1, 1, 1, 1, 1, 1
	},
	{
		1, 1, 1, 1, 1, 1, 1, 1,
		1, 0, 0, 0, 0, 0, 0, 1,
		1, 3, 1, 0, 1, 1, 3, 1,
		1, 3, 4, 5, 4, 0, 3, 1,
		1, 4, 1, 0, 1, 1, 4, 1,
		1, 0, 0, 0, 0, 0, 0, 1,
		1, 1, 1, 1, 1, 1, 1, 1
	}
};
int cas = 0;	//To be 0 means the first level//Record the number of boxes in each level or the total number of items and purposes togetherint boxSum[2] = {3,4};
// Map drawingvoid drawMap()
{
	for (int i = 0; i < 7; i++)
	{
		for (int j = 0; j < 8; j++)
		{
			if (j == 0)
				printf("\t\t");
			switch (map[cas][i][j])
			{
				//	//0:Empty 1:■ :Wall			case 0:
				printf("  ");
				break;
			case 1:
				printf("■");
				break;
				//3:☆ 4: ★ //Destination and box			case 3:
				printf("☆");
				break;
			case 4:
				printf("★");
				break;
				//5: ※ //People			case 5:
			case 8:
				printf("※");
				break;
			case 7:
				printf("⊙");
				break;
				//7:⊙ //Purpose (3) and box (4) together				//8: ※ //People (5) and purpose (3) are together			}
		}
		printf("\n");
	}
}
//Key processingvoid keyDown()
{
	//Analyze the key pressing process	// Where is the positioning person	//There are two kinds of situations for people: the first is: people, and the second is: people stand on the purpose	int i, j;
	for (i = 0; i < 7; i++)
	{
		for (j = 0; j < 8; j++)
		{
			if (map[cas][i][j] == 5 || map[cas][i][j] == 8)
			{
				break;
			}
		}
		if (map[cas][i][j] == 5 || map[cas][i][j] == 8)
		{
			break;
		}
	}
	char ch = _getch();	//Invisible character input, + header file	switch (ch)
	{
		//72 80   75 77
	case 'w':
	case 'W':
	case 72:
		//The next place is equal to an open space or a purpose can be walked		if (map[cas][i - 1][j] == 0 || map[cas][i - 1][j] == 3)
		{
			//3+5=8: means the purpose is to be with people			//The new place (map[i-1][j]) people (5) are here			map[cas][i - 1][j] += 5;
			//The old place (map[i][j]) people (5) left			map[cas][i][j] -= 5;
		}
		//If the next one is a box, you need to further judge that you can leave		//Note: Boxes are two states: Boxes, boxes and purpose are together		else if (map[cas][i - 1][j] == 4 || map[cas][i - 1][j] == 7)
		{
			//Judge whether you can leave in the next place where you make the box			if (map[cas][i - 2][j] == 0 || map[cas][i - 2][j] == 3)
			{
				//The new place box is here				map[cas][i - 2][j] += 4;
				//The position of the box: The box (-4) left and the person comes (+5)				map[cas][i - 1][j] += 1;
				//The original place has left				map[cas][i][j] -= 5;
			}
		}
		break;
	case 's':
	case 'S':
	case 80:
		//The next place is equal to an open space or a purpose can be walked		if (map[cas][i + 1][j] == 0 || map[cas][i + 1][j] == 3)
		{
			//3+5=8: means the purpose is to be with people			//The new place (map[i-1][j]) people (5) are here			map[cas][i + 1][j] += 5;
			//The old place (map[i][j]) people (5) left			map[cas][i][j] -= 5;
		}
		else if (map[cas][i + 1][j] == 4 || map[cas][i + 1][j] == 7)
		{
			//Judge whether you can leave in the next place where you make the box			if (map[cas][i + 2][j] == 0 || map[cas][i + 2][j] == 3)
			{
				//The new place box is here				map[cas][i + 2][j] += 4;
				//The position of the box: The box (-4) left and the person comes (+5)				map[cas][i + 1][j] += 1;
				//The original place has left				map[cas][i][j] -= 5;
			}
		}
		break;
	case 'a':
	case 'A':
	case 75:
		//The next place is equal to an open space or a purpose can be walked		if (map[cas][i][j - 1] == 0 || map[cas][i][j - 1] == 3)
		{
			//3+5=8: means the purpose is to be with people			//The new place (map[i-1][j]) people (5) are here			map[cas][i][j - 1] = map[cas][i][j - 1] + 5;
			//The old place (map[i][j]) people (5) left			map[cas][i][j] = map[cas][i][j] - 5;
			//j+=5  j=j+5
		}
		else if (map[cas][i][j - 1] == 4 || map[cas][i][j - 1] == 7)
		{
			//Judge whether you can leave in the next place where you make the box			if (map[cas][i][j - 2] == 0 || map[cas][i][j - 2] == 3)
			{
				//The new place box is here				map[cas][i][j - 2] += 4;
				//The position of the box: The box (-4) left and the person comes (+5)				map[cas][i][j - 1] += 1;
				//The original place has left				map[cas][i][j] -= 5;
			}
		}
		break;
	case 'D':
	case 'd':
	case 77:
		//The next place is equal to an open space or a purpose can be walked		if (map[cas][i][j + 1] == 0 || map[cas][i][j + 1] == 3)
		{
			//3+5=8: means the purpose is to be with people			//The new place (map[i-1][j]) people (5) are here			map[cas][i][j + 1] += 5;
			//The old place (map[i][j]) people (5) left			map[cas][i][j] -= 5;
		}
		//The next place is a box, to determine whether the next place of the box is the purpose and empty space.		else if (map[cas][i][j + 1] == 4 || map[cas][i][j + 1] == 7)
		{
			//Judge whether you can leave in the next place where you make the box			if (map[cas][i][j + 2] == 0 || map[cas][i][j + 2] == 3)
			{
				//The new place box is here				map[cas][i][j + 2] += 4;
				//The position of the box: The box (-4) left and the person comes (+5)				map[cas][i][j + 1] += 1;
				//The original place has left				map[cas][i][j] -= 5;
			}
		}
		break;
	}
}
//Performance judgment//What to use to determine the outcome: The number of boxes reaching their destinationint gameOver()
{
	int count = 0;
	//Search all the places	for (int i = 0; i < 7; i++)
	{
		for (int j = 0; j < 8; j++)
		{
			if (map[cas][i][j] == 7)
				count++;
		}
	}
	return count;
}
//When the number of boxes is zero, it is also a victoryint gameOver2()
{
	int count = 3;
	//Search all the places	for (int i = 0; i < 7; i++)
	{
		for (int j = 0; j < 8; j++)
		{
			if (map[cas][i][j] == 3)
				count--;
		}
	}
	return count;
}
int main()
{
	while (1)
	{
		printf("\n\tUse the arrow keys or w a s d keys to move ※Push ★ into☆\n",cas+1);
		printf("\n\t\t There are two levels in total, %d level\n",cas+1);
		drawMap();
		if (gameOver() == boxSum[cas])
		{
			cas++;
			if (cas == 2)
				break;
			else
			    printf("\n\t\t This level is OK! Press any button to continue\n");	
		}
		keyDown();
		system("cls");
	}
	printf("game over!");
	printf("\n");
	system("pause");
	return 0;
}

This is the end of this article about writing a box-pushing game based on C++. For more related content of C++ box-pushing game, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!

  • C++
  • Push the box
  • game

Related Articles

  • C++ logarithmic implementation example

    Logarithms are used to verify the correctness of algorithms on your own local platform. This article introduces the implementation of C++ logarithms in detail. The example code in the article is very detailed and has certain reference value. Interested friends can refer to it.
    2021-08-08
  • Visual Studio Community 2022 (VS2022) installation graphic and text method

    This article mainly introduces the installation method of Visual Studio Community 2022 (VS2022). This article introduces you in steps in a very detailed way through pictures and texts. It has certain reference value for your study or work. Friends who need it can refer to it.
    2022-09-09
  • Detailed explanation of the stacking order of function parameters in C language and examples

    This article mainly introduces the detailed explanation of the stacking order of function parameters in C language and relevant information about the examples. Friends who need it can refer to it
    2017-02-02
  • cin implements input string method in c++

    This article mainly introduces the method of cin implementing input strings in C++, which is of good reference value and hopes to be helpful to everyone. If there are any mistakes or no complete considerations, I hope you will be very grateful for your advice
    2022-11-11
  • C++ implementation stack operations (push and pop)

    This article mainly introduces the operations (push and pop) of C++ implementation stack, which is of good reference value and hopes to be helpful to everyone. If there are any mistakes or no complete considerations, I hope you will be very grateful for your advice
    2022-07-07
  • Example sharing cmake compiles a simple c++ project (demo)

    The following is a small example to illustrate that cmake compiles a C++ project and generates an executable file. Friends who need it can refer to it.
    2020-02-02
  • Interview FAQs: The Difference between C Language and C++

    In C, variables or functions modified with static are mainly used to indicate that this variable or function can only be accessed in the code block of this file, and the code outside the file is not authorized to access. Today, I will introduce to you the common differences between C language and C++ in the interview. Interested friends follow the editor to take a look.
    2021-05-05
  • Detailed solution to the problem of "Cannot start the program, the system cannot find the specified file" when writing C programs or CUDA programs in VS2019

    This article mainly introduces the detailed solution to the problem of "cannot start the program, and the system cannot find the specified file" when writing C programs or CUDA programs in VS2019. The article uses very detailed pictures and texts, which has certain reference learning value for everyone's study or work. Friends who need it, please learn with the editor below.
    2020-08-08
  • C language arithmetic operator sorting

    Arithmetic operators are used for various numerical operations, including addition (+), subtraction (-), multiplication (*), division (/), rematch (or modulo operation, %), self-increase (++), and self-decrease (--) in total.
    2023-03-03
  • Description of the usage of scanf function and space carriage return in C language

    This article mainly introduces the usage description of the scanf function and space carriage return in C language, which is of good reference value and hopes to be helpful to everyone. Let's take a look with the editor
    2020-12-12

Latest Comments