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

Agg*_*ras 5 python pygame screen-resolution

所以我试图用 python 和 pygame 制作游戏,但我注意到我无法制作高分辨率显示器,因为当我尝试制作具有更多像素的显示器时,pygame 窗口对于我的4k (3840x2160)显示器来说太大了. 我应该注意到我的显示器连接到一台分辨率为 (1366x768) 的旧戴尔笔记本电脑。但是当我输入这个时:print(pygame.display.list_modes())它告诉我我可以使用高达 4k 的分辨率,而不仅仅是我的笔记本电脑的分辨率。经过大量搜索和尝试后,我接受了我的游戏分辨率较低的事实并继续前进。当我继续编码游戏时,我想要一个弹出窗口,所以我导入了pyautogui,我的 pygame 窗口突然变得更小了。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()
Run Code Online (Sandbox Code Playgroud)

截图1

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

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()
Run Code Online (Sandbox Code Playgroud)

截图2

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

xav*_*avc 4

经过大量源代码挖掘后,我相信我已经找到了解决方案: pyautogui 导入pyscreeze功能center, grab, pixel, pixelMatchesColor, screenshot。第 63 至 71 行pyscreeze/__init__.py如下:

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.
Run Code Online (Sandbox Code Playgroud)

上面的代码调用SetProcessDPIAware,相当于以下内容:

系统 DPI 感知。此窗口不会根据 DPI 更改进行缩放。它将查询 DPI 一次,并在进程的生命周期内使用该值。如果 DPI 发生变化,进程将不会调整到新的 DPI 值。当DPI改变系统值时,系统会自动放大或缩小。

如果想获得相同的效果,只需在代码中pyautogui包含上述调用即可。SetProcessDPIAware