I have seen image.get_rect()
and pygame.Rect()
being used in different programs.How do they exactly differ from each other.Don't both of these codes are used to set a rectangular boundary around a surface/image or are they different?
Asked
Active
Viewed 1,664 times
0

dejanualex
- 3,872
- 6
- 22
- 37

Python_newbie.
- 47
- 1
- 6
1 Answers
2
rect = pygame.Rect(x, y, w, h)
constructs an instance object of the class pygame.Rect
.
pygame.Surface.get_rect
creates a pygame.Rect
object from a surface the size of the surface. e.g.:
rect = image.get_rect(center = (x, y))
pygame.Surface.get_rect.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. 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).

Rabbid76
- 202,892
- 27
- 131
- 174