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

Pygame not returning events while embedded in PyQt

A functional code implementation: import time import contextlib from PySide6.QtCore import QObject, Signal, Qt, QThread from PySide6.QtGui import QImage, QPixmap from PySide6.QtWidgets import QMainWindow, QLabel, QWidget, QVBoxLayout, QApplication with contextlib.redirect_stdout(None): import pygame DISPLAY_WIDTH = 800 DISPLAY_HEIGHT = 600 class PygameWorker(QObject): send_image = Signal(QImage) def run(self): # Initialise screen pygame.init() screen = pygame.display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT), pygame.OPENGL) # … Read more

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 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