I want to centre an image inside a rect note that I am using the image rect but asseperate rect to mount the image on.
rect_1 = pygame.Rect(1, 1, 178, 178)
...
cross = pygame.image.load("cross.png").convert_alpha()
cross_minified = pygame.transform.rotozoom(cross, 0, 0.25)
...
screen.blit(cross_minified, rect_1)
and after these codes I cannot find a way to center my x to the middle of the rect_1
it looks somewhat like this
Use the functionality of pygame.Rect. Get a rectangle with the size of the image by pygame.Surface.get_rect and set its center by the keyword argument center from the center of rect_1:
cross_rect = cross_minified.get_rect(center = rect_1.center)
screen.blit(cross_minified, cross_rect)
Related
I am using Pygame and have an image. I can clip a rectangle from it:
image = pygame.transform.scale(pygame.image.load('example.png'), (32, 32))
handle_surface = image.copy()
handle_surface.set_clip(pygame.Rect(0, 0, 32, 16))
clipped_image = surface.subsurface(handle_surface.get_clip())
I have tried to use subsurface by passing a Surface:
handle_surface = image.copy()
hole = pygame.Surface((32, 32))
pygame.draw.circle(hole, (255, 255, 255), (0, 0), 32)
handle_surface.set_clip(hole)
image = surface.subsurface(handle_surface.get_clip())
surf = image.copy()
But I get the error:
ValueError: invalid rectstyle object
This error is because despite its name, subsurface expects a Rect, not a Surface. Is there a way to clip another shape from this image and have collidepoint work correctly?
You cannot use pygame.Surface.subsurface because a Surface is always rectangular and cannot have a circular shape. pygame.Rect.collidepoint detects if a point is inside a rectangular area and therefore cannot help you either.
Collision detection between a circle and a point can be calculated using the distance between the pointer and the center of the circle. Calculate the square of the Euclidean distance (dx*dx + dy*dy) from the point to the center of the circle. Check that the square of the distance is less than the square of the radius. In the following code the coordinates of the point are (px, py) and the circle is defined by its center (cx, cy) and its radius (radius).
dx = px - cx
dy = py - cy
if dx*dx + dy*dy <= radius*radius:
print('hit')
An alternative solution could be PyGame collision with masks. Also pygame.sprite.collide_circle could help, but then you would have to create a pygame.sprite.Sprite object for the point with radius 1, which seems to overcomplicate the problem.
If you want to clip a circular area from a pygame.Surface, see:
how to make circular surface in PyGame
How do I focus light or how do I only draw certain circular parts of the window in pygame?
How do I display a large black rectangle with a moveable transparent circle in pygame?
Can I use an image on a moving object within Pygame as opposed to to a color?
Short instruction:
Create a rectangular partial area from the image (only if the circle does not fill the whole image). However, the image must have an alpha channel. If it does not have one, this can be achieved with convert_alpha.
Create a transparent (pygame.SRCALPHA) mask with the size of the image
Draw a white opaque circle on the mask (I use pygame.draw.ellipse here, because it is easier to set the dimension)
Blend the circular mask with the image using the blend mode pygame.BLEND_RGBA_MIN. (see pygame.Surface.blit)
sub_image = image.subsurface(pygame.Rect(x, y, w, h)).convert_alpha()
mask_image = pygame.Surface(sub_image.get_size(), pygame.SRCALPHA)
pygame.draw.ellipse(mask_image, (255, 255, 255, 255), sub_image.get_rect())
sub_image.blit(mask_image, (0, 0), special_flags=pygame.BLEND_RGBA_MIN)
I want to centre an image inside a rect note that I am using the image rect but asseperate rect to mount the image on.
rect_1 = pygame.Rect(1, 1, 178, 178)
...
cross = pygame.image.load("cross.png").convert_alpha()
cross_minified = pygame.transform.rotozoom(cross, 0, 0.25)
...
screen.blit(cross_minified, rect_1)
and after these codes I cannot find a way to center my x to the middle of the rect_1
it looks somewhat like this
Use the functionality of pygame.Rect. Get a rectangle with the size of the image by pygame.Surface.get_rect and set its center by the keyword argument center from the center of rect_1:
cross_rect = cross_minified.get_rect(center = rect_1.center)
screen.blit(cross_minified, cross_rect)
I want to centre an image inside a rect note that I am using the image rect but asseperate rect to mount the image on.
rect_1 = pygame.Rect(1, 1, 178, 178)
...
cross = pygame.image.load("cross.png").convert_alpha()
cross_minified = pygame.transform.rotozoom(cross, 0, 0.25)
...
screen.blit(cross_minified, rect_1)
and after these codes I cannot find a way to center my x to the middle of the rect_1
it looks somewhat like this
Use the functionality of pygame.Rect. Get a rectangle with the size of the image by pygame.Surface.get_rect and set its center by the keyword argument center from the center of rect_1:
cross_rect = cross_minified.get_rect(center = rect_1.center)
screen.blit(cross_minified, cross_rect)
So I am Loading an Image in python and I need to Center the Image in Pygame. As Pygame starts the drawing from the upper left side of the screen so the x and y coordinates 0,0 doesn't work. Can anyone tell me the center x and y coordinates of the pygame window?
You can get the center of the window through the window rectangle (pygame.Surface.get_rect):
screen = pygame.display.set_mode((800, 600))
center = screen.get_rect().center
Use this to blit an image (pygame.Surface object) in the center of the screen:
screen.blit(image, image.get_rect(center = screen.get_rect().center))
pygame.Surface.get_rect() returns a rectangle with the size of the Surface object, that always starts at (0, 0). However, the position of the rectangle can be specified with a keyword argument. In this case, the center of the rectangle is set by the center of the screen.
When the 2nd argument of blit is a rectangle (pygame.Rect), the upper left corner of the rectangle will be used as the position for the blit.
import PIL.ImageDraw as ImageDraw,PIL.Image as Image, PIL.ImageShow as ImageShow
#that s my class Point(2D)
class Pnt(namedtuple('Pnt', 'x y')):
__slots__ = ()
def __init__(self, *args):
super(Pnt, self).__init__(*args)
Here is the vector of the vertices(convex polygon)
vertix = []
vertix.append(Pnt(50, 100))
vertix.append(Pnt(100, 200))
vertix.append(Pnt(200, 200))
vertix.append(Pnt(300, 0))
vertix.append(Pnt(250, -200))
vertix.append(Pnt(100, -100))
Here I want to draw the polygon. The problem is it is not centered, so almost half the polynom is outside the frame.
im = Image.new("RGB", (600,800))
draw = ImageDraw.Draw(im)
draw.polygon(vertix, fill=230, outline=255)
im.show()
If you want to center your polygon in the Image you can
1) determine the bounding box of your polygon,
2) calculate the coordinates of the center of the bounding box,
3) calculate the vector required to translate the center of the bounding box to the center of the Image rectangle,
4) create a new polygon by translating the coords of the vertices of the old poly by the vector found in step 3.