我可以在屏幕上移动 pygame 游戏窗口吗 (pygame)

Can I move the pygame game window around the screen (pygame)

提问人:Won't Tell 提问时间:6/13/2017 最后编辑:api55Won't Tell 更新时间:5/19/2023 访问量:7527

问:

在我正在制作的游戏中,我试图在屏幕上移动窗口以进行迷你游戏(不要问),我已经尝试了我看到自己的线程,但只找到了 1 个

x = 100
y = 0
import os
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (x,y)

import pygame
pygame.init()
screen = pygame.display.set_mode((100,100))

# wait for a while to show the window.
import time
time.sleep(2)

它不起作用 (请记住,我不是超级有经验,目前编码是一种爱好)

python pygame 窗口 movewindow

评论

0赞 Luke B 6/13/2017
os.environ['SDL_VIDEO_WINDOW_POS'] 仅设置窗口的开始位置。它不会移动已创建的窗口。在您给出的代码中,窗口应显示在左上角的 (100,0) 处。是吗?

答:

3赞 Nick Jarvis 6/15/2017 #1

查看下面的代码。我结合了两个不同的答案,但如果不使用 Tkinter,这似乎会非常困难。值得庆幸的是,我不认为 Tkinter 会过多地妨碍您的应用程序(在这里似乎很容易工作)。

# Moving a pygame window with Tkinter.
# Used code from:
#    https://stackoverflow.com/questions/8584272/using-pygame-features-in-tkinter
#    https://stackoverflow.com/questions/31797063/how-to-move-the-entire-window-to-a-place-on-the-screen-tkinter-python3

import tkinter as tk
import os, random

w, h = 400, 500

# Tkinter Stuffs
root = tk.Tk()
embed = tk.Frame(root, width=w, height=h)
embed.pack()

os.environ['SDL_WINDOWID'] = str(embed.winfo_id())
os.environ['SDL_VIDEODRIVER'] = 'windib' # This was needed to work on my windows machine.

root.update()

# Pygame Stuffs
import pygame
pygame.display.init()
screen = pygame.display.set_mode((w, h))

# This just gets the size of your screen (assuming the screen isn't affected by display scaling).
screen_full_size = pygame.display.list_modes()[0]

# Basic Pygame loop
done = False
while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                done = True

            if event.key == pygame.K_SPACE:
                # Press space to move the window to a random location.
                r_w = random.randint(0, screen_full_size[0])
                r_h = random.randint(0, screen_full_size[1])
                root.geometry("+"+str(r_w)+"+"+str(r_h))

    # Set to green just so we know when it is finished loading.
    screen.fill((0, 220, 0))

    pygame.display.flip()

    root.update()

pygame.quit()
root.destroy()
3赞 Deathlake9 3/25/2018 #2

我找到了一种不使用 .tkinter

如果调整显示大小,它将跳转到 的位置。os.environ['SDL_VIDEO_WINDOW_POS']

x=100
y=0
import os
os.environ['SDL_VIDEO_WINDOW_POS'] = '%d,%d' % (x,y)

#create a display
import pygame
pygame.init()
screen=pygame.display.set_mode((100,100))

#wait before moving the display
import time
time.sleep(2)

#set where the display will move to
x=200
y=200
os.environ['SDL_VIDEO_WINDOW_POS']='%d,%d' %(x,y)

#resize the screen causing it to move to x y set by environ
pygame.display.set_mode((101,100))

#set the size back to normal
pygame.display.set_mode((100,100))

评论

0赞 Jerrybibo 4/27/2018
此外,仅向系统建议放置窗口的位置;它可以拒绝这样做。os.environ['SDL_VIDEO_WINDOW_POS']
0赞 Eduardo Delfante 5/18/2023 #3

这是一个解决方案

import pygame, random
from pygame._sdl2 import Window

pygame.init()

# This will get the screen resolution, must be done before the 
# pygame.display.set_mode
display_info = pygame.display.Info()
window_size = (display_info.current_w, display_info.current_h)

surface = pygame.display.set_mode( (100, 200) )

window = Window.from_display_module()
window.position = (0,100) # Will place the window around the screen


inProgram = True
while inProgram:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            inProgram = False

        if event.type == pygame.KEYDOWN:

            if event.key == pygame.K_SPACE:
                # Move the window to a random screen position
                window.position = (random.randint(0, window_size[0]), 
                random.randint(30, window_size[1]))

    surface.fill((0, 100, 100))

    pygame.display.flip()

请记住,它仅适用于 pygame 2

希望对您有所帮助!