如何计算屏幕的中间pygame。表面?

2024-06-28 11:19:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在计算屏幕中间的图像pygame.表面对象。我试过做简单的screen.blit(my_image, (0, 0)) 但那不行。有没有一种方法可以让我计算出图片的位置,所以它在中间?我的每一张照片都有不同的尺寸等等

编辑:

我知道了!我做了一个函数:

screenw = 500
screenh = 500

# true_coords(pygame.Surface(...), (250, 250))
def true_coords(obj, coords):
    objw = obj.get_width()
    objh = obj.get_height()

    true_coords = coords[0] - (objw / 2), coords[1] - (objh /2)
    return(true_coords)

Tags: 对象图像imagetrueobjget屏幕my
1条回答
网友
1楼 · 发布于 2024-06-28 11:19:42

使用Rect类为您完成所有计算。你知道吗

给定名为Surfacesurf和名为screen的显示Surface,可以执行以下操作:

screen = pygame.display.set_mode(...)
screen_rect = screen.get_rect()

surf = pygame.Surface(...)
# set the center of the new Rect to the center of the screen
surf_rect = surf.get_rect(center=screen_rect.center)

screen.blit(surf, surf_rect)

^{}类有许多有用的虚拟属性,如centertopleftbottomright等,您可以使用这些属性轻松定位Surface。通常使用SurfaceRect一起包装在^{}中。你知道吗

相关问题 更多 >