Project code | Plane
# - * - Coding: UTF-8 - * - Import pygame, Time from Plane Import Plane from HeroPlane Import HeroPlane from Screen Import Screen from pygame.locals Import * DEF key_control (plane_temp): # get events, such as buttons, etc. for Event in pygame.event.get (): # determine whether the exit button is clicked iF event.type == the qUIT: Print ( " exit " ) exit () # Determining whether a key is pressed elif Event.type == KEYDOWN: # detecting whether a key or a left IF event.key == K_a or event.key == K_LEFT: Print ( ' left ' ) plane_temp.move_left () # detecting whether a key is d or right elif event.key == K_d or event.key == K_RIGHT: Print ( ' right ' ) plane_temp.move_right () # detect whether the key is the space key elif event.key == K_SPACE: Print ( ' Space ' ) plane_temp.fire () DEF main (): Screen = with pygame.display.set_mode ((480, 852), 0, 32 ) # Create window object screen_temp = Screen (Screen) # Create a plane target plane = Plane (Screen) # create enemy objects enemyPlane = HeroPlane (Screen) the while True: screen_temp.display () # display window plane.display (plane.bullet_list) # display aircraft enemyPlane.display (enemyPlane.bullet_list) # enemy aircraft display enemyPlane.move () # enemy movement enemyPlane.fire () # enemy fire key_control (Plane) # keyboard event listener pygame.display.update () # Update window the time.sleep (0.01) # delay of 0.01 seconds, to prevent the program memory overflow IF the __name__ == ' __main__ ' : main ()
Base.py
Base class
# - * - Coding: UTF-8 - * - Import pygame class Base (Object): # Background Image Image = None DEF __init__ (Self, screen_temp, the X-, the y-, image_path): self.x = the X- self.y = the y- self.screen = screen_temp self.image_load (image_path) # aircraft assigned picture object DEF image_load (Self, image_path): self.image = pygame.image.load (image_path)
BasePlane.py
Aircraft base class
# - * - Coding: UTF-. 8 - * - from Base Import Base class BasePlane (Base): DEF the __init__ (Self, screen_temp, X, Y, image_path): Base. The __init__ (Self, screen_temp, X, Y, image_path) # indicates the plane DEF Run the display (Self, bullet_list): self.screen.blit (self.image, (self.x, self.y)) # display aircraft for bullet in bullet_list: # cycle removed the bullet objects # determine whether the bullet out of bounds if bullet.judge (): bullet_list.remove (bullet) # If you delete the cross-border bullet bullet bullet.display () # Display bullet bullet.move () # bullet moving step
Plane.py
Aircraft Objects
# - * - Coding: UTF-. 8 - * - from Bullet Import Bullet from BasePlane Import BasePlane class Plane (BasePlane): # Storage bullet objects bullet_list = [] DEF the __init__ (Self, screen_temp): BasePlane. The __init__ (Self, screen_temp, 210, 700, " ./resource/hero1.png " ) # aircraft moves leftward offset DEF move_left (Self): self.x - 10 = # aircraft moved rightward offset DEF move_right (Self): Self. X + = 10 # The bullet object is created to store the aircraft into bullet_list list DEF Fire (Self): self.bullet_list.append (Bullet (self.screen, self.x, self.y)) Print (self.bullet_list)
HeroPlane.py
Enemy objects
# - * - Coding: UTF-. 8 - * - Import Random from BasePlane Import BasePlane from EnemyBullet Import EnemyBullet class HeroPlane (BasePlane): # define a storage class attribute for direction = ' right ' # storage bullet objects bullet_list = [] DEF __init__ (Self, screen_temp): . BasePlane __init__ (Self, screen_temp, 0, 0, " ./resource/enemy-1.gif " ) # enemy movement trajectory DEF the move (Self): #Create default enemy bullets rightward IF self.direction == ' right ' : self.x + =. 5 # each time moved to the right to increase the step size 5px elif self.direction == ' left ' : # leftward self.x -. 5 = # per 5px moved to the left to reduce the step size # when the aircraft and moves to the right moves to the left border iF self.x> 480 - 50: # 480 is the total width of the interface; 50 is the width of the aircraft Therefore the distance traveled by the enemy should width of the interface - the width of the enemy (moving distance = width of the interface - the aircraft and width) self.direction = ' left ' elif self.x <= 0: # when the enemy has moved to the left on the We will continue to move to the right = self.direction ' right ' # fire DEF Fire (Self): random_temp = the random.randint (. 1, 100) # randomly generate 1-- random number 100 IF (== 20 is random_temp) or (random_temp == 78): # random probability self.bullet_list.append (EnemyBullet (self.screen, self.x, self.y)) # create enemy bullets objects
BaseBullet.py
Bullet base class
# -*- coding:utf-8 -*- from Base import Base class BaseBullet(Base): def __init__(self, screen_temp, x, y, image_path): Base.__init__(self, screen_temp, x, y, image_path) # 子弹背景 def display(self): self.screen.blit(self.image, (self.x, self.y))
Bullet.py
Bullet Object
# - * - Coding: UTF-. 8 - * - from BaseBullet Import BaseBullet class Bullet (BaseBullet): DEF the __init__ (Self, screen_temp, X, Y): . BaseBullet the __init__ (Self, screen_temp, X + 40, Y - 20 is, " ./resource/bullet.png " ) # bullet step DEF Move (Self): self.y - = 10 # determines whether y-axis of the bullet bounds DEF Judge (Self): iF self.y < 0: return True the else : return False
EnemyBullet.py
Object enemy bullets
# - * - Coding: UTF-. 8 - * - from BaseBullet Import BaseBullet class EnemyBullet (BaseBullet): DEF the __init__ (Self, screen_temp, X, Y): BaseBullet. The __init__ (Self, screen_temp, X + 30, Y + 30, " ./resource/bullet-1.gif " ) # bullet moving step DEF move (Self): self.y + = 20 is # determines whether y-axis has a bullet bounds DEF Judge (Self): iF self.y> 890: # 890 interface total height of the return True the else : return False
Screen.py
Window object
# -*- coding:utf-8 -*- from Base import Base class Screen(Base): def __init__(self, screen_temp): Base.__init__(self, screen_temp, 0, 0, "./resource/background.png") def display(self): self.screen.blit(self.image, (self.x, self.y))