LBG 发表于 2018-8-8 17:07:07

pygame打飞机音效为什么还是一直重复?

我按照教程改了代码,为什么还是敌机一被消灭,消灭的音效还是循环播放啊?

      # 绘制大飞机
      for each in big_enemies:
            if each.active:
                each.move()
                if switch_image:
                  screen.blit(each.image1, each.rect)
                else:
                  screen.blit(each.image2, each.rect)
                # 当大飞机进入画面时播放音效
                if each.rect.bottom == -50:
                  enemy3_fly_sound.play(-1)
            else: # 毁灭
                if not(delay % 5):
                  if e3_destroy_index == 0:
                        enemy3_down_sound.play()
                  screen.blit(each.destroy_images,each.rect)
                  e3_destroy_index = (e3_destroy_index + 1) % 6
                  if e3_destroy_index == 0:
                        enemy3_fly_sound.stop()
                        each.reset()


      # 绘制中飞机
      for each in mid_enemies:
            if each.active:
                each.move()
                screen.blit(each.image, each.rect)
            else: # 毁灭
                if not(delay % 5):
                  if e2_destroy_index == 0:
                        enemy2_down_sound.play()
                  screen.blit(each.destroy_images, each.rect)
                  e2_destroy_index = (e2_destroy_index + 1) % 4
                  if e2_destroy_index == 0:
                        each.reset()

      # 绘制小飞机
      for each in small_enemies:
            if each.active:
                each.move()
                screen.blit(each.image, each.rect)
            else:# 毁灭
                if not(delay % 5):
                  if e1_destroy_index == 0:
                        enemy1_down_sound.play()
                  screen.blit(each.destroy_images, each.rect)
                  e1_destroy_index = (e1_destroy_index + 1) % 4
                  if e1_destroy_index == 0:
                        each.reset()

塔利班 发表于 2018-8-8 18:21:33

忘了甲鱼老师原来是什么代码了
你的代码也不全,那问题就出在条件上,你的条件是否一直被满足
你的reset对active是否重置

LBG 发表于 2018-8-8 18:24:39

塔利班 发表于 2018-8-8 18:21
忘了甲鱼老师原来是什么代码了
你的代码也不全,那问题就出在条件上,你的条件是否一直被满足
你的reset ...

我跟着视频一步一步做的,可以说和视频的完全一样,但是就是会出这个bug

塔利班 发表于 2018-8-8 18:26:29

肯定有敲错的地方,甲鱼老师也是东改西改出来的,你肯定哪里没整对,不行就DOWN个源码

python小鲨鱼 发表于 2020-4-14 11:13:43

把你的enemy模块贴出来看下,错误的地方应该是在enemy中reset的属性中没有重置active的属性为True

    def reset(self):
      self.active = True
      self.energy = BigEnemy.energy
      self.rect.left, self.rect.top = \
                        randint(0,self.width - self.rect.width ), \
                        randint(-15 * self.height, -5 * self.height)
页: [1]
查看完整版本: pygame打飞机音效为什么还是一直重复?