def item_effect(event,ai_settings,screen,stats,aliens,ship_bullets,items): if event.key == pygame.K_1 and stats.item_1 - len(ai_settings.timekeep[1]) > 0: ai_settings.ship_bullet_width *= 8 # 飞船子弹宽度乘以 8 ai_settings.timekeep[1].append((time())) elif event.key == pygame.K_2 and stats.item_2 - len(ai_settings.timekeep[2]) > 0: ai_settings.floating_drop_speed /= 2 # 外星人下降速度除以 2 ai_settings.timekeep[2].append((time())) elif event.key == pygame.K_3 and stats.item_3 - len(ai_settings.timekeep[3]) > 0: ai_settings.energy_bullet = False # 普通子弹升级为高能子弹 ai_settings.timekeep[3].append((time())) elif event.key == pygame.K_4 and stats.item_4 - len(ai_settings.timekeep[4]) > 0: stats.killed_number += len(aliens.sprites()) stats.score += len(aliens.sprites()) * ai_settings.alien_points stats.item_4 -= 1 aliens.empty() ship_bullets.empty() # 清除屏幕上所有外星人 #refactor 两个if单独定义函数 if stats.killed_number // ai_settings.award_base > Item.count: item = Item(ai_settings,screen) item.caculate_number() items.add(item) if stats.killed_number // ai_settings.level_base > stats.level: if stats.level < 20: ai_settings.increase_speed() stats.level += 1 elif event.key == pygame.K_5 and stats.item_5 - len(ai_settings.timekeep[5]) > 0: ai_settings.effect_time *= 2 ai_settings.timekeep[5].append((time())) elif event.key == pygame.K_6 and stats.item_6 - len(ai_settings.timekeep[6]) > 0: ai_settings.timekeep[6].append(time())
def check_bullet_alien_collisions(ai_settings,screen,stats,ship,aliens,ship_bullets,items): '''响应子弹与外星人的碰撞''' # 检查是否有子弹击中了外星人,如果是,就删除相应的子弹和外星人 collisions = pygame.sprite.groupcollide(ship_bullets,aliens,ai_settings.energy_bullet,True) # 计分 if collisions: for aliens_hit in collisions.values(): stats.killed_number += len(aliens_hit) stats.bullet_killed_number += len(aliens_hit) stats.score += ai_settings.alien_points * len(aliens_hit) #refactor 两个if单独定义函数 if stats.killed_number // ai_settings.award_base > Item.count: item = Item(ai_settings,screen) item.caculate_number() items.add(item) if stats.killed_number // ai_settings.level_base > stats.level: if stats.level < 20: ai_settings.increase_speed() stats.level += 1