这只是一个关于 PyGame 中 Sprite 的快速问题,我按照下面的代码加载了我的图像,我只是想知道如何在 PyGame 中显示 Sprite ,例如绘制矩形或圆形。无论如何,我不想让它表现出来。我想我使用了 blit
命令,但我不确定,而且我在网上也找不到太多。
这是我加载它的图片代码。
Star = pygame.image.load('WhiteStar.png').convert_alpha()
您可以只提供加载 Sprite 的轮廓。我只是想显示它。
最佳答案
使用blit
绘制图像。实际上 blit
画了一个 Surface
到另一个。因此,您需要将图像blit
到与显示器关联的 Surface 上。
您需要指定图像在目标上 blit
的位置。该位置可以由定义左上角位置的一对坐标指定。或者可以用矩形指定,只考虑矩形的左上角:
screen = pygame.dispaly.set_mode((width, height))
star = pygame.image.load('WhiteStar.png').convert_alpha()
# [...]
while run:
# [...]
screen.blit(star, (x, y))
# [...]
使用 pygame.Rect
当您想将曲面的中心放置在特定点时。 pygame.Surface.get_rect.get_rect()
返回一个具有 Surface 对象大小的矩形,该矩形始终从 (0, 0) 开始,因为 Surface 对象没有位置。矩形的位置可以由关键字参数指定。例如,矩形的中心可以用关键字参数 center
指定。这些关键字参数应用于 pygame.Rect
的属性在返回之前(请参阅 pygame.Rect
以获取关键字参数的完整列表):
screen.blit(star, star.get_rect(center = (x, y)))
关于python - 如何在 Pygame 中显示 Sprite ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59348409/