Sign in to follow this  
djtschke

pygame: fit background to resolution

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this