1、import pygamefrom plane_sprites import *class PlaneGame(object):# 初始化 开始游戏类方法 启动游戏def _init_(self):# 创建窗口 时钟 调用创建精灵方法self.screen = pygame.display.set_mode(SCREEN_RECT.size)self.clock = pygame.time.Clock()self._create_spites()pygame.time.set_timer(CREATE_ENEMY_EVENT, 1000)pygame.time.set_timer(HERO_F
2、IRE_EVENT, 500)# 添加精灵def _create_spites(self):# 背景精灵bg1 = BackGroup()bg2 = BackGroup(True)self.back_ground = pygame.sprite.Group(bg1, bg2)# 敌机精灵self.enemy_group = pygame.sprite.Group()# 英雄精灵self.hero = Hero()self.hero_group = pygame.sprite.Group(self.hero)# 监听def _event_handler(self):# 关闭 退出游戏for ev
3、ent in pygame.event.get():if event.type = pygame.QUIT:PlaneGame._Gameover()elif event.type = CREATE_ENEMY_EVENT:# 创建敌机 denemy = Enemy()self.enemy_group.add(enemy)elif event.type = HERO_FIRE_EVENT:self.hero.fire()# 按键keys_pressed = pygame.key.get_pressed()if keys_pressedpygame.K_RIGHT:self.hero.speed
4、 = 2elif keys_pressedpygame.K_LEFT:self.hero.speed = -2else:self.hero.speed = 0#碰撞检测def _check_collide(self):pygame.sprite.groupcollide(self.hero.bullets, self.enemy_group, True, True)enemys = pygame.sprite.spritecollide(self.hero, self.enemy_group, True)if len(enemys) 0:self.hero.kill()PlaneGame._G
5、ameover()# 更新图象 更新屏幕def _update_sprites(self):self.back_ground.update()self.back_ground.draw(self.screen)self.enemy_group.update()self.enemy_group.draw(self.screen)self.hero_group.update()self.hero_group.draw(self.screen)self.hero.bullets.update()self.hero.bullets.draw(self.screen)# 游戏结束staticmethod
6、def _Gameover():pygame.quit()exit()def start_game(self):# 时钟 监听 碰撞检测 更新图象 更新屏幕 结束self.clock.tick(FRAME_PER_SEC)self._event_handler()self._check_collide()self._update_sprites()pygame.display.update()if _name_ = “_main_“:#创建对象game = PlaneGame()# 启动游戏while True:game.start_game()import randomimport pyga
7、me# 定义常量SCREEN_RECT = pygame.Rect(0, 0, 480, 700)FRAME_PER_SEC = 60CREATE_ENEMY_EVENT = pygame.USEREVENTHERO_FIRE_EVENT = pygame.USEREVENT + 1class GameSprite(pygame.sprite.Sprite):def _init_(self, image_name, speed = 1):super()._init_()self.image = pygame.image.load(image_name)self.rect = self.imag
8、e.get_rect()self.speed = speeddef update(self):self.rect.y += self.speedclass BackGroup(GameSprite):def _init_(self, is_alt = False):super()._init_(“.imageshuibeijing.png“)if is_alt:self.rect.y = -self.rect.height# 背景精灵def update(self):super().update()if self.rect.y = SCREEN_RECT.height:self.rect.y
9、= -self.rect.heightclass Enemy(GameSprite):# 随机精灵# 调用父类,图片def _init_(self):super()._init_(“.imagesdiji1.png“)# 随机速度self.speed = random.randint(1, 3)#随机位置self.rect.bottom = 0max_x = SCREEN_RECT.width - self.rect.widthself.rect.x = random.randint(0, max_x)def update(self):# 调用父类super().update()#判断是否删除
10、if self.rect.y = SCREEN_RECT.height:self.kill()def _del_(self):# print(“敌机挂了 “)passclass Hero(GameSprite):# 英雄图片def _init_(self):super()._init_(“.imageseiji1.png“, 0)self.rect.centerx = SCREEN_RECT.centerxself.rect.bottom = SCREEN_RECT.bottom - 120self.bullets = pygame.sprite.Group()def update(self)
11、:self.rect.x += self.speedif self.rect.x SCREEN_RECT.right:self.rect.right = SCREEN_RECT.rightdef fire(self):for i in (0, 1, 2):bullet = Bullet()bullet.rect.bottom = self.rect.y - i * 20bullet.rect.centerx = self.rect.centerxself.bullets.add(bullet)class Bullet(GameSprite):def _init_(self):super()._init_(“.imageszidan1.png“, -2)def update(self):super().update()if self.rect.bottom 0:self.kill()def _del_(self):pass