pygame os.environ sdl\u video\u window\u pos不工作

ux6nzvsh  于 2021-09-08  发布在  Java
关注(0)|答案(1)|浏览(438)

我试图在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()
vcudknz3

vcudknz31#

对于pygame的早期版本(2.0.0之前),可以通过设置更改窗口位置 SDL_VIDEO_WINDOW_POS 和呼唤 pygame.display.set_mode() (至少在某些系统。
这已经不可能了。如果要更改窗口的位置,必须重新初始化 display 模块 pygame.display.quit()pygame.display.init() .
无论如何 SDL_VIDEO_WINDOW_POS 需要在调用之前进行设置 pygame.display.set_mode() .
最简单的例子:

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()

相关问题