I want the image of the character to be a rect doesn't make a lot of sense, but you're probably looking for the get_rect
method of the Surface
class:
get_rect()
get the rectangular area of the Surface
get_rect(**kwargs) -> Rect
Returns a new rectangle covering the entire surface. This rectangle will always start at 0, 0 with a width. and height the same size as the image...
(I guess with regular image you mean a Surface
, since a Surface
is how pygame represents images)
Note that the Rect
returned by this function always starts at 0
, 0
. If you already track the position of your object somehow (say a x
and y
variable), you could either
drop those variables and only use a
Rect
to keep track of the position of your object. If you move your object, instead of altering thex
andy
variable (e.g.x += mx
/y += my
), you would just update theRect
(e.g.pos.move_ip(mx, my)
wherepos
is theRect
).create a new
Rect
whenever you need aRect
, and make sure it points to the right location, using named arguments (e.g.your_surface.get_rect(x=x, y=y)
)use a
Sprite
, which is basically a combination of aSurface
and aRect
.