C language, as the first programming language for most people, is of self-evident importance. Many programming habits and logical ways have been formed at this time. This is a small box pushing game I wrote after LEARNING C language in my freshman year. My logical ability has been improved. I would like to share this small box pushing game project with you here.

GitHub storage address: github.com/weizhiwen/C…

Let’s take a look at the final run.

This is a small game in Windows Dos interface, the interface has a map pushing boxes, using # to represent the map boundary, P to represent the small person pushing boxes, X to represent boxes, O to represent the box to push to the target position.

W (W), S (S), A (A) and D (D) correspond to the upward, downward, left and right movement of the SIMS respectively.

To write this little game, we faced the following problems.

  • 1. How to save the game map?

  • 2. How does the game work?

  • 3. How does the game map change when the position is fixed?

  • 4. How to write the movement logic of the villain?

  • 5. How does the game end?

1. How to save the game map?

There are only basic data types in C language, and the game map is a two-dimensional plane structure. It is easy to think of using a two-dimensional array to save the game map. For details of the code, see the level.

2. How does the game work?

Since a box pushing game takes input from the user until the game is over, we can set a flag to determine whether the game is over, setting this flag as a condition for a while loop. In each cycle, the user’s input is received, and the next operation is carried out according to the value entered by the user. In the game, it is the movement direction of the villain, up, down, left and right. Here we can use a switch statement to judge. Each loop corresponds to a user input.

3. How does the game map change when the position is fixed?

In each cycle, the current map should be displayed first, so that the user can move input next time. We set the game map as a global variable so that character changes on the map are permanent after the avatar moves, and then print a new map with partial changes. In this process, the characters on the game map can be constantly changed, but the position of the map can not be fixed. If we can refresh the value on the screen, we can keep changing the position. Refresh essence is to eliminate the old and usher in the new, that is, remove the original and usher in the new. In the program, we can remove the original interface, and display the new interface in the original location. C can use the system(” CLS “) function to clear the console contents, and then we can display the new map contents.

The movement logic of the villain belongs to the specific program implementation, we put it below again, first to say how the program ends.

4. How does the game end?

We talked about setting a flag to determine if the game is over, but when is the game over? The game goal of pushing chests is to push each chests to a target position, which is a game over situation and can be written as a function since each loop is judged. In addition, if the user wants to quit, this is also a game over situation, and I have only considered these two cases here, but the rest is up to the reader.

At this point we can write the outline of the program, a big loop outside, and each loop refreshes the interface, receives user input, processes user input, and decides if the game is over.

5. How to write the movement logic of the villain?

In the screenshot above, you can see that I have put the movements of the avatar up, down, left, and right into four functions, MoveToUp(), MoveToDown(), MoveToLeft(), and MoveToRight(). Take MoveToUp() as an example to analyze the logic of the movement of the little man.

Theoretically, the SIMS can move up and down, but due to the restrictions of the map, the SIMS can not pass through the wall, can only move on the allowed road, such as the following situation, the SIMS want to move up, is certainly not allowed.

And in this case, the little man can move up, because there’s no constraint on the little man.

Therefore, we need to judge the space (the next position) to which the villain can theoretically move. If it is not a constraint (the box and the position to move the box will be described in detail below), the villain can move, and if there is a constraint, it cannot move. Therefore, we need to record the value of a coordinate point. Here, the reference of “next position” can select the current position of the villain. When the game starts, the starting position of the villain is taken as the current position. When the person moves up, the x-coordinate of the “next position” is the x-coordinate of the person’s current position minus one, and the y-coordinate is the x-coordinate of the person’s current position. And then we can according to the horizontal ordinate “next position” to find a specific character values, if it is empty, you can move, if it is the target location to move the box, man can also move, there is also a kind of circumstance is the next position is box, we consider the “next position” of box, box of the next position is very good also. Since the man and the box move in a line, as the man moves up, the x-coordinate of the box’s “next position” is the same as the man’s “next position” minus one. We also need to determine the character value of the box’s “next position”. If the character value is a space and the box can be moved, it can be moved. The code for moving the little man up is as follows:

The code for moving the little man down, left and right is similar, except that the next coordinate of moving the little man is changed. Moving down, the abscissa of “next position” is the abscissa of the little man plus one, and the ordinate of the two is the same. For details of the code, see the control.

At this point the whole program is done, you can run the whole program and it looks like this, can you find any bugs?

I believe you have found carefully, when the villain moved to the box to move the target position, and then moved out, the position will “disappear”, why this situation? We always focus on the dog to move in front of “the next position” and the “next” to move the box, but there was no attention, before moving to this place position (a) the original value, we can record the “on a location” values, but consider this problem is much, especially the case both in the box moving target location, The situation is complicated, so is there an easy way? In fact, so far, our program has been basically fine, except for the “character disappearance” of the target location where the box is moving. This is just a Bug. How does Microsoft do when it tests users? After the system is released and constantly issued patches, we can also put a “patch” to this program. The boxes are going to move in the same place, so can we use a two-dimensional array to store those particular positions? The values of these special positions are also special, either the target position, or the box, or the person, not the whitespace character, so we can write a “patch” — a function to fix this Bug. When the person moves, add the following repair function to the end of the movement function in each direction. If it is a whitespace character, change the value of the special position to the character value of the target position, in this case the character “O”. This “fixes” the Bug in the program, and the problem of “disappearing characters” is solved.

I’ve divided the program into separate files. The GitHub repository also has a description of the program directory. When reading the code, the reader will notice the extern keyword, which is used to allow a variable or function to be shared between split files. Change the game map in the level, you can achieve multiple levels of pushing boxes, readers are interested in trying to improve, this article also plays a role of throwing a brick to attract jade.

Finally, I want to say that programming is very logical, no matter what language is used, the logic of the program is the same, but which language is more convenient, faster. Write a program to really play logic, only clear logic, code can write well, otherwise at most is the code porter.

Welcome to pay attention to my wechat public number: “programming heart road”, in the way of programming, we grow together, reply to any keyword will have a surprise oh!