Pygame Tutorial 1 – part 3.1

To scale an image with pygame is easy:

  • load the image
  • get the size of the screen
  • blit the image on the screen using pygame.transoform.scale passing the image and the dimension you want the image to be scaled to in a tuple

import pygame

# SCALE AN IMAGE TO FIT THE SCREEN
 
pygame.init()
 
screen = pygame.display.set_mode((600, 500))
bg = pygame.image.load("image.png").convert_alpha()
sw, sh = screen.get_size()
while True:
    screen.blit(pygame.transform.scale(bg, (sw, sh)),(0, 0))
    if pygame.event.get(pygame.QUIT):
        break
    pygame.display.update()
pygame.quit()

Live coding video

xds


Subscribe to the newsletter for updates
Tkinter templates
Avatar My youtube channel

Twitter: @pythonprogrammi - python_pygame

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.