所以我正在 python 中加载图像,并且需要将图像放在 Pygame 中居中。由于 Pygame 从屏幕的左上角开始绘图,因此 x 和 y 坐标 0,0 不起作用。谁能告诉我 pygame 窗口的中心 x 和 y 坐标?
最佳答案
可以通过窗口矩形( pygame.Surface.get_rect
)获取窗口的中心:
screen = pygame.display.set_mode((800, 600))
center = screen.get_rect().center
用它来blit
屏幕中央的图像
(pygame.Surface
对象):
screen.blit(image, image.get_rect(center = screen.get_rect().center))
pygame.Surface.get_rect()
返回一个具有 Surface 对象大小的矩形,该矩形始终从 (0, 0) 开始。但是,可以使用关键字参数指定矩形的位置。在这种情况下,矩形的中心由屏幕的中心设置。
当 blit
的第二个参数是一个矩形 ( pygame.Rect
),矩形的左上角将用作 blit
的位置。
关于python - 如何在pygame中将图像置于窗口中间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70530062/