Skip to content Skip to sidebar Skip to footer

How Can I Change The Position Of A Surface(game Window) With Respect To Computer Screen In Pygame?

I want to change the position of Game window with respect to Computer Screen. But couldn't find anything in Documentation. Please Help me.

Solution 1:

On windows, it is possible to change the position of an initialized window using its handle (hwnd). In User32.dll there is a function called MoveWindow, that recieves a window's handle and changes its position. You can call it using python's standard ctypes module.

from ctypes import windll

defmoveWin(x, y):
    # the handle to the window
    hwnd = pygame.display.get_wm_info()['window']

    # user32.MoveWindow also recieves a new size for the window
    w, h = pygame.display.get_surface().get_size()

    windll.user32.MoveWindow(hwnd, x, y, w, h, False)

Solution 2:

You can set the position that the pygame display initializes with environment variables that can be accessed with os.environ. An example from http://www.pygame.org/wiki/SettingWindowPosition?parent=CookBook:

x = 100y = 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)

I do not think it's possible to change the position of an already initialized display, but you could quit the current display, set the environment variable, and reinitialize it.

Post a Comment for "How Can I Change The Position Of A Surface(game Window) With Respect To Computer Screen In Pygame?"