问题
I want to centre an image inside a rect note that I am using the image rect but asseperate rect to mount the image on.
rect_1 = pygame.Rect(1, 1, 178, 178)
...
cross = pygame.image.load("cross.png").convert_alpha()
cross_minified = pygame.transform.rotozoom(cross, 0, 0.25)
...
screen.blit(cross_minified, rect_1)
and after these codes I cannot find a way to center my x to the middle of the rect_1 it looks somewhat like this
回答1:
Use the functionality of pygame.Rect. Get a rectangle with the size of the image by pygame.Surface.get_rect and set its center by the keyword argument center
from the center of rect_1
:
cross_rect = cross_minified.get_rect(center = rect_1.center)
screen.blit(cross_minified, cross_rect)
来源:https://stackoverflow.com/questions/63834628/how-to-center-image-inside-a-rect-pygame