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

Bri*_*ian 4 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()
Run Code Online (Sandbox Code Playgroud)

小智 5

我遇到了同样的问题,我设法找到了答案并将其张贴在这里。我找到的答案如下:



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

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

他们在我链接的页面上提供了一个示例代码,用于展示如何解决这个问题。

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

ctypes.windll.user32.SetProcessDPIAware()
Run Code Online (Sandbox Code Playgroud)

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

话虽如此,为了完成这项工作,请将这段代码放在 pygame.display.set_mode() 之前的任何位置

import ctypes
ctypes.windll.user32.SetProcessDPIAware()
#
# # # Anywhere Before
#
pygame.display.set_mode(resolution)
Run Code Online (Sandbox Code Playgroud)

我希望这可以帮助你和其他任何发现他们有同样问题的人。