1、我方飞机模块import pygame class MyPlane(pygame.sprite.Sprite): def __init__(self,bg_size): pygame.sprite.Sprite.__init__(self) self.image1 = pygame.image.load(r'E:\learn pygame\image\me1.png').convert_alpha() self.image2 = pygame.image.load(r'E:\learn pygame\image\me2.png').convert_alpha() self.image3 = pygame.image.load(r'E:\learn pygame\image\me_protect.png').convert_alpha() self.destroy_images = [] self.destroy_images.extend([\ pygame.image.load(r'E:\learn pygame\image\me_destroy_1.png').convert_alpha(), \ pygame.image.load(r'E:\learn pygame\image\me_destroy_2.png').convert_alpha(), \ pygame.image.load(r'E:\learn pygame\image\me_destroy_3.png').convert_alpha(), \ pygame.image.load(r'E:\learn pygame\image\me_destroy_4.png').convert_alpha() \ ]) self.rect = self.image1.get_rect() self.width,self.height = bg_size[0],bg_size[1] self.rect.left,self.rect.top = (self.width - self.rect.width) // 2,self.height - self.rect.height - 60 self.speed = 10 self.active = True self.invincible = False self.mask = pygame.mask.from_surface(self.image1) def moveUp(self): if self.rect.top > 0: self.rect.top -= self.speed else: self.rect.top = 0 def moveDown(self): if self.rect.bottom < self.height - 30: self.rect.top += self.speed else: self.rect.bottom = self.height - 30 def moveLeft(self): if self.rect.left > 0: self.rect.left -= self.speed else: self.rect.left = 0 def moveRight(self): if self.rect.right < self.width: self.rect.left += self.speed else: self.rect.right = self.width def reset(self): self.rect.left,self.rect.top = (self.width - self.rect.width) // 2,self.height - self.rect.height - 30 self.active = True self.invincible = True 2、敌机与背景动图模块import pygamefrom random import * class SmallEnemy(pygame.sprite.Sprite): energy = 1 def __init__(self,bg_size): pygame.sprite.Sprite.__init__(self) self.image = pygame.image.load(r'E:\learn pygame\image\enemy1.png').convert_alpha() self.destroy_images = [] self.destroy_images.extend([\ pygame.image.load(r'E:\learn pygame\image\enemy1_down1.png').convert_alpha(), \ pygame.image.load(r'E:\learn pygame\image\enemy1_down2.png').convert_alpha(), \ pygame.image.load(r'E:\learn pygame\image\enemy1_down3.png').convert_alpha(), \ pygame.image.load(r'E:\learn pygame\image\enemy1_down4.png').convert_alpha() \ ]) self.rect = self.image.get_rect() self.width,self.height = bg_size[0],bg_size[1] self.speed = 2 self.active = True self.rect.left,self.rect.top = \ randint(0,self.width - self.rect.width), \ randint(-5 * self.height, 0) self.mask = pygame.mask.from_surface(self.image) self.energy = SmallEnemy.energy self.hit = False def move(self): if self.rect.top < self.height: self.rect.top += self.speed else: self.reset() def reset(self): self.active = True self.energy = SmallEnemy.energy self.rect.left,self.rect.top = \ randint(0,self.width - self.rect.width), \ randint(-5 * self.height, 0) class MidEnemy(pygame.sprite.Sprite): energy = 4