Use python to make a small game code, use python to make a small game

Hello everyone, the editor is here to answer the following questions for you, how to write a simple game in python and make a small game code in python. Let’s take a look today!

Today, I will show you some interesting little things about Python, and show you whether the little games I made have any memories of your childhood. Without further ado, let’s show it to you! If you are interested, it’s okay if you don’t understand. You can download PyCharm and follow my picture code!

First of all, we choose the Python version of the development tool: 3.6.4. For the relevant templates, choose the pygame template, as well as some of the modules that come with Python. Setting up the environment is also very simple. You only need to install Python and add it to the environment variables, and pip installs the required relevant templates.

Implement step by step:

Step1: Define the game elf class

Since the game involves collision testing, we first define some game sprite classes, including people pushing boxes, boxes, walls and target location indicators.

First, let’s define the elf class of the person pushing the box:

He needs to have the ability to move. Here, a simulated movement option is set up to determine whether it can move up, down, left, and right through simulated movement. Because of the properties and types of other things on the map, we define them as the same elf class (in fact, they have similar properties, but I feel it is still necessary to distinguish between people and objects):

Among them, the boxes have the ability to be moved, while others cannot be moved. Simulated move selection functions similarly to before.

Step2: Define the game map class

Here we define a game map class, the purpose is to use this class to create any game map. Therefore, this class should be able to add and save game elements (people, walls, boxes, etc.) and draw the map on the screen. At the same time, you should also bring a method to determine whether the box on this map has been delivered to the specified location (this makes it easier to change levels):

Step3: Define the game interface class

The game interface class is responsible for parsing the map files of each game level in the levels folder, and using the game map class to create and display the game map:

At the same time, because the game map area > the game window interface, this class needs to add the function of scrolling the game map according to the character's position:

Step4: Define the main game loop of a certain level

The main loop is mainly responsible for instantiating the game interface class and performing some operations on the game interface class based on the results of key detection:

The logic of character movement is: if the character moves to a blank space, the person moves; if it hits a box, the box can move one space in the same direction as the person, then both the person and the box move; in other cases, neither the person nor the box can move.

Step5: Define the game start, switch and end interfaces

It's relatively simple to do, so just get the source code.

Start screen:

Switch interface:

End interface:

Step6: Implement the main function of the game

Just string all the interfaces together:

Finally, I casually added some background music, which shouldn't be a big problem. Students who understand it can try to do it. Students who don't understand it can just type according to the code. After running, it will be as shown in the figure:

If no one understands it, everyone is welcome to leave a comment! Or you can send me a private message. Comments will be answered and private messages will be read. You won’t miss any students who are studying seriously! ! !

Guess you like

Origin blog.csdn.net/i_like_cpp/article/details/132163881