0

I have a code that creates a sprite by right-clicking, when you click on it with the left mouse button, it should move, but when i try to move one of sprites, it only moves the last one, but I need that when I hover the mouse over the sprite, only it can be moved, while the rest remain in their places, heres code

import pygame as pg
import sys
import task
import data

pg.init()
screen = pg.display.set_mode((800, 600))
clock = pg.time.Clock()

while True:
    for event in pg.event.get():
        if event.type == pg.QUIT or event.type == pg.KEYDOWN and event.key == pg.K_ESCAPE:
            pg.quit()
            sys.exit()

    if event.type == pg.MOUSEBUTTONDOWN and event.button == 3:
        card = task.Card(event.pos)
        data.all_sprites.add(card)
    if event.type == pg.MOUSEBUTTONDOWN and event.button == 1:
        data.MouseDown = True
    elif event.type == pg.MOUSEBUTTONUP and event.button == 1:
        data.MouseDown = False
        data.SetMove = False
    if event.type == pg.MOUSEMOTION and data.MouseDown:
        card.move()

screen.fill('white')
data.all_sprites.draw(screen)
data.all_sprites.update()
pg.display.update()
clock.tick(60)

and the Card class

import pygame as pg
import data

class Card(pg.sprite.Sprite):
    def __init__(self, pos):
        super().__init__()
        self.image = pg.image.load("graphics/BlankTask.jpg").convert()
        self.rect = self.image.get_rect(center=pos)
        self.rect.x = pos[0]
        self.rect.y = pos[1]

def update(self):
    if self.rect.collidepoint(pg.mouse.get_pos()[0], pg.mouse.get_pos()[1]) and 
data.MouseDown:
        data.SetMove = True

def move(self):
    if data.SetMove:
        self.rect.x = pg.mouse.get_pos()[0]
        self.rect.y = pg.mouse.get_pos()[1]
F1xed
  • 1
  • 1
  • Please correct the [Indentation](https://docs.python.org/3/reference/lexical_analysis.html)in your code snippets. – Rabbid76 Oct 11 '22 at 17:21

0 Answers0