Captain Confused
Captain Confused

Reputation: 13

pygame: how to display full-screen without cutting off edges

My game is designed to work with a 16:9 display ratio.

However, my computer monitor does not have a 16:9 display. So, I've tried various methods to tell pygame to stretch the game window to full-screen, And I've encountered various problems such as:

1- The screen goes black, and my monitor says: "resolution mismatch".

2- The game window gets stretched to fit, and this messes up the graphics.

3- The edges of the screen get cut off, this is VERY unacceptable as it would give some players a disadvantage concerning how much of the playing field they can see!

I want pygame to display the game in full-screen without cutting off edges...I want it to add black bars to ether the top and bottom, or the left and right edges of the screen when necessary-depending on the players monitor.

Thanks in advance!

(And honestly, I can't believe I'm having so much trouble with what should be just a simple command, but I can't find answers anywhere!)

Upvotes: 1

Views: 1882

Answers (2)

Lost Robot
Lost Robot

Reputation: 1321

This is how you would scale the screen to fit any monitor, while still keeping the aspect ratio.

First you would use this code (or similar) to calculate what the screen needs to be scaled to:

import pygame
pygame.init()
infostuffs = pygame.display.Info() # gets monitor info

monitorx, monitory = infostuffs.current_w, infostuffs.current_h # puts monitor length and height into variables

dispx, dispy = <insert what you want your display length to be>, <and height>

if dispx > monitorx: # scales screen down if too long
    dispy /= dispx / monitorx
    dispx = monitorx
if dispy > monitory: # scales screen down if too tall
    dispx /= dispy / monitory
    dispy = monitory

dispx = int(dispx) # So your resolution does not contain decimals
dispy = int(dispy)

This gives you dispx and dispy, which are the dimensions that you should scale your display to every loop before you update the display. Also, just to warn you, I have not been able to test this code. If there is anything wrong, please tell me in the comments so I can fix it.

EDIT: Added two more lines of code.

Upvotes: 2

bstipe
bstipe

Reputation: 282

I have not tried it, but my approach would be:

1. 16 / 9 ~= 1.778
2. `pygame.init()` ; `scr = pygame.display.Info()` ; `win_size = width, height = scr.current_w, scr.current_h` should give the display width and height.
3. Multiply height by 1.778, `x = int(height * 1.778)`.
4. If x < width, then width = x.
5. If not, then divide width by 1.7788, `y = int(width / 1.778)`. Now, height = y
6. `win_size = width, height` ; `screen = pygame.display.set_mode(win_size, FULLSCREEN)`
7. Scale and center align your graphics to fit.

Upvotes: 0

Related Questions