Two hundred lines of Python code will take you to create a "Daily Cool Running" game! Easy to understand, clear thinking!

Recently, the editor found that it has not been a long time for everyone to show cases of interesting games. I just happened to write a "Daily Cool Running" game during the weekend, so I used it to share with you and relive the classic cool running game with everyone.

Prepare the development environment

 

As the saying goes, "If you want to be good at work, you must first sharpen your tools." Let's first introduce what libraries we use this time. (It should be noted that because many friends will report that after many libraries are installed, there will be errors and the corresponding modules cannot be found). This is because the library versions are different, so I will introduce each program used in detail. Links and their corresponding versions, the tools we used this time and their version numbers are as follows:

PyCharm:2019.1.3

python3.5+, with built-in sys and random libraries

pygame:1.9.6

 

 

02

The overall idea of ​​the program

 

After introducing our basic environment, let's introduce the idea of ​​today's game design.

1). Game initialization

  • Use pygame to create a game window of a specific size.

  • Display the initial interface of the game and prompt the user to start the game

  • Set a background picture in the game interface and display objects such as characters, obstacles, and gold coins.

2). Game control

  • The character runs forward automatically, press the space bar to control the character to jump.

  • Use the addObstacle function to create obstacles.

  • Use the updateScreen function to continuously update the display of objects in the interface.

3). Collision detection

  • Use the ListenKeyBoard function to monitor the user's keyboard input, and detect whether people collide with obstacles, gold coins, etc.

  • Collision between characters and obstacles: When it is detected that there is an intersection between the position information of the characters and the obstacle, it is judged as a collision, the character's life value is reduced by one, and the obstacle is eliminated.

  • Collision between character and gold coin: When the character hits the gold coin, the gold coin is destroyed and the score is +100.

  • When the character successfully avoids obstacles, the score is +10.

  • Use the judgeState function to determine whether the game is over.

  • At the end of the game, the final score is displayed, and the user is prompted to press "Enter" to restart the game.

 

 

03

Take you step by step to write the game

 

1). First, take a look at how the program is initialized as a whole:

 

The program is implemented by the pygame library as the main body. First, the size of the game window and the background images of different modules of the program, such as the picture of obstacles, the background image during the game, and the background image at the end of the game, are defined in the program.

 

2). The game start interface

After running the program, the program first runs the function self.startGame, and gives the game introduction interface, prompting the user to start the game.

 

In the self.startGame function, the initial interface of the game is drawn first, and then the user's keyboard input is monitored. When the user outputs the "ESC" key, the game is directly exited, otherwise, the real game is entered. The initial interface is shown in the figure below:

 

3). Create various objects

After entering the game, we need to create characters, obstacles, gold coins, and game background settings. Since the characters and obstacles have been created in the game background by the way of classes, and their class forms are very similar, here we will briefly explain the obstacles and gold coins.

 

In the obstacle class, the initial constructor of the class mainly sets the display image (self.surface), coordinate information (self.x, self.y) and length and width information (self.w, self.h) of the class object. ). The self.getPos function is used to return the coordinates and length and width information of the current object, and the self.judgeCollision function is used to detect the collision of the object.

 

In the program, you can add obstacle objects through the self.addObstacle function, and randomly set the height position of the obstacle in the game interface. The obstacle target in the game is shown in the figure below.

 

In the above picture, the obstacles are framed by the red frame. It can be seen that there are different height coordinates and different interval widths between the obstacles, so as to bring different difficulty challenges for parkour characters. The character is the target framed by the blue frame, and located directly above the screen, framed by the green frame is the information of "remaining health and score".

 

4). User keyboard monitoring and collision detection

By monitoring the user's keyboard input, control the movement of the character, such as jumping or exiting the game.

 

In the above figure, we can see that when the user enters the space bar, if the character's current state is 0, the character must complete the first level jump, if the character's current state is 1, then the character must complete the second level jump. If the current self.game_state is 1, it means that the character is dead, and the game will restart when the user enters the "Enter" key.

 

For collision detection between people and obstacles or gold coins, the judgment is made through the self.judgeCollision function in the obstacle class.

 

If it is judged to be a gold coin, the user's score is +100 and the gold coin is eaten. Otherwise, the user's life value is reduced by one and the obstacle disappears.

 

5). The game interface is constantly refreshed and the game is over

Through the self.updateScreen function, the game interface can be continuously refreshed.

 

In the above program, after updating the position of the game background, the remaining life value and the value of the score, the number of obstacles, and the movement state of the characters, they can be displayed in the game interface using the screen.blit function. Through continuous Update to make the game look like a process of constant movement.

 

The self.judgeState function is used to determine whether the game is over. If the game is over, the points obtained by the user will be displayed and the user will be prompted to press the "Enter" key to restart the game.

 

 

The above is the sharing and explanation of the parkour program that Xiaobian brought to you today. If you are interested, you can find the source code of the assistant in the background of the official account. Feel the fun of classic parkour games.

How to get the source code:

 

*Disclaimer: This article is organized on the Internet, and the copyright belongs to the original author. If the source information is wrong or infringes on rights, please contact us to delete or authorize the matter

Recently, many friends consulted about Python learning issues through private messages. To facilitate communication, click on the blue to join the discussion and answer resource base by yourself

Guess you like

Origin blog.csdn.net/weixin_43881394/article/details/112525642