如何在rect pygame中居中放置图像

问题描述

我想将图像居中放置在rect内部,请注意,我正在使用图像rect,但是安装了适当的rect才能安装图像。

rect_1 = pygame.Rect(1,1,178,178)
...
cross = pygame.image.load("cross.png").convert_alpha()
cross_minified = pygame.transform.rotozoom(cross,0.25)
...
screen.blit(cross_minified,rect_1)

在这代码之后,我找不到将x居中到rect_1中间的方法 它看起来像这样

解决方法

使用pygame.Rect的功能。从center的中心开始,得到一个图像大小为pygame.Surface.get_rect的矩形,并通过关键字参数rect_1设置其中心:

cross_rect = cross_minified.get_rect(center = rect_1.center)
screen.blit(cross_minified,cross_rect)