How do I focus light or how do I only draw certain circular parts of the window in pygame?

I suggest a solution, which combines a clipping region pygame.Surface.set_clip and drawing a black rectangle with a circular transparent area in the center. Define a radius and create a square pygame.Surface with twice the radius. radius = 50 cover_surf = pygame.Surface((radius*2, radius*2)) Set a white color key which identifies the transparent color (set_colorkey) a nd … Read more

How do I convert an OpenCV image (BGR and BGRA) to a pygame.Surface object

The shape attribute of a numpy.array is the number of elements in each dimension. The first element is the height, the second the width and the third the number of channels. A pygame.Surface can be generated by pygame.image.frombuffer. The 1st argument can be a numpy.array and the 2nd argument is the format (RGB or RGBA). … Read more

How do I scale a PyGame image (Surface) with respect to its center?

You missed to update the size of self.pixeltitlerect after the Surface has been scaled: self.pixeltitle = pg.transform.scale(self.pixeltitle,(xsize,ysize)) # size of surface has been changed get the new rectangle self.pixeltitlerect = self.pixeltitle.get_rect() self.pixeltitlerect.center = (250,120) self.screen.blit(self.pixeltitle,self.pixeltitlerect) Or even shorter (see pygame.Surface.get_rect()): self.pixeltitle = pg.transform.scale(self.pixeltitle,(xsize,ysize)) self.pixeltitlerect = self.pixeltitle.get_rect(center = (250,120)) self.screen.blit(self.pixeltitle,self.pixeltitlerect) Do not scale the original Surface. … Read more

How can I drag more than 2 images in PyGame?

Create a list of 4 images: images = [img1, img2, img3, img4] Create a list of image rectangles: img_rects = [images[i].get_rect(topleft = (20+40*i, 20)) for i in range(len(images))] Use pygame.Rect.collidelist to find the image which is clicked: if e.type == pygame.MOUSEBUTTONDOWN: mouse_rect = pygame.Rect(e.pos, (1, 1)) current_image = mouse_rect.collidelist(img_rects) Draw the current_image: if e.type == … Read more

How to zoom in and out of an image pygame and using the mousposition as the center of the zoom

You’ll need to scale and blit the original image when you zoom. Use the attribute maprect to define the scaled size and relative location of the map. Add a method blit map that can scale and blit the map. Use the method in the constructor of the class App: class App: def __init__(self): # […] … Read more

pygame image background does not match main background

If the background has a uniform color, then you can set the transparent colorkey by pygame.Surface.set_colorkey. For instance if the background is the white (255, 255, 255): image = pygame.image.load(‘./car1.bmp’).convert() image.set_colorkey((255, 255, 255)) Anyway that won’t solve the issue if the background has multiple colors. I recommend to use an image format which supports per … Read more

SVG rendering in a PyGame application. Prior to Pygame 2.0, Pygame did not support SVG. Then how did you load it?

This is a complete example which combines hints by other people here. It should render a file called test.svg from the current directory. It was tested on Ubuntu 10.10, python-cairo 1.8.8, python-pygame 1.9.1, python-rsvg 2.30.0. #!/usr/bin/python import array import math import cairo import pygame import rsvg WIDTH = 512 HEIGHT = 512 data = array.array(‘c’, … Read more

How can I load an animated GIF and get all of the individual frames in Pygame?

Pygame itself does not support animated gifs, which is stated in the documentation. So you’ll have to 1) use some other piece of code / library to split the gif when you’re loading your sprites or images. Those you would find just by googling for “python split gif” or something. E.g. Python: Converting GIF frames … Read more

How to turn the sprite in pygame while moving with the keys

See How do I rotate an image around its center using PyGame? for rotating a surface. If you want to rotate an image around a center point (cx, cy) you can just do that: rotated_car = pygame.transform.rotate(car, angle) window.blit(rotated_car, rotated_car.get_rect(center = (cx, cy)) Use pygame.math.Vector2 to store the position and the direction of movement. Change … Read more

Drag multiple sprites with different “update ()” methods from the same Sprite class in Pygame

I recommend to create a class DragOperator which can drag an pygame.Rect object: class DragOperator: def __init__(self, sprite): self.sprite = sprite self.dragging = False self.rel_pos = (0, 0) def update(self, event_list): for event in event_list: if event.type == pygame.MOUSEBUTTONDOWN: self.dragging = self.sprite.rect.collidepoint(event.pos) self.rel_pos = event.pos[0] – self.sprite.rect.x, event.pos[1] – self.sprite.rect.y if event.type == pygame.MOUSEBUTTONUP: self.dragging … Read more