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