python - 如何限制pygame中的 Sprite 列表

标签 python pygame

我刚刚学习 pygame,遇到了一个问题。我的程序创建了一支外星人舰队,你必须消灭它们。我想在完全移除后立即生成整个舰队,但是当您摧毁另一艘船时,我的程序会立即生成一艘船。

我尝试使用 while len < 48 来限制列表,但没有成功。 这是立即产生外星人的程序(我也在主循环之前插入了它):

 def create_fleet(custom_settings, screen, spaceship, aliens):
   alien = Alien(custom_settings, screen)
   number_aliens_x = get_number_aliens_x(custom_settings, alien.rect.width)
   number_rows = get_number_rows(custom_settings, spaceship.rect.height, alien.rect.height) #!!!
   for row_number in range(number_rows):
       for alien_number in range(number_aliens_x):
           if len(aliens) < 48:
                create_alien(custom_settings, screen, aliens, alien_number, row_number)

New ships spawn in the left top verge

最佳答案

And I want to spawn the whole fleet just after complete removal [...]

因此,一旦外星人数量为 0 ( len(aliens) == 0`),您就必须开始创建外星人舰队,例如:

def create_fleet(custom_settings, screen, spaceship, aliens):

      if len(aliens) == 0:

          alien = Alien(custom_settings, screen)
          number_aliens_x = get_number_aliens_x(custom_settings,alien.rect.width)
          number_rows = get_number_rows(custom_settings,spaceship.rect.height,alien.rect.height)

          for row_number in range(number_rows):
              for alien_number in range(number_aliens_x):
                  if len(aliens) < 48:
                      create_alien(custom_settings, screen, aliens, alien_number, row_number)

关于python - 如何限制pygame中的 Sprite 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58545878/

相关文章:

python - 如何使用matplotlib在同一窗口中绘制多个散点图?

c++ - 如何从外语线程调用python函数(C++)

python - 如何将pygame转换为exe?

Python with/Pygame - 这两组的区别?

python - 使用 pygame/pyglet 从数组重建图像时颜色不正确

python - Ubuntu 上 PIL 的问题

python - 代码中有一个我无法识别的错误

Python/Scipy "invalid index to scalar variable"

python - 与比特币军械库守护进程的连接被拒绝

python - 列表元素与 pandas 列的关键字匹配