PyGame os.environ SDL_视频窗口_位置不工作

2024-10-04 07:30:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在PyGame中设置窗口的位置,但这种方法对我不起作用。它说当我打印出来时它会更新,但是视觉上没有任何变化

我的代码:

    import pygame as pg
from tkinter import messagebox as mb
import time
import sys
import requests
import os

pg.init()

sc = pg.display.set_mode((1000,750))
try:
    testimg143 = open("Data\\test.txt", "r")
except:
    ud = "ginfo\\"
else:
    ud = ""
    testimg143.close()

#main game loop

gamewindowfullscreen = False

def setgamewindowcenter():
    x = 500
    y = 100
    os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (x,y)
    print(os.environ['SDL_VIDEO_WINDOW_POS'])

while True:
    for event in pg.event.get():
        if (event.type == pg.KEYUP):
            if (event.key == pg.K_F11):
                if (gamewindowfullscreen == False):
                    sc = pg.display.set_mode((0,0), pg.FULLSCREEN)
                    setgamewindowcenter()
                    gamewindowfullscreen = True
                else:
                    sc = pg.display.set_mode((1000,750))
                    setgamewindowcenter()
                    gamewindowfullscreen = False
                    
        if event.type == pg.QUIT:
                pg.quit()
                exit()

Tags: importeventfalseifosmodeasdisplay
2条回答

对于Pygame的早期版本(2.0.0之前),可以通过设置SDL_VIDEO_WINDOW_POS并调用^{}(至少在某些系统中是这样)来更改窗口位置。
这不再可能。如果要更改窗口的位置,必须重新初始化^{}模块pygame.display.quit() pygame.display.init()
在任何情况下,都需要在调用pygame.display.set_mode()之前设置SDL_VIDEO_WINDOW_POS

最简单的例子:

import pygame as pg
import os

def setgamewindowcenter(x = 500, y = 100):
    os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (x,y)
    print(os.environ['SDL_VIDEO_WINDOW_POS'])

pg.init()
setgamewindowcenter()
sc = pg.display.set_mode((1000, 750))

gamewindowfullscreen = False
run = True
while run:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            run = False

        if event.type == pg.KEYUP:
            if event.key == pg.K_F11:
                if gamewindowfullscreen == False:
                    sc = pg.display.set_mode((0,0), pg.FULLSCREEN)
                    gamewindowfullscreen = True
                else:
                    pg.display.quit()
                    setgamewindowcenter()
                    pg.display.init()
                    sc = pg.display.set_mode((1000, 750))
                    gamewindowfullscreen = False
                    
pg.quit()
exit()

如果您在windows上,则可以使用C API通过^{}移动窗口

from ctypes import windll, Structure, wintypes, pointer

SWP_NOMOVE = 0x0002
SWP_NOOWNERZORDER = 0x0200
SWP_NOSIZE = 0x0001


def move_window_abs(x, y, hwnd=None):
    if hwnd is None:
        hwnd = pygame.display.get_wm_info()["window"]
    windll.user32.SetWindowPos(hwnd, 0, x, y, 0, 0, SWP_NOSIZE | SWP_NOOWNERZORDER)

与辅助函数一起获得准确位置:

class Rect(Structure):
    _fields_ = [
        ("left", wintypes.LONG),
        ("top", wintypes.LONG),
        ("right", wintypes.LONG),
        ("bottom", wintypes.LONG),
    ]


def get_window_rect(hwnd=None):
    if hwnd is None:
        hwnd = pygame.display.get_wm_info()["window"]
    r = Rect(0, 0, 0, 0)
    windll.user32.GetWindowRect(hwnd, pointer(r))
    return pygame.Rect(r.left, r.top, r.right - r.left, r.bottom - r.top)

还可以实现相对移动功能

def move_window_rel(dx, dy, hwnd=None):
    if hwnd is None:
        hwnd = pygame.display.get_wm_info()["window"]
    r = get_window_rect(hwnd)
    windll.user32.SetWindowPos(hwnd, 0, r.x + dx, r.y + dy, 0, 0, SWP_NOSIZE | SWP_NOOWNERZORDER)

请注意,这些函数设置窗口的左上角,而不是中心

相关问题 更多 >