你必须通过
all_sprites
中的参数
Worker.__init__
方法到
__init__
Sprite
类,以便将精灵添加到此组中。
pygame.sprite.Sprite.__init__(self, all_sprites, groups)
您还可以重新排列
方法并将这两个组作为最后一个参数传递(它们将被添加到
groups
列表)。
# In the Worker class.
def __init__(self, idw, image_running, image_idle, image_accident,
location, *groups):
# Pass the two groups as the `groups` argument.
Worker(idw, IMG_WORKER_RUNNING, IMG_WORKER_IDLE,
IMG_WORKER_ACCIDENT, (50, 50), self.all_sprites,
self.workers)
在while循环中,您需要在背景曲面上闪烁或在每一帧中填充屏幕以清除它,然后绘制精灵,最后用
pygame.display.flip()
(您需要在示例中缩进它)。
while carry_on:
for event in pygame.event.get():
if (event.type == pygame.QUIT) or (simulation_time == 0):
carry_on = False
# You probably don't want to decrement the
# simulation_time once per event. Dedent
# this line to decrement it once per frame.
simulation_time -= 1
agent_action = 0
send_alert = np.random.normal(0, 0.1, 1)
if send_alert > 0.2:
agent_action = 1
self.all_sprites.update(self.screen, agent_action)
# Blit a background surface or fill the screen.
self.screen.fill((0, 40, 0)) # Fill with dark green.
# Blit all sprites.
self.all_sprites.draw(self.screen)
# Update the screen.
pygame.display.flip()
clock.tick(20)