I have an image in pygame and my code detects if this image is clicked on. It worked fine and I decided to resize the image, but when I did that the image randomly disappeared. Here was the code before the image disappeared:
import pygame
pygame.init()
width = 500
height = 500
screen = pygame.display.set_mode((width, height))
white = (255, 255, 255)
screen.fill(white)
pygame.display.set_caption('Aim Trainer')
target = pygame.image.load("aim target.png").convert_alpha()
x = 20 # x coordinate of image
y = 30 # y coordinate of image
screen.blit(target, (x, y)) # paint to screen
pygame.display.flip() # paint screen one time
targetSize = pygame.transform.scale(target, (5, 3))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
# Set the x, y positions of the mouse click
x, y = event.pos
if target.get_rect().collidepoint(x, y):
print('clicked on image')
# loop over, quite pygame
pygame.quit()
and here is my code after:
import pygame
pygame.init()
width = 500
height = 500
screen = pygame.display.set_mode((width, height))
white = (255, 255, 255)
screen.fill(white)
pygame.display.set_caption('Aim Trainer')
target = pygame.image.load("aim target.png").convert_alpha()
x = 20 # x coordinate of image
y = 30 # y coordinate of image
pygame.display.flip() # paint screen one time
targetSize = pygame.transform.scale(target, (5, 3))
screen.blit(targetSize, (x, y)) # paint to screen
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
# Set the x, y positions of the mouse click
x, y = event.pos
if target.get_rect().collidepoint(x, y):
print('clicked on image')
# loop over, quite pygame
pygame.quit()
As you can see, the only thing that changed was me renaming screen.blit(target, (x, y))
to screen.blit(targetSize, (x, y))
, and me moving this line of code a few lines furthur down to avoid a 'TargetSize is not defined' error. But for some reason, this change makes the image disappear. The program still detects it when I click on the image, it's just that the image isn't visible.