Python project practice "Plane War Game"

Table of contents

1. Introduction to Pygame library package

2. Pygame installation

3. Project development ideas

3.1 Preface

3.2 Aircraft War Development Steps


1. Introduction to Pygame library package

Pygame is a python-based game development library that provides a series of tools and interfaces that enable developers to easily create various types of games, including 2D games and simple 3D games. To put it bluntly, it is a directory package that contains many defined function modules and attributes.

2. Pygame installation

Windows as an example:

 #Tutorial on using modules in the pygame library: https://www.pygame.org/docs

 

 

3. Project development ideas

3.1 Preface

__name__ attribute

I know that there may be multiple "*.py" files in a project, and each "*.py" file defines many functional functions to be used as module files. After the module file is created, in order to test whether the functions defined in the module can be used, developers usually call the function methods they wrote in the module file. In this way, when the main "*.py" file of the program wants to use a function in the module, it needs to import the corresponding module file into the startup "*.py" file (importing the module is equivalent to copying the code in the module file into this "*.py" import location); this will cause when a function in the module is called, the code that has been written to call the function in the module file will also be executed. Therefore, in order to avoid this kind of thing, we can add a "__name__="__main__" judgment to the function call in the module file before calling the function.

1. When the "*.py" file is used as a startup file, the value of __name__ in the file is "__main__"

2. When the "*.py" file is imported into the "*.py" startup file as a module, the __name__ attribute value in the module is equal to the file name of its module, and the value of __name__ in this startup file is "__main__".

Summary: If the .py file where __name__="__main__" is imported as a module, the __name__ value in the file is equal to the module file name. But if you run it directly from a local file, the value of __name__ is "__main__".

3.2 Aircraft War Development Steps

Note: The source code resource package of "Plane War Game" has been uploaded to the top of this article. Welcome to download and test!

1. Create a game window

        1. Set the window size

        2. Set the window title

        3. Set window icon

2. Add sound effects

        1. Game background sound effects

        2. The enemy aircraft is hit and explodes with sound effects

3. Define enemy and bullet lists to save corresponding instance objects

4. Loop to create enemy instance objects

5. Define the Pythagorean distance function

6. Define the score font display function

7. Define the game end slogan display and object clearing functions

8. Define a list to save bullets and enemy instance objects

9. while loops all the time

*The dynamic changes in the game screen that we see are actually similar to the principle of video playback. They are all related pictures that are displayed on the screen in turn. When the user looks at the screen with the naked eye, due to the fast carousel speed of the front and back pictures on the screen, it gives the user a dynamically changing video feeling; in fact, the dynamic videos that the user sees are the dynamic effects displayed by the carousel of pictures one by one. Therefore, loops can be used in games to show the real-time position changes of game species to achieve dynamic visual effects.

        1. Draw the background image

        2. Player keyboard or mouse event monitoring

                ●When the player presses different keys, the corresponding position of the player's aircraft will also change.

                ●The player presses the space bar to fire a bullet to create a bullet instance object

        3. Display player position

                ●Define and create player aircraft classes and instance objects

                ●Define instance attributes: The initial position of the x,y axis picture of the player's aircraft.

                                          Define the player's movement speed, that is, the coordinate change value variable

                                          Load player aircraft image

               ● Define the player movement boundary control method (can only move within the specified x-axis horizontal range)

        4. Show enemies

                ●Define enemy classes

                ●Instance attributes: Randomly define the initial coordinates x and y values ​​of the enemy picture

                                    Define enemy movement speed variables

                                    Load enemy images

                ●Define the enemy movement method: the default is to move horizontally, move in the opposite direction beyond the left and right boundaries and take one step forward towards the player. When reaching the bottom of the screen or being hit by a bullet, the enemy position will be re-randomly initialized to give the player the illusion of a constant stream of enemies.

        5. Display player bullets

                ●Define bullet class

                ●Define bullet class instance attributes: Bullet initial position coordinates (always a little above the player's aircraft head)

                                                       Bullet moving speed

                                                       Load bullet image

                ●Define the method of bullet hitting the enemy: Define the Pythagorean theorem function to measure the distance between the bullet and the aircraft; if the distance between the two is within a certain range, it means hitting the enemy---->The bullet and the enemy disappear (the enemy explodes when hit) Sound effect)---->Delete the bullet object in the list and reset the enemy aircraft position.

               ●Define the bullet movement method: The bullet moves upward once each loop; if the bullet reaches the top of the window, it disappears.

        6. Display player scores

                ●Outside the loop: Accumulate the fraction variable by +1; initialize the font function; set the font and its size.

                ●Define the score font display function: Define the font content variable; infect the decorative font (color) through the font variable; display the score font.

        7. Display game over

                ●Call the game end function (the game ends when the player's plane collides with the enemy's plane ---> clean up all objects ----> display the game end banner)

        8. Update the window content (for each cycle, the position changes of each object in this round will be displayed on the background image)

@Statement: The knowledge level of the blogger of "山月仁无声" is limited. If there are any inaccuracies in the above article, IT enthusiasts are welcome to correct me. I will definitely learn from you with an open mind!

Guess you like

Origin blog.csdn.net/qq_60243891/article/details/132525268