Pygame: What is the Rect() arguments exactly, and what do they represent? - python

Here is what I understand so far about Rect, it is a data type that contains all the properties of a given rectangular surface. And is used along the pygame.draw.rect() function as the third argument in order for it to work.
The code follows this syntax (from the official documentation):
Rect(left, top, width, height)
I understand about the third and fourth argument, which suppose to represent the dimensions of the rectangular surface. What I don't understand is the first 2 argument - what is it suppose to represent and what does it do? Why does it always start at (0,0) and what can we use it for?

The first two arguments get assigned to the x and y attributes of the rect, the coordinates of the topleft corner. So if you want to create a rect that is positioned at the coordinates (200, 300), you can write: rect = pygame.Rect(200, 300, 40, 50). The third and fourth argument are indeed the dimensions of the resulting rect instance.
pygame.Rects also have a lot of other attributes which you can use to position your rect.
x,y
top, left, bottom, right
topleft, bottomleft, topright, bottomright
midtop, midleft, midbottom, midright
center, centerx, centery
If you want to move a rect, you can assign the new position to the x, y, topleft or one of the other attributes.
rect.x = 5
rect.y = 10
# That's pretty much the same as writing:
rect.topleft = (5, 10)
# You can also increment the position.
rect.x += 5
# Or use the `rect.move_ip` method.
rect.move_ip(5, 10)
Rects are used to store the blit position of sprites or images and for collision detection, for example with the colliderect or collidepoint methods.

Related

is there any way to get position of the RECT in pygame?

I want to know is there any way I can have the position of the Rect I create and stored in variable name "rect" in pygame
A pygame.Rect object has a lot of virtual attributes:
The Rect object has several virtual attributes which can be used to move and align the Rect:
x,y
top, left, bottom, right
topleft, bottomleft, topright, bottomright
midtop, midleft, midbottom, midright
center, centerx, centery
size, width, height
w,h
The top left position of the pygame.Rect object can be get and set with the x and y attribute:
rect = pygame.Rect(10, 20, 5, 5)
print(rect.x, rect.y)

What's difference between .get_rect() and pygame.Rect()?

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?
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).

Finding the center of an image to define a hitbox

I'm trying to select the center of my image to make a hitbox some pixels around it.
I was able to inflate() the hitbox to make it the right size, but it always uses the top left corner of the image. This is fine when i'm moving left, but when i turn right it goes away from the character (in the image the character is dragging a sword, so it goes way off center).
I've been reading about Vector2, pos and offset, but i can't get it to work.
In conclusion i need to learn a way to find the center of my image in order to place it's hitbox a few pixels to each side. Either that or how to "shift" the corner the hitbox uses so it's always in the front of the char.
Use a pygame.Rect object. Get the rectangle from the pygame.Surface object (image) and set the top left corner position (x, y) of the rectangle. e.g.:
rect = image.get_rect()
rect.topleft = (x, y)
respectively
rect = image.get_rect(topleft = (x, y))
pygame.Rect provides a lot of virtual attributes, which can be used to retrieve the corners, borders, center and size of the rectangle. .center returns the center of the rectangle:
center_x, center_y = rect.center

Finding the position of center using pygame

I am trying to move an image from its center using pygame. I loaded the image using pygame but I have the top corner coordinates of the image. how do i get the coordinates of center.
If you're using a Rect (and you should be), it's super easy:
rect = image.get_rect()
center_pos = rect.center
The Rect class has lots of super-useful handles:
x,y
top, left, bottom, right
topleft, bottomleft, topright, bottomright
midtop, midleft, midbottom, midright
center, centerx, centery
size, width, height
w,h
All of them can be assigned to as well:
rect.center = (x, y)
You can also query the size of the image. Adjust the corner coordinates by half of the size in each direction.

Pygame - What happens if you blit a surface with a rect smaller than the surface?

In this blit call
screen = pygame.Surface(640, 480)
bgsurf = pygame.Surface(640, 480)
new_rect = pygame.Rect(0, 0, 80, 80)
screen.blit(bgsurf, new_rect, new_rect)
how pygame decides which portion of bgsurf it will copy to the screen in the new_rect area?
From the pygame docs:
blit(source, dest, area=None, special_flags = 0) -> Rect
Draws a source Surface onto this Surface. The draw can be positioned
with the dest argument. Dest can either be pair of coordinates
representing the upper left corner of the source. A Rect can also be
passed as the destination and the topleft corner of the rectangle will
be used as the position for the blit. The size of the destination
rectangle does not effect the blit.
An optional area rectangle can be passed as well. This represents a
smaller portion of the source Surface to draw.
So as you can see, pygame would blit the whole surface at (0,0).
If you want to blit a part of surface, you need to pass in the area Rect.
EDIT:
In your case, it will blit the subsurface given by new_rect onto screen where the top-left corner will be placed at (0,0).

Categories

Resources