Won't Tell
Won't Tell

Reputation: 81

Can I move the pygame game window around the screen

In the game I'm making, I'm trying to move the window around the screen for a mini game (don't ask) and I've tried what I saw own threads and only found 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)

and it doesn't work (keep in mind I'm not super experienced and currently code as a hobby)

Upvotes: 8

Views: 9804

Answers (4)

Eduardo Delfante
Eduardo Delfante

Reputation: 23

Here is a solution

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

Keep in mind that it only works in pygame 2

Hope it was helpful!

Upvotes: 1

Pimus
Pimus

Reputation: 1

import pygame
from pygame._sdl2 import Window
import sys

pygame.init()
ventana = pygame.display.set_mode((500, 400))
pygame.display.set_caption('Mi juego')

# Mover ventana a la posicion x=10, y=30
window = Window.from_display_module()
window.position = (10, 30)


while True:
    for evento in pygame.event.get():
        if evento.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

Upvotes: -1

Deathlake9
Deathlake9

Reputation: 73

I've found a way of doing it without using tkinter.

If you resize the display, it will jump to the location of 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))

Upvotes: 3

Nick Jarvis
Nick Jarvis

Reputation: 131

Check out the below code. I kind of combined two different answers, but it seems like it will be pretty difficult without using Tkinter. Thankfully I don't think Tkinter will get in the way of your application too much (seemed to work pretty easily here).

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

Upvotes: 3

Related Questions