pygame 精灵似乎 stretch/grow 而不是沿 x 轴移动

pygame sprite appears to stretch/grow instead of move along x-axis

我想做的是制作一个沿屏幕 x 轴移动的绿色框。这是在线教程告诉我要做的:

class Player(pygame.sprite.Sprite):

     def __init__(self):
          pygame.sprite.Sprite.__init__(self)
          self.image = pygame.Surface((50, 50))
          self.image.fill((0, 255, 0))
          self.rect = self.image.get_rect()
          self.rect.center = (80, 80)

     def update(self):
          self.rect.x += 5

但我最终得到的是一个沿 x 轴增长的矩形,而不是沿 x 轴移动。

为什么这段代码没有按预期移动精灵?

你必须清除每一帧的显示,例如通过 .fill():

screen.fill(0)

如果不清除显示,则矩形会在前一个矩形的顶部连续绘制(位移 5):

+---+---+--------+
|1  |2  |3       |
|   |   |        |
|   |   |        |
+---+---+--------+