I'm rendering some text on a screen and I want to draw a box behind acting as background. Text is a surface if I know correctly, so you call get_rect() to get it's coordinates, width and height. So when I print(my_text_surface.get_rect()) I get this:
rect(0, 0, 382, 66)>
By this information I assume I can write:
my_suface.get_rect(1)
and get it's x coordinate. But then it's says:
get_rect only accepts keyword arguments
So I'm asking you here if get_rect() can get me a list and, if yes how can I access it?
If you need my code:
font = self.pygame.font.Font("freesansbold.ttf", 64)
spadenpala_text = font.render("Spadenpala", True, (255, 255, 255))
spadenpala_text_position = spadenpala_text.get_rect(center=(self.width/2, 200))
print(spadenpala_text.get_rect([1]))
Thanks, Mirko Dolenc
pygame.Surface.get_rect()
doesn't return a list, but a pygame.Rect
object:
Returns a new rectangle covering the entire surface.
pygame.Surface.get_rect()
returns a rectangle with the size of the Surface object, that always starts at (0, 0) since a Surface object has no position. A Surface is blit
at a position on the screen. The position of the rectangle can be specified by a keyword argument. For example, the center of the rectangle can be specified with the keyword argument center
. These keyword argument are applied to the attributes of the pygame.Rect
before it is returned (see pygame.Rect
for a full list of the keyword arguments).
A pygame.Rect
object has a lot of virtual attributes like .x
, .y
, .width
, .height
etc. e.g.:
surf = pygame.Surface((100, 50))
rect = surf.get_rect(center = (200, 200))
print(rect)
print(rect.x)
print(rect.y)
print(rect.width)
print(rect.height)
output:
<rect(150, 175, 100, 50)>
150
175
100
50