# 以下是主函数: import pygame from plane_sprites import * class PlaneGame(object): """飞机大战主游戏""" def __init__(self): print("游戏初始化") # 1.创建游戏的窗口 self.screen = pygame.display.set_mode(SCREEN_RECT.size) # 2.创建游戏的时钟 self.clock = pygame.time.Clock() # 3.调用私有方法,精灵和精灵组的创建 self.__creat_sprites() # 4.设置定时器事件 - 创建敌机 1s pygame.time.set_timer(CREAT_ENEMY_EVENT, 1000) pygame.time.set_timer(HERO_FIRE_EVENT, 200) def __creat_sprites(self): # 创建背景精灵和精灵组 bg1 = Background() bg2 = Background(True) self.back_group = pygame.sprite.Group(bg1, bg2) # 创建敌机的精灵组 self.enemy_group = pygame.sprite.Group() # 创建英雄的精灵和精灵组 self.hero = Hero() self.hero_group = pygame.sprite.Group(self.hero) def start_game(self): print("游戏开始...") while True: # 1.设置刷新帧率 self.clock.tick(FRAME_PER_SEC) # 2.事件监听 self.__event_handler() # 3.碰撞检测 self.__check_collide() # 4.更新/绘制精灵组 self.__update_sprites() # 5.更新显示 pygame.display.update() def __event_handler(self): for event in pygame.event.get(): # 判断是否退出游戏 if event.type == pygame.QUIT: PlaneGame.__game_over() elif event.type == CREAT_ENEMY_EVENT: # print("敌机出场...") # 创建敌机精灵 enemy = Enemy() # 将敌机精灵添加到敌机精灵组
python飞机大战实战演练代码
最新推荐文章于 2024-06-07 15:17:39 发布