pygame: 如何全屏显示不切边

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

我的游戏设计为使用 16:9 显示比例。

但是,我的电脑显示器没有16:9显示。所以,我尝试了各种方法告诉 pygame 将游戏 window 拉伸到全屏,我遇到了各种问题,例如:

1- 屏幕变黑,我的显示器显示:"resolution mismatch"。

2- 游戏 window 被拉伸以适应,这会弄乱图形。

3- 屏幕边缘被切断,这是非常不可接受的,因为这会让一些玩家在他们能看到多少比赛场地方面处于劣势!

我想要 pygame 以全屏显示游戏而不截断边缘...我想要它添加黑条到屏幕的顶部和底部或左右边缘必要时——取决于玩家监视器。

提前致谢!

(老实说,我不敢相信我在执行一个简单的命令时遇到了这么多麻烦,但我在任何地方都找不到答案!)

我没试过,但我的做法是:

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.

这是缩放屏幕以适应 任何 显示器的方式,同时仍保持纵横比。

首先,您将使用此代码(或类似代码)来计算屏幕需要缩放到的大小:

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)

这为您提供了 dispx 和 dispy,它们是您应该在每个循环 更新显示之前 缩放显示的维度。另外,提醒您,我无法测试此代码。如果有任何问题,在评论中告诉我,以便我能修好它。

编辑:添加了两行代码。