One of the reasons why Python is so popular is that it is used in a wide variety of fields, including game development. Instead, the preferred module for game development in Python is PyGame.
1. Getting to know Pygame first
PyGame is a cross-platform Python module designed for video games, including images, sounds, etc., built on the basis of SDL (Simple DirectMedia Layer), allowing real-time video game development without being affected by low-level languages such as C or lower-level Assembly language bound. Based on such a vision, all required game functions and concepts (mainly graphics) are completely simplified into the game logic itself, and all resource structures can be provided by high-level languages (such as Python).
1. Install Pygame
The official website of PyGame is www.pygame.org. You can view the relevant documents of PyGame at this URL. The installation command for PyGame is simple:
pip install pygame
Check if the module is installed successfully:
import pygame
pygame.ver
The result of the operation is as follows:
2. Common modules of Pygame
The advantage of Pygame in game development is that you don't need to think too much about the underlying content, and you can focus on the game logic. For example, PyGame integrates many low-level related modules, such as accessing display devices, managing events, using fonts, etc.
Pygame commonly used modules are as follows:
module name
Function
pygame.cdrom
Access the optical drive.
pygame.cursors
Load cursor.
pygame.display
Access display devices.
pygame.draw
Draw shapes, lines and points.
pygame.event
Manage events.
pygame.font
Use fonts.
pygame.image
Load and store images.
pygame.joystick
Use a gamepad or something similar.
pygame.key
Read keyboard keys.
pygame.mixer
sound.
pygame.mouse
mouse.
pygame.movie
Play video.
pygame.music
play music.
pygame.overlay
Access advanced video overlays.
pygame.rect
Manage rectangular areas.
pygame.sndarray
Manipulate sound data.
pygame.sprite
Manipulate moving images.
pygame.surface
Manage images and screens.
pygame.surfarray
Manage bitmap image data.
pygame.time
Manage time and frame information.
pygame.transform
Zoom and pan the image.
Use Pygame's display module and event module to create a PyGame window, the code is as follows:
Create a game window, and then create a small ball inside the window. Move the ball at a certain speed. When the ball hits the edge of the game window, the ball bounces back and continues to move. (1) Create a game window with width and height set to 640*480:
In the above code, first import the pygame module, and then call the init() method to initialize the pygame module. Next, set the width and height of the window, and finally use the display module to display the form.
Common methods of the display module:
method name
Function
pygame.display.init
Initialize the display module.
pygame.display.quit
End the display module.
pygame.display.get_init
Returns True if the display module has been initialized.
pygame.display.set_mode
Initializes an interface ready to be displayed.
pygame.display.get_surface
Get the current Surface object.
pygame.display.flip
Update the entire Surface object to be displayed on the screen.
pygame.display.update
Update part of the content to be displayed on the screen. If there is no parameter, it has the same function as flip.
(2) When running the above code, a flashing black window will appear, because the program will automatically close after execution is completed. If you want to keep the window displayed, you need to use while True to keep the program running. In addition, you need to set the close button. The code is as follows:
In the above code, polling event detection is added. pygame.event.get() can get the event queue, use for...in to traverse the event, and then judge the event type according to the type attribute. The event processing method here is similar to that of GUI. For example, event.type equal to pygame.GUIT means that the event of closing the pygame window is detected, pygame.KEYDOWN means the event of keyboard press , pygame.MOUSEBUTTONDOWN means the event of mouse press , etc. (3) Add a small ball in the window. We first prepare a ball.png image, then load the image, and finally display the image in the window:
The above code uses the load() method of the image module to load the image, and the return value ball is a Surface object. Surface is a pygame object used to represent a picture, and various operations such as painting, deformation, and copying can be performed on a Surface object. In fact, the screen is just a surface, and pygame.display.set_mode returns a screen Surface object. If you draw the Surface object of ball to the screen.Surface object, you need to use the blit() method, and finally use the flip method of the display module to update the entire Surface object to be displayed on the screen.
The result of the operation is as follows:
Common methods of the Surface object are as follows:
method name
Function
pygame.Surface.blit
Draw an image onto another image.
pygame.Surface.convert
Converts the pixel format of an image.
pygame.Surface.convert_alpha
Convert the pixel format of the image, including the conversion of the alpha channel.
In the checkDead() method of the above code, upRect.colliderect(Bird.birdRect) is used to detect whether the bird’s rectangular area collides with the upper pipe’s rectangular area, and the parameter of the colliderect() function is another rectangular area object. The death of the bird after the collision is as follows:
This example has realized the basic functions of Flappy Bird, but there are still many areas to be improved, such as setting the difficulty of the game, including setting the height of the pipe, the flying speed of the bird, etc.
4. Summary
It mainly explains how to use Pygame to develop games. First understand the basic usage of Pygame through a jumping ball game, and then use Python to gradually develop a well-known game Flappy Bird. Through the study of this chapter, you can master the basic knowledge of Pygame, and use the Python object-oriented way of thinking to develop a small Python game, and further experience the fun of Python programming.