Finding the position of center using pygame - python

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.

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)

How do I only center text on x-axis, but be able to move the y-axis in pygame?

I am trying to put text in the center of the screen, but also on top to label the text in the center. In order to center the text currently, I am using the code below...
info = pygame.display.Info()
screen = pygame.display.set_mode((info.current_w, info.current_h), pygame.FULLSCREEN)
screen_rect = screen.get_rect()
txt = font.render("some random message", True, color)
screen.blit(txt, txt.get_rect(center=screen_rect.center))
But I cannot find a way to modify the center using get_rect() to edit just the y-axis. I also could not find any more elaboration about the get_rect() function in the documentation.
The pygame.Rect object has the following attributes:
x, y
top, left, bottom, right
topleft, bottomleft, topright, bottomright
midtop, midleft, midbottom, midright
center, centerx, centery
size, width, height
w,h
So you could set the centerx to the screen's centerx to make the rectangle centered without affecting the y value.
screen.blit(text, text.get_rect(centerx=screen_rect.centerx))
For completion, if you want it centered at the top, you could write
screen.blit(text, text.get_rect(midtop=screen_rect.midtop)
and centered at the bottom
screen.blit(text, text.get_rect(midbottom=screen_rect.midbottom)

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

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

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.

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