【问题标题】:Pygame: How do I add an image to a rect?Pygame:如何将图像添加到矩形?
【发布时间】:2018-06-05 16:36:04
【问题描述】:

我正在 Pygame 上制作自己的游戏,我想将图像添加到矩形而不是彩色矩形。

我知道如何将图像添加到 pygame,但我想知道是否有某种方法可以添加图像并使用 Pygame.rect 属性,例如 rect.centerYrect.centerX

有人知道怎么做吗?

【问题讨论】:

  • 你应该使用 pygame 的 Sprite 类,因为它基本上是一个结合了图像和矩形的类

标签: python pygame


【解决方案1】:

加载你的图片,调用它的get_rect方法得到一个图片大小的矩形,然后设置矩形的坐标(有几个attributesx, y, topleft, center, etc.)...

IMAGE = pg.image.load('an_image.png').convert()  # or .convert_alpha()
# Create a rect with the size of the image.
rect = IMAGE.get_rect()
rect.center = (200, 300)

...并将矩形作为dest 参数传递给pygame.Surface.blit 以在rect.topleft 坐标处对图像进行blit。

screen.blit(IMAGE, rect)

【讨论】:

    猜你喜欢
    • 2021-03-07
    • 1970-01-01
    • 1970-01-01
    • 2015-01-23
    • 1970-01-01
    • 1970-01-01
    • 2023-01-01
    • 1970-01-01
    相关资源
    最近更新 更多