Table of contents
2. Introduction to common modules of Pygame
2.1 Common methods of display module
2.2 Common methods of pygame.event module
2.3 Common methods of Surface objects
1.1 After adding an infinite loop, the form does not respond.
Describe the basic knowledge about Pyganme development.
1. Overview of Pygame
Pygame is a cross-platform python module, which is converted to video game design (including images and sounds), and is built on the basis of SDL (Simple DirectMedia Layer), without the comfort of low-level languages (such as assembly). All game functions and concepts are reduced to the game logic itself, and all resource structures can be provided by a high-level language.
1. Install Pyganme
Pygame official website: http://www.pygame.org , you can find relevant documents on the official website.
Install:
pip install pygame
Installation check:
# _*_ coding:utf-8 _*_
import pygame
pygame.ver # 查看pygame版本
As a result, the version displayed indicates that the installation was successful:
pygame 2.1.2 (SDL 2.0.18, Python 3.7.8)
Hello from the pygame community. https://www.pygame.org/contribute.html
>>>
2. Introduction to common modules of Pygame
module | illustrate |
---|---|
pygame.cdrom | access drive |
pygame.cursors | load cursor |
pygame.display | access display device |
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 tethered handle or something similar |
pygame.key | read keyboard keys |
pygame.mixer | sound |
pygame.mouse | mouse |
pygame.music | play audio |
pygame.overlay | Access advanced video overlays |
pygame.reet | Manage Rectangular Areas |
pygame.sndarray | Operating sound data |
pygame.sprite | Manipulate moving images |
pygame.surface | Manage images and screens |
pygame.surfarray | Manage bitmap data |
pygame.time | Manage time and frame information |
pygame.transform | Zoom and move images |
2.1 Common methods of display module
method | illustrate | illustrate |
---|---|---|
pygame.display.init() | Initialize the display module | |
pygame.display.quit() | End display module | |
pygame.display.get_init() | Returns whether to initialize, initialization returns True | |
pygame.display.set_mode() | Initialization interface | set_mode(width,height) specifies the width and height of the interface |
pygame.display.get_surface() | Get the current Surface object | |
pygame.display.flip() | Update the entire surface object to be displayed to the screen | |
pygame.display.update() | Update part of the content to display on the screen. If there is no parameter, it is to update the entire surface object | |
2.2 Common methods of pygame.event module
method | illustrate | Remark |
---|---|---|
pygame.event.get() | get event queue | QUIT: Close window event detected KEYDOWN: keyboard down event MOUSEBUTTONDOWN: mouse down event |
2.3 Common methods of Surface objects
method | illustrate | Remark |
---|---|---|
pygame.Surface.blit() | draw an image onto another image | |
pygame.Surface.convert() | Convert the pixel format of an image | |
pygame.Surface.convert_alpha() | Convert the pixel format of the image, including the alpha channel | |
pygame.Surface.fill() | Color Fill Surface | |
pygame.Surface.get_rect() | Get the rectangular area of Surface and return a Rect object | |
2. Problem summary
1. Form problem
1.1 After adding an infinite loop, the form does not respond.
Problem: In order to keep the form displayed, an infinite loop is added to the code segment to keep the code displayed, but the form is stuck after adding the infinite loop.
while True:
clock.tick(60)
pygame.display.flip()
Result: Even with a clock added, it still freezes
There is no specific solution after searching on the Internet. After adding event scanning later, it will not be stuck. The specific reason is not clear.
This is different from C# and the like, you need to add events to avoid error reporting.
while True:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.display.flip()