<p>如果要从列表中选择随机元素,建议使用<code>random.choice()</code>(<a href="https://docs.python.org/3/library/random.html" rel="nofollow noreferrer">^{<cd2>}</a>):</p>
<pre class="lang-py prettyprint-override"><code>import random
</code></pre>
<pre class="lang-py prettyprint-override"><code>alien_list = [ALIEN1, ALIEN2, ALIEN3]
random_alien = random.choice(alien_list)
</code></pre>
<hr/>
<p><code>Enemy</code>构造函数的参数是外星飞船的图像:</p>
<pre class="lang-py prettyprint-override"><code>class Enemy(Ship):
ALIEN_CHOICE = [ALIEN1, ALIEN2, ALIEN3]
def __init__(self, x, y, ship_img, health=100):
super().__init__(x,y, health)
self.ship_img = ship_img
# mask #
self.mask = pygame.mask.from_surface(self.ship_img)
</code></pre>
<pre class="lang-py prettyprint-override"><code>enemy = Enemy(random.randrange(50, WIDTH-100), random.randrange(-1500, -100), random.choice(Enemy.ALIEN_CHOICE))
</code></pre>
<hr/>
<p>或者,您可以完全跳过该论证(由您决定):</p>
<pre class="lang-py prettyprint-override"><code>class Enemy(Ship):
ALIEN_CHOICE = [ALIEN1, ALIEN2, ALIEN3]
def __init__(self, x, y, health=100):
super().__init__(x,y, health)
self.ship_img = random.choice(Enemy.ALIEN_CHOICE)
# mask #
self.mask = pygame.mask.from_surface(self.ship_img)
</code></pre>
<pre class="lang-py prettyprint-override"><code>enemy = Enemy(random.randrange(50, WIDTH-100), random.randrange(-1500, -100))
</code></pre>