Home > Software design >  What is the correct way to resize an image in pygame?
What is the correct way to resize an image in pygame?

Time:11-09

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.

CodePudding user response:

  1. You have to get the rectangle from the scaled iamge

  2. pygame.Surface.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 left of 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).

  3. Update the display after drawing the image

targetSize = pygame.transform.scale(target, (5, 3))
target_rect = targetSize.get_rect(topleft = (x, y))

screen.blit(targetSize, (x, y))
pygame.display.flip()

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.MOUSEBUTTONDOWN:
            if target_rect.collidepoint(event.pos):
                print('clicked on image')
  • Related