Pygame FULLSCREEN显示标志创建一个对于屏幕来说太大的游戏屏幕



更新的问题

我发现问题似乎是因为我正在使用FULLSCREEN显示标志来创建窗口。我在屏幕的左上角添加了一个矩形(0,0(,但当我运行程序时,它大部分都在屏幕外。然后,当我来回Alt Tab键时,矩形被适当地放置在0,0处,并且炮塔偏离中心。

所以基本上,当程序启动时,游戏屏幕比我的实际屏幕大,但居中。然后在Alt Tab之后,游戏屏幕上排列着0,0,但由于游戏屏幕比我的屏幕大,所以炮塔看起来偏离了中心,但实际上是相对于游戏居中的。

所以真正的问题是,为什么使用FULLSCREEN显示标志会使屏幕比我的电脑屏幕大?

原始问题

我正在屏幕中央构建一个简单的炮塔演示,它跟随光标的位置,好像在它所在的地方开火。一切都很完美,直到我用Alt Tab键离开屏幕,然后用Alt Tab返回。此时,炮塔偏离中心(向下和向右(

import pygame, math
pygame.init()
image_library = {}
screen_dimen = pygame.display.Info()
print("Screen Dimensions ", screen_dimen)
def get_image(name):
    if name not in image_library:
        image = pygame.image.load(name)
        image_library[name] = image
    else:
        image = image_library[name]
    return image
robot_turret_image = get_image('robot_turret.png')
screen = pygame.display.set_mode((0, 0), pygame.`FULLSCREEN`)
done = False
clock = pygame.time.Clock()
while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        if event.type == pygame.MOUSEMOTION:
            print(event.pos)
        if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
            done = True
    screen.fill((0, 0, 0))
    pos = pygame.mouse.get_pos()
    angle = 360 - math.atan2(pos[1] - (screen_dimen.current_h / 2),
                             pos[0] - (screen_dimen.current_w / 2)) * 180 / math.pi
    rot_image = pygame.transform.rotate(robot_turret_image, angle)
    rect = rot_image.get_rect(center=(screen_dimen.current_w / 2, screen_dimen.current_h / 2))
    screen.blit(rot_image, rect)
    color = (0, 128, 255)
    pygame.draw.rect(screen, color, pygame.Rect(0, 0, 200, 200))
    pygame.display.update()
    clock.tick(60)

中心好像已经关闭了。我已经打印出了Alt Tab前后的屏幕尺寸,它们是一样的,所以我不明白为什么图像会移动。我相信我错过了一些关于Pygame状态变化的东西,但不知道是什么。如果相关的话,我使用的是Windows 10。

好吧,我从gamedev.stackexchange 中发现了一个解决方案

我会在这里重新散列。问题是,使用全屏标签会使屏幕比我的电脑屏幕大。下面的代码解决了这个

import ctypes
ctypes.windll.user32.SetProcessDPIAware()
true_res = (ctypes.windll.user32.GetSystemMetrics(0), ctypes.windll.user32.GetSystemMetrics(1))
pygame.display.set_mode(true_res,pygame.FULLSCREEN)

需要注意的是,这可能只是一个windows修复程序,但我没有其他系统可以测试它。但它适用于带有python 3.5.1和pygame 1.9.2a0 的windows 10

最新更新