python - Pygame display.info 给出了错误的分辨率大小

标签 python pygame

我正在使用 pygame 构建一个小游戏。我希望游戏窗口的大小与显示器分辨率相同。我的电脑屏幕分辨率是 1920x1080,display.info 说窗口大小也是 1920x1080,但是当我运行它时,它创建的窗口大约是我屏幕大小的一倍半。

import pygame, sys

def main():
    #set up pygame, main clock
    pygame.init()
    clock = pygame.time.Clock()

    #creates an object with the computers display information
    #current_h, current_w gives the monitors height and width
    displayInfo = pygame.display.Info()

    #set up the window
    windowWidth = displayInfo.current_w
    windowHeight = displayInfo.current_h
    window = pygame.display.set_mode ((windowWidth, windowHeight), 0, 32)
    pygame.display.set_caption('game')

    #gameLoop
    while True:
        window.fill((0,0,0))
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

        #draw the window onto the screen
        pygame.display.flip()
        clock.tick(60)

main()

最佳答案

我一直遇到同样的问题,我设法找到了答案并发布了它 here .我找到的答案如下:



我设法在此处的 Pygame BitBucket 页面上找到了一个提交,它解释了这个问题并给出了一个如何修复它的示例。

正在发生的事情是,某些显示环境可以配置为拉伸(stretch)窗口,因此它们在高 PPI(每英寸像素)显示器上看起来不会很小。这种拉伸(stretch)导致在较高分辨率下显示的内容比实际显示的要大。

他们在我链接到的页面上提供了示例代码,以显示如何解决此问题。

他们通过导入 ctypes 并调用它来解决这个问题:

ctypes.windll.user32.SetProcessDPIAware()

他们还表示,这是一个仅限 Windows 的解决方案,自 Python 2.4 起可在基本 Python 中使用。在此之前,需要安装它。

话虽如此,要使其正常工作,请将这段代码放在 pygame.display.set_mode() 之前的任何位置

import ctypes
ctypes.windll.user32.SetProcessDPIAware()
#
# # # Anywhere Before
#
pygame.display.set_mode(resolution)


我希望这对您和其他遇到同样问题的人有所帮助。

关于python - Pygame display.info 给出了错误的分辨率大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27421391/

相关文章:

python - 使用 Tensorflow 2.0 使用多个 GPU 进行训练时出现错误 : Out of range: End of sequence

python - 从列表中绘制的对象不会停留在屏幕上(pygame)

python - 我的 py2app 应用程序无法打开。有什么问题?

python - 如何在 pygame 中使用相同的类创建独立工作的重复 Sprite ?

python - 如何在Python中利用字节数组的每一位

python - Django 调试工具栏仅适用于管理部分

python - 如何在anaconda中安装包?

python - 清除win7中的python IDLE屏幕

python-3.x - 包装io.BufferedIOBase,使其变为可搜索

python - 将两个 pygame.Color 对象混合在一起