【问题标题】:pygame: how to display full-screen without cutting off edgespygame:如何在不切断边缘的情况下全屏显示
【发布时间】:2016-11-02 01:29:22
【问题描述】:

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

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

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

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

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

我希望 pygame 在不切断边缘的情况下全屏显示游戏...我希望它在必要时在屏幕的顶部和底部或左右边缘添加黑条 - 取决于玩家监控。

提前致谢!

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

【问题讨论】:

  • 你能添加一些你已经尝试过的东西吗?这可能有助于确定问题出在哪里。
  • 对不起,但我很确定显示我的代码不会有帮助。如果有人可以向我展示一个有效的代码,我应该很高兴!

标签: pygame fullscreen


【解决方案1】:

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

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

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,它们是您应该将显示缩放到每个循环在更新显示之前的尺寸。另外,只是提醒您,我无法测试此代码。如果有任何问题,在 cmets 中告诉我,以便我修复它。

编辑:添加了另外两行代码。

【讨论】:

  • 我尝试了您的代码,现在我收到一条错误消息,内容为:“需要整数参数,得到浮点数”
  • 那是因为 Pygame 中的某些函数需要整数,而不是浮点数。缩放图像就是其中之一,因此您可以将值转换为整数。感谢您指出这一点,我编辑了这个问题。
【解决方案2】:

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

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.

【讨论】:

    猜你喜欢
    • 2021-03-28
    • 1970-01-01
    • 1970-01-01
    • 2021-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    相关资源
    最近更新 更多