Skip to content Skip to sidebar Skip to footer

Getting The Center Of Surfaces In Pygame With Python 3.4

I'm having troubles getting the center of surfaces with pygame. It defaults to the upper left corner of surfaces when trying to place them on other surfaces. To demonstrate what I

Solution 1:

It will always blit the Surface at the topleft corner. A way around this is to calculate where you have to place the Surface in order for it to be centered around a position.

x, y = 50, 50
screen.blit(surface, (x - surface.get_width() // 2, y - surface.get_height() // 2))

This will position its center at the (x, y) coordinates.

Alternatively, you can create a Rect object with the center at x and y, and use that to position the surface.

x, y = 50, 50
rect = surface.get_rect(center=(x, y))
screen.blit(surface, rect)

Post a Comment for "Getting The Center Of Surfaces In Pygame With Python 3.4"