【问题标题】:How to Display Sprites in Pygame?如何在 Pygame 中显示精灵?
【发布时间】:2019-12-15 21:56:39
【问题描述】:

这只是一个关于 PyGame 中精灵的快速问题,我已经按照下面的代码加载了我的图像,我只是想知道如何在 PyGame 中显示精灵,比如绘制一个矩形或圆形。无论如何,我不想让它表现出来。我想我使用了blit 命令,但我不确定,我在网上找不到太多内容。

这是我用于加载它的图像代码。

Star = pygame.image.load('WhiteStar.png').convert_alpha()

您可以只提供加载精灵的轮廓。我只是想展示它。

【问题讨论】:

  • 您需要将图像blit 到您的屏幕表面。 pygame.Surface.blit(Star, screen) 之类的东西 screen 是你的 pygame 显示变量。

标签: python pygame sprite pygame-surface


【解决方案1】:

使用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.Rectpygame.Surface.get_rect.get_rect() 返回一个具有 Surface 对象大小的矩形,该矩形始终从 (0, 0) 开始,因为 Surface 对象没有位置。矩形的位置可以由关键字参数指定。例如,可以使用关键字参数center 指定矩形的中心。这些关键字参数在返回之前应用于pygame.Rect 的属性(有关关键字参数的完整列表,请参见pygame.Rect):

screen.blit(star, star.get_rect(center = (x, y)))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多