为什么我的 pygame window 不适合我的 4k(3840x2160) 显示器? pygame window 的比例:(3000x1500)

Why does my pygame window not fit in my 4k(3840x2160) monitor? scale of pygame window: (3000x1500)

所以我试图用 python 和 pygame 制作游戏,但我注意到我无法制作高分辨率显示器,因为当我尝试制作具有更多像素的显示器时, pygame window 对于我的 4k (3840x2160) 显示器来说太大了。我应该注意到我的显示器连接到分辨率为 (1366x768) 的旧戴尔笔记本电脑。 但是当我输入这个时:print(pygame.display.list_modes())它告诉我我可以使用高达 4k 的分辨率,而不仅仅是分辨率我的笔记本电脑。经过大量搜索和尝试后,我接受了我的游戏分辨率较低的事实并继续前进。当我继续编写游戏代码时,我想要一个弹出窗口 window,所以我导入了 pyautogui,我的 pygame window 突然变小了。 BOOM 问题已解决。我提高了分辨率,我没有遇到任何问题,我的游戏现在 运行 处于非常高的 分辨率 !我很困惑所以我做了一个非常简单的 pygame 程序所以我可以测试它并且它确实有效。这是低质量并且不适合我的屏幕

import pygame
import sys

pygame.init()
screen = pygame.display.set_mode((3000, 1500))
font = pygame.font.Font('font.otf', 50)

while True:
    screen.fill((255, 255, 255))
    txt = font.render("hello", True, (0, 0, 0))
    screen.blit(txt, (100, 100))
    pygame.display.update()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

screenshot1

这是高分辨率并且适合我的屏幕:

import pygame
import sys
import pyautogui

pygame.init()
screen = pygame.display.set_mode((3000, 1500))
font = pygame.font.Font('font.otf', 50)

while True:
    screen.fill((255, 255, 255))
    txt = font.render("hello", True, (0, 0, 0))
    screen.blit(txt, (100, 100))
    pygame.display.update()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

screenshot2

我什至不需要使用 pyautogui! 谁可以给我解释一下这个? 谢谢

经过大量的源代码潜水后,我相信我已经找到了解决方案:pyautogui 为函数 center, grab, pixel, pixelMatchesColor, screenshot 导入 pyscreezepyscreeze/__init__.py 的第 63 到 71 行如下:

if sys.platform == 'win32':
    # On Windows, the monitor scaling can be set to something besides normal 100%.
    # PyScreeze and Pillow needs to account for this to make accurate screenshots.
    # TODO - How does macOS and Linux handle monitor scaling?
    import ctypes
    try:
       ctypes.windll.user32.SetProcessDPIAware()
    except AttributeError:
        pass # Windows XP doesn't support monitor scaling, so just do nothing.

上面的代码调用了SetProcessDPIAware,等价于:

System DPI aware. This window does not scale for DPI changes. It will query for the DPI once and use that value for the lifetime of the process. If the DPI changes, the process will not adjust to the new DPI value. It will be automatically scaled up or down by the system when the DPI changes from the system value.

如果想在没有 pyautogui 的情况下获得相同的效果,您可以在代码中包含上面对 SetProcessDPIAware 的调用。