Python 游戏编程之实现飞机大战(含源代码)( 四 )

enemy3_fly_sound.set_volume(0.2)enemy1_down_sound = pygame.mixer.Sound("sound/enemy1_down.wav")enemy1_down_sound.set_volume(0.2)enemy2_down_sound = pygame.mixer.Sound("sound/enemy2_down.wav")enemy2_down_sound.set_volume(0.2)enemy3_down_sound = pygame.mixer.Sound("sound/enemy3_down.wav")enemy3_down_sound.set_volume(0.5)me_down_sound = pygame.mixer.Sound("sound/me_down.wav")me_down_sound.set_volume(0.2)def add_small_enemies(group1, group2, num): for i in range(num): e1 = enemy.SmallEnemy(bg_size) group1.add(e1) group2.add(e1)def add_mid_enemies(group1, group2, num): for i in range(num): e2 = enemy.MidEnemy(bg_size) group1.add(e2) group2.add(e2)def add_big_enemies(group1, group2, num): for i in range(num): e3 = enemy.BigEnemy(bg_size) group1.add(e3) group2.add(e3)def inc_speed(target, inc): for each in target: each.speed += incdef main: pygame.mixer.music.play(-1) # 生成我方飞机 me = myplane.MyPlane(bg_size) enemies = pygame.sprite.Group # 生成敌方小型飞机 small_enemies = pygame.sprite.Group add_small_enemies(small_enemies, enemies, 15) # 生成敌方中型飞机 mid_enemies = pygame.sprite.Group add_mid_enemies(mid_enemies, enemies, 4) # 生成敌方大型飞机 big_enemies = pygame.sprite.Group add_big_enemies(big_enemies, enemies, 2) # 生成普通子弹 bullet1 = bullet1_index = 0 BULLET1_NUM = 4 for i in range(BULLET1_NUM): bullet1.append(bullet.Bullet1(me.rect.midtop)) # 生成超级子弹 bullet2 = bullet2_index = 0 BULLET2_NUM = 8 for i in range(BULLET2_NUM // 2): bullet2.append(bullet.Bullet2((me.rect.centerx - 33, me.rect.centery))) bullet2.append(bullet.Bullet2((me.rect.centerx + 30, me.rect.centery))) clock = pygame.time.Clock # 中弹图片索引 e1_destroy_index = 0 e2_destroy_index = 0 e3_destroy_index = 0 me_destroy_index = 0 # 统计得分 score = 0 score_font = pygame.font.Font("font/font.ttf", 36) # 标志是否暂停游戏 paused = False pause_nor_image = pygame.image.load("images/pause_nor.png").convert_alpha pause_pressed_image = pygame.image.load("images/pause_pressed.png").convert_alpha resume_nor_image = pygame.image.load("images/resume_nor.png").convert_alpha resume_pressed_image = pygame.image.load("images/resume_pressed.png").convert_alpha paused_rect = pause_nor_image.get_rect paused_rect.left, paused_rect.top = width - paused_rect.width - 10, 10 paused_image = pause_nor_image # 设置难度级别 level = 1 # 全屏炸弹 bomb_image = pygame.image.load("images/bomb.png").convert_alpha bomb_rect = bomb_image.get_rect bomb_font = pygame.font.Font("font/font.ttf", 48) bomb_num = 3 # 每30秒发放一个补给包 bullet_supply = supply.Bullet_Supply(bg_size) bomb_supply = supply.Bomb_Supply(bg_size) SUPPLY_TIME = USEREVENT pygame.time.set_timer(SUPPLY_TIME, 30 * 1000) # 超级子弹定时器 DOUBLE_BULLET_TIME = USEREVENT + 1 # 标志是否使用超级子弹 is_double_bullet = False # 解除我方无敌状态定时器 INVINCIBLE_TIME = USEREVENT + 2 # 生命数量 life_image = pygame.image.load("images/life.png").convert_alpha life_rect = life_image.get_rect life_num = 3 # 用于阻止重复打开记录文件 recorded = False # 游戏结束画面 gameover_font = pygame.font.Font("font/font.TTF", 48) again_image = pygame.image.load("images/again.png").convert_alpha again_rect = again_image.get_rect gameover_image = pygame.image.load("images/gameover.png").convert_alpha gameover_rect = gameover_image.get_rect # 用于切换图片 switch_image = True # 用于延迟 delay = 100 running = True while running: for event in pygame.event.get: if event.type == QUIT: pygame.quit sys.exit elif event.type == MOUSEBUTTONDOWN: if event.button == 1 and paused_rect.collidepoint(event.pos):