python - pygame Sprite 似乎拉伸(stretch)/增长而不是沿 x 轴移动

标签 python pygame sprite

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

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

最佳答案

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

screen.fill(0)

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

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

关于python - pygame Sprite 似乎拉伸(stretch)/增长而不是沿 x 轴移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57207769/

相关文章:

python - pyopengl 上的 gldrawpixels 没有绘制任何东西?

ios - cocos2d 动画不显示 Sprite 表

html - 为动态图像生成 CSS Sprite

python - 非相邻对之间的 pct_change

python - 使用 pygame 进行 Sprite 副本

python - 如何自动计算集群数量?

python - Pygame 碰撞检测错误, Sprite

swift - 更改 SpriteKit 中的主 Sprite

python - RTTM文件格式

python - 使用 get_text 时“NoneType”对象不可调用 beautifulsoup 错误