Dr. Numerus
Dr. Numerus

Reputation: 11

Pygame rescaling game-resolution + coordinates

i searched a lot about this, but no post seems to answere this specific question. So i am making a pygame application and i'm very new to this. For the screen scaling i use the following lines of code to get the user's screen informations:

infoObject = pygame.display.Info()
ScreenWidth = infoObject.current_w
ScreenHeight = infoObject.current_h
window = pygame.display.set_mode((ScreenWidth, ScreenHeight), pygame.FULLSCREEN)

But my problem here is: I use my current display stats, but another user could obviously have other stats. However, for the placement of text i'm using things like:

window.blit(text, (ScreenWidth/2 - text.get_rect().width/2, ScreenHeight*0.6))

to get it to the middle of the screen. If the resolution is changed, the location of the text will be too.

Sometimes i am also using things like:

window.blit(text, (ScreenWidth/2 - text.get_rect().width/2-500, ScreenHeight*0.6))

to move the text to the left. If the screen has only a width of 720 (for instance), it won't be visible. But since i work with relative positions here, the original layout/design will be no longer present neither. Does somebody know how i can "rescale" my game screen so it "automatically" rescales my text position, if that makes sense :) ?

Upvotes: 1

Views: 318

Answers (1)

Prune
Prune

Reputation: 77847

This is basic algebra: you scaled the screen midpoint, but you didn't scale the movement to the new screen dimensions. Since you didn't specify what "my current display stats" means in actual values, so ...

width_factor  = ScreenWidth  / my_current_display_width
height_factor = ScreenHeight / my_current_display_height
move_dist = 500 * width_factor
new_x = ScreenWidth/2 - text.get_rect().width/2 - 500
window.blit(text, (new_x, ScreenHeight*0.6))

I'm very suspicious about that magic 0.6 you threw in; from whence does that originate? Should that be height_factor?

Upvotes: 3

Related Questions