pygame:如何在不切断边缘的情况下显示全屏

时间:2016-10-24 11:54:50

标签: pygame fullscreen

我的游戏旨在以16:9的显示比例工作。

但是,我的电脑显示器没有16:9显示屏。因此,我尝试了各种方法来告诉pygame将游戏窗口拉伸到全屏,并且我遇到了各种问题,例如:

1-屏幕变黑,我的显示器显示:"分辨率不匹配"。

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

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

我希望pygame能够在不切断边缘的情况下全屏显示游戏......我希望它在必要时添加黑条以保持屏幕的顶部和底部,或者屏幕的左右边缘 - 具体取决于球员监视。

提前致谢!

(老实说,我不能相信我应该只是一个简单的命令会遇到很多麻烦,但我无法在任何地方找到答案!)

2 个答案:

答案 0 :(得分:0)

我没有尝试过,但我的方法是:

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.

答案 1 :(得分:0)

这是如何缩放屏幕以适合任何显示器,同时仍然保持纵横比。

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

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,这是您应该在更新显示之前将显示缩放到每个循环的尺寸。另外,只是为了警告你,我无法测试此代码。如果有任何错误,在评论中告诉我,以便我可以修复它。

编辑:增加了两行代码。