Pygame 精灵似乎会在 x 轴上拉伸/增长,而不是移动。

3
我想要做的是制作一个在屏幕的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轴移动。

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


2
你在每帧绘制之前清除显示屏了吗? - Michael Bianconi
1个回答

2

在每一帧中,您需要清除显示,例如使用.fill()

最初的回答
screen.fill(0)

如果您不清除显示,那么矩形将继续在先前的矩形上绘制(偏移5个单位):

最初的回答:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接