1、飞机大战Python程序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.ti
2、me.set_timer(HERO_FIRE_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 _even
3、t_handler(self): # 关闭退出游戏 for event in pygame.event.get(): if event.type = pygame.QUIT: PlaneGame._Gameover() elif event.type = CREATE_ENEMY_EVENT: # 创建敌机d enemy = Enemy() self.enemy_group.add(enemy) elif event.type = HERO_FIRE_EVENT: self.hero.fire() # 按键 keys_pressed = pygame.key.get_pressed() if
4、keys_pressedpygame.K_RIGHT: self.hero.speed = 2 elif keys_pressedpygame.K_LEFT: self.hero.speed = -2 else: 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,
5、True) if len(enemys) 0: self.hero.kill() PlaneGame._Gameover()# 更新图象 更新屏幕 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.
6、update() self.hero.bullets.draw(self.screen) # 游戏结束 staticmethod 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_: #创建对象 ga
7、me = PlaneGame() # 启动游戏 while True: game.start_game()import randomimport pygame# 定义常量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):
8、super()._init_() self.image = pygame.image.load(image_name) self.rect = self.image.get_rect() self.speed = speed def 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
9、# 背景精灵 def update(self): super().update() if self.rect.y = SCREEN_RECT.height: self.rect.y = -self.rect.heightclass Enemy(GameSprite): # 随机精灵 # 调用父类,图片 def _init_(self): super()._init_(.imagesdiji1.png) # 随机速度 self.speed = random.randint(1, 3) #随机位置 self.rect.bottom = 0 max_x = SCREEN_RECT.width - s
10、elf.rect.width self.rect.x = random.randint(0, max_x) def update(self): # 调用父类 super().update()#判断是否删除 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_R
11、ECT.centerx self.rect.bottom = SCREEN_RECT.bottom - 120 self.bullets = pygame.sprite.Group() def update(self): self.rect.x += self.speed if self.rect.x SCREEN_RECT.right: self.rect.right = SCREEN_RECT.right def fire(self): for i in (0, 1, 2): bullet = Bullet() bullet.rect.bottom = self.rect.y - i * 20 bullet.rect.centerx = self.rect.centerx self.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欢迎您的下载,资料仅供参考!致力为企业和个人提供合同协议,策划案计划书,学习资料等等打造全网一站式需求
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1