Modules provided by pygame:
pygame.display access display device
pygame.event manages events
pygame.draw draws shapes, lines and points
pygame.surface manages images and screens
pygame.rect coordinate processing
pygame.font text processing
pygame.music audio processing
pygame.sprite animation sprite
pygame.image image processing
pygame.sprite time processing
Import the pygame library:
import pygame
from pygame.locals import * # pygame.locals is a module that manages constants in pygame, importing many commonly used constants and functions for us to use.
Initialize the pygame library:
pygame.init() #Get and detect the hardware devices of our computer. When some audio or video devices cannot be detected, some modules in the pygame library may not be available. Therefore, pygame must be initialized before use.
Create window:
import pygame,sys #import pygame library and system library
from pygame.locals import * #Import constants
pygame.init() #initialize pygame
screen=pygame.display.set_mode([640,480]) #Create a window and set the window size using the pygame.display module.
while True: #Let the window not flash back
for event in pygame.event.get(): #traverse the list of all events, use pygame.event module.
if event.type==QUIT: #If the event type is exit
sys.exit() #Exit window