-1

I'm trying to implement collisions into my game but I do not know how to get the rect attributes for my images. The player gets to choose where the images spawn(he places them) so I cannot set an exact number etc.

1 Answers1

3

The pygame.Surface object has a method get_rect which returns an instance of a pygame.Rect that encloses the Surface. Note that the position (x and y attributes of the Rect) starts at (0,0), but you can change this value and reference it whenever you blit to another surface.

So if you have a pygame.Surface named surf, and a screen surface called screen, you could do something like this:

surf_rect = surf.get_rect()
# move the surface 20 units to the right, and 15 units down
surf_rect.x += 20
surf_rect.y += 15
# now draw this surface to the screen based on the surf_rect's position
screen.blit(surf, (surf_rect.x, surf_rect.y))

http://www.pygame.org/docs/ref/surface.html#pygame.Surface.get_rect

Additionally, pygame.Surface also has get_width and get_height methods that are pretty self-explanatory.

Haz
  • 2,539
  • 1
  • 18
  • 20