The following is the main program
The import pygame
The from plane_sprites import *
The class PlaneGame (object) :
"' the war the main game" '
Def __init__ (self) :
Print (" game initialization ")
# to create the game window
The self. The screen=pygame. Display. Set_mode (SCRENT_RECT. Size)
# to create the game clock
The self. The clock=pygame. Time. Clock ()
# call a private method, elves and fairies group create
Self. __create_sprites ()
Def __create_sprites (self) :
# create background and elves group
Bg1=Background ("./images/Background. PNG ")
Self. Back_group=pygame. Sprite. Group (bg1)
Def statr_game (self) :
Print (" game ")
While True:
Frame # set the refresh rate
The self. The clock. Tick (FRAME_PER_SEC)
# time listening
Self. __event_handler ()
# collision detection
Self. __check_collide ()
# update drawing elves group
Self. __create_sprites ()
# update the display
Pygame. Display. The update ()
Pass
Def __event_handler (self) :
For the event in pygame. Event. The get () :
# determine whether quit the game
If the event. The type==pygame. QUIT:
PlaneGame. __game_over ()
Def __check_collide (self) :
Pass
Def __update_sprites (self) :
Self. Back_group. The update ()
Self. Back_group. The draw (self. Screen)
@ staticmethod
Def __game_over () :
Print (" game over ")
Pygame. Quit ()
The exit ()
If __name__=="__main__ ':
"' create the game object" '
Game=PlaneGame ()
# start game
Game. Statr_game ()
The following is to provide the elves
The import pygame
Constant # screen size
,0,480,700 SCRENT_RECT=pygame. The Rect (0)
Frame # refresh rate
FRAME_PER_SEC=60
The class GameSprite (pygame. Sprite. Sprite) :
"" "plane war game spirit "" "
Def __init__ (self, image_name, speed=1) :
# call initialization method of the parent
Super () __init__ ()
# define the object's properties
The self. The image=pygame. Image. The load (image_name)
# define the rect record the aircraft's initial position
The self. The rect=self. Image. Get_rect ()
The self. The speed=speed
Def update (self) :
# in the vertical direction of the screen
The self. The rect. + y=self. Speed
Class Background (GameSprite) :
"' the game background elves'
Def update (self) :
# 1 call the superclass method to achieve
Super (). The update ()
# to judge whether to remove the screen if remove screen images will be moved to the screen above the
If the self. The rect. Y & gt;=SCRENT_RECT. Height:
The self. The rect. Y=- self. The rect. Height
Run results no background image is loaded up with normal set the size of the box