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

2024/10/6 20:40:48

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)

Answer

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-python3import tkinter as tk
import os, randomw, 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 = Trueif event.type == pygame.KEYDOWN:if event.key == pygame.K_ESCAPE:done = Trueif 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()
https://en.xdnf.cn/q/70321.html

Related Q&A

mocking a function within a class method

I want to mock a function which is called within a class method while testing the class method in a Django project. Consider the following structure: app/utils.py def func():...return resp # outcome i…

After resizing an image with cv2, how to get the new bounding box coordinate

I have an image of size 720 x 1280, and I can resize it to 256 x 256 like thisimport cv2 img = cv2.imread(sample_img.jpg) img_small = cv2.resize(img, (256, 256), interpolation=cv2.INTER_CUBIC)Say I hav…

convert a tsv file to xls/xlsx using python

I want to convert a file in tsv format to xls/xlsx..I tried usingos.rename("sample.tsv","sample.xlsx")But the file getting converted is corrupted. Is there any other method of doing…

How do you edit cells in a sparse matrix using scipy?

Im trying to manipulate some data in a sparse matrix. Once Ive created one, how do I add / alter / update values in it? This seems very basic, but I cant find it in the documentation for the sparse ma…

AttributeError: DataFrame object has no attribute _data

Azure Databricks execution error while parallelizing on pandas dataframe. The code is able to create RDD but breaks at the time of performing .collect() setup: import pandas as pd # initialize list of …

Python: Problem with overloaded constructors

WARNING: I have been learning Python for all of 10 minutes so apologies for any stupid questions!I have written the following code, however I get the following exception: Message FileName Li…

Validate inlines before saving model

Lets say I have these two models:class Distribution(models.Model):name = models.CharField(max_length=32)class Component(models.Model):distribution = models.ForeignKey(Distribution)percentage = models.I…

Grouping and comparing groups using pandas

I have data that looks like:Identifier Category1 Category2 Category3 Category4 Category5 1000 foo bat 678 a.x ld 1000 foo bat 78 l.o …

Transform a 3-column dataframe into a matrix

I have a dataframe df, for example:A = [["John", "Sunday", 6], ["John", "Monday", 3], ["John", "Tuesday", 2], ["Mary", "Sunday…

python multiline regex

Im having an issue compiling the correct regular expression for a multiline match. Can someone point out what Im doing wrong. Im looping through a basic dhcpd.conf file with hundreds of entries such as…