python - How to scale images to screen size in Pygame -


i wondering how go scaling size of images in pygame projects resolution of screen. example, envisage following scenario assuming windowed display mode time being; assume full screen same:

  • i have 1600x900 background image of course displays natively in 1600x900 window

  • in 1280x720 window can scale images' rect 1280x720

  • what happens, if need add, 300x300 px image @ x,y 1440,860 (example sizes) sized fit original 1600x900 background? of course 1600x900 can of course use image natively smaller/larger window sizes?

basically, how scale images window size , position them accordingly? guess there must easy automated method right can't figure out , quite frankly haven't got time search it...

thanks in advance, ilmiont

you can scale image pygame.transform.scale:

import pygame picture = pygame.image.load(filename) picture = pygame.transform.scale(picture, (1280, 720)) 

you can bounding rectangle of picture with

rect = picture.get_rect() 

and move picture with

rect = rect.move((x, y)) screen.blit(picture, rect) 

where screen set like

screen = pygame.display.set_mode((1600, 900)) 

to allow widgets adjust various screen sizes, make display resizable:

import os import pygame pygame.locals import *  pygame.init() screen = pygame.display.set_mode((500, 500), hwsurface | doublebuf | resizable) pic = pygame.image.load("image.png") screen.blit(pygame.transform.scale(pic, (500, 500)), (0, 0)) pygame.display.flip() while true:     pygame.event.pump()     event = pygame.event.wait()     if event.type == quit:         pygame.display.quit()     elif event.type == videoresize:         screen = pygame.display.set_mode(             event.dict['size'], hwsurface | doublebuf | resizable)         screen.blit(pygame.transform.scale(pic, event.dict['size']), (0, 0))         pygame.display.flip()