How to move an image when clicked on it in PYGAME?

Question:

Basically, when I click on an image I want the image to move to a new different location. When I click again, it should move again.

import pygame
import random

pygame.init()

screen = pygame.display.set_mode((1420, 750))

pygame.display.set_caption("Soccer Game")
icon = pygame.image.load('soccer-ball-variant.png')
pygame.display.set_icon(icon)


ball = pygame.image.load('soccer2.png')
ballrect = ball.get_rect()

X = random.randrange(0, 1100)
Y = random.randrange(0, 600)

def player():
    screen.blit(ball, (X, Y))

run = True
while run:
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.MOUSEBUTTONDOWN:
            x, y = event.pos
            if ball.get_rect().collidepoint(x, y):
                X = random.randrange(0, 1100)
                Y = random.randrange(0, 600)
                player()



    screen.fill((255, 255, 255))
    player()

    pygame.display.update()

The problem is that this program only works when i click on the left-up corner of the screen, and not on the ball. I am a biginner in the pygame module, but i think the problem is this if statement:

if ball.get_rect().collidepoint(x, y):
    X = random.randrange(0, 1100)
    Y = random.randrange(0, 600)
    player()
Asked By: Benjamin Starck

||

Answers:

pygame.Surface.get_rect.get_rect() returns a rectangle with the size of the Surface object, that always starts at (0, 0) since a Surface object has no position. A Surface is blit at a position on the screen. The position of the rectangle can be specified by a keyword argument. For example, the top leftof the rectangle can be specified with the keyword argument topleft. These keyword argument are applied to the attributes of the pygame.Rect before it is returned (see pygame.Rect for a full list of the keyword arguments).

if ball.get_rect().collidepoint(x, y):

if ball.get_rect(topleft = (X, Y)).collidepoint(x, y):

However, I recommend removing the X and Y variables, but using ballrect instead:

ballrect = ball.get_rect()
ballrect.x = random.randrange(0, 1100)
ballrect.y = random.randrange(0, 600)

def player():
    screen.blit(ball, ballrect)
if ballrect.collidepoint(event.pos):
    ballrect.x = random.randrange(0, 1100)
    ballrect.y = random.randrange(0, 600)

Complete example:

import pygame
import random

pygame.init()
screen = pygame.display.set_mode((1420, 750))
pygame.display.set_caption("Soccer Game")
icon = pygame.image.load('soccer-ball-variant.png')
pygame.display.set_icon(icon)

ball = pygame.image.load('soccer2.png')
ballrect = ball.get_rect()
ballrect.x = random.randrange(0, 1100)
ballrect.y = random.randrange(0, 600)

def player():
    screen.blit(ball, ballrect)

run = True
while run: 
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.MOUSEBUTTONDOWN:
            if ballrect.collidepoint(event.pos):
                ballrect.x = random.randrange(0, 1100)
                ballrect.y = random.randrange(0, 600)

    screen.fill((255, 255, 255))
    player()
    pygame.display.update()
Answered By: Rabbid76
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.