• Advertisement
Sign in to follow this  

pygame: fit background to resolution

This topic is 2924 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello everyone, here is my problem:´ I have coded a scrolling background. The window size of my screen is 200x100 and the size of the background image is 300x100. The scrolling works fine and everything. Now I wanted to code a method that scales the backrgound image proportionally when the window size changes. Somehow the image won't scale. Here is the main code of the scrolling background test:
import pygame
import engine

screen = pygame.display.set_mode((200, 100))
pygame.key.set_repeat(1, 1)
running = 1 

level_1 = pygame.image.load("bg2.png")
level_1 = engine.scale_to_resolution(level_1, screen)
background = engine.background(level_1, screen, (0,0))
background.draw()


while running:
	for event in pygame.event.get():
		if event.type == pygame.QUIT:
			running = 0
		elif event.type == pygame.KEYDOWN:
			if event.key == pygame.K_RIGHT:
				background.scroll_right()
			if event.key == pygame.K_LEFT:
				background.scroll_left()
	
	pygame.display.flip()

and here is the code of the scale_to_resolution() method:
def scale_to_resolution(image, surface):
	im_width, im_height = image.get_size()
	sf_width, sf_height = surface.get_size()
	pygame.transform.scale(image, ((im_width * sf_height / im_height), sf_height))
	return image

thank you for your help !! best regards djtschke

Share this post


Link to post
Share on other sites
Advertisement
Ha, solved it myself. has to be:

[source lang=python]
image = pygame.transform.scale(image, ((im_width * sf_height / im_height), sf_height))

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement