从 Pygame 中的组中识别单个精灵
Identify individual sprite from group in Pygame
原Post:
使用 pygame 是否可以从组中识别随机精灵?
我正在努力学习 Python 并且一直在努力改进 Alien Invasion 程序。对于外星人本身,外星人与外星人 class 并由此创建了一个组,其中有 4 行,每行 8 个外星人。
我想让一个随机的外星人定期飞到屏幕底部。是否可以与团队一起执行此操作,或者如果我想拥有此功能,我是否必须想出一些其他方法来创建我的车队?
我遇到过一些案例,其他人似乎一直在尝试类似的东西,但没有任何信息说明他们是否成功。
更新:
我已经对此进行了更深入的研究。我尝试在 game_functions.py
中创建一个 alien_attack
函数。内容如下:
def alien_attack(aliens):
for alien in aliens:
alien.y += alien.ai_settings.alien_speed_factor
alien.rect.y = alien.y
我在 alien_invasion.py
的 while 循环中用 gf.alien_attack(aliens)
调用了它。不幸的是,这导致 3 行消失,1 行以我想要的方式进行攻击,只是整行都这样做而不是单个精灵。
我也尝试在 alien_attack.py
中将 aliens = Group()
更改为 aliens = GroupSingle()
。这导致游戏开始时屏幕上只有一个精灵。它以我想要的方式攻击,但我希望所有其他精灵也出现但不攻击。这是怎么做到的?
您可以通过调用 random.choice(sprite_group.sprites())
(sprites()
returns 组中的精灵列表)来选择一个随机精灵。将这个精灵分配给一个变量,然后用它做任何你想做的事情。
这是一个最小的例子,我只是在 selected 精灵上绘制了一个橙色矩形并调用它的 move_down
方法(按 R 到 select 另一个随机精灵)。
import random
import pygame as pg
class Entity(pg.sprite.Sprite):
def __init__(self, pos):
super().__init__()
self.image = pg.Surface((30, 30))
self.image.fill(pg.Color('dodgerblue1'))
self.rect = self.image.get_rect(center=pos)
def move_down(self):
self.rect.y += 2
def main():
pg.init()
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
all_sprites = pg.sprite.Group()
for _ in range(20):
pos = random.randrange(630), random.randrange(470)
all_sprites.add(Entity(pos))
# Select a random sprite from the all_sprites group.
selected_sprite = random.choice(all_sprites.sprites())
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
elif event.type == pg.KEYDOWN:
if event.key == pg.K_r:
selected_sprite = random.choice(all_sprites.sprites())
all_sprites.update()
# Use the selected sprite in the game loop.
selected_sprite.move_down()
screen.fill((30, 30, 30))
all_sprites.draw(screen)
# Draw a rect over the selected sprite.
pg.draw.rect(screen, (255, 128, 0), selected_sprite.rect, 2)
pg.display.flip()
clock.tick(30)
if __name__ == '__main__':
main()
pg.quit()
原Post: 使用 pygame 是否可以从组中识别随机精灵?
我正在努力学习 Python 并且一直在努力改进 Alien Invasion 程序。对于外星人本身,外星人与外星人 class 并由此创建了一个组,其中有 4 行,每行 8 个外星人。
我想让一个随机的外星人定期飞到屏幕底部。是否可以与团队一起执行此操作,或者如果我想拥有此功能,我是否必须想出一些其他方法来创建我的车队?
我遇到过一些案例,其他人似乎一直在尝试类似的东西,但没有任何信息说明他们是否成功。
更新:
我已经对此进行了更深入的研究。我尝试在 game_functions.py
中创建一个 alien_attack
函数。内容如下:
def alien_attack(aliens):
for alien in aliens:
alien.y += alien.ai_settings.alien_speed_factor
alien.rect.y = alien.y
我在 alien_invasion.py
的 while 循环中用 gf.alien_attack(aliens)
调用了它。不幸的是,这导致 3 行消失,1 行以我想要的方式进行攻击,只是整行都这样做而不是单个精灵。
我也尝试在 alien_attack.py
中将 aliens = Group()
更改为 aliens = GroupSingle()
。这导致游戏开始时屏幕上只有一个精灵。它以我想要的方式攻击,但我希望所有其他精灵也出现但不攻击。这是怎么做到的?
您可以通过调用 random.choice(sprite_group.sprites())
(sprites()
returns 组中的精灵列表)来选择一个随机精灵。将这个精灵分配给一个变量,然后用它做任何你想做的事情。
这是一个最小的例子,我只是在 selected 精灵上绘制了一个橙色矩形并调用它的 move_down
方法(按 R 到 select 另一个随机精灵)。
import random
import pygame as pg
class Entity(pg.sprite.Sprite):
def __init__(self, pos):
super().__init__()
self.image = pg.Surface((30, 30))
self.image.fill(pg.Color('dodgerblue1'))
self.rect = self.image.get_rect(center=pos)
def move_down(self):
self.rect.y += 2
def main():
pg.init()
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
all_sprites = pg.sprite.Group()
for _ in range(20):
pos = random.randrange(630), random.randrange(470)
all_sprites.add(Entity(pos))
# Select a random sprite from the all_sprites group.
selected_sprite = random.choice(all_sprites.sprites())
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
elif event.type == pg.KEYDOWN:
if event.key == pg.K_r:
selected_sprite = random.choice(all_sprites.sprites())
all_sprites.update()
# Use the selected sprite in the game loop.
selected_sprite.move_down()
screen.fill((30, 30, 30))
all_sprites.draw(screen)
# Draw a rect over the selected sprite.
pg.draw.rect(screen, (255, 128, 0), selected_sprite.rect, 2)
pg.display.flip()
clock.tick(30)
if __name__ == '__main__':
main()
pg.quit()