-1
import pygame
from sys import exit

pygame.init()
screen = pygame.display.set_mode((2000, 1000))
pygame.display.set_caption("Pygame Window")
clock = pygame.time.Clock()

test = pygame.Rect(950, 900, 100, 100)

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()

    pygame.draw.rect(screen, (255,0,0), test)

    mouse_pos = pygame.mouse.get_pos()
    if event.type == pygame.MOUSEBUTTONDOWN:
        print('click')
        pygame.draw.rect(screen, (255,0,0), test)

    pygame.display.update()
    clock.tick(60)

In the code above I want to move the rectangle "test" to the mouse cursor position only when I click my mouse.

How do I do this?

1 Answers1

0

Move the rectangle to the mouse cursor position before drawing the rectangle -

test.center = event.pos