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

Won*_*ell 6 python pygame window movewindow

在我正在制作的游戏中,我试图在屏幕上移动窗口进行迷你游戏(不要问)我已经尝试过我看到的自己的线程并且只找到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)
Run Code Online (Sandbox Code Playgroud)

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

小智 3

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

# Moving a pygame window with Tkinter.
# Used code from:
#    /sf/ask/600899071/
#    /sf/ask/2225794441/

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