#๐Ÿ”’ Pygame window freezing?

27 messages ยท Page 1 of 1 (latest)

warm mountain
#

Main code
#Imports & Initial
import pygame, os, sys, time
pygame.init()

x = 200
y = 200
Main problem
def enemy_movement():
global x, y
pygame.draw.rect(screen, (255,0,0), (x, y, 50, 50))
time.sleep(100)
pygame.draw.rect(screen, (0,0,0),
(x, y, 50, 50))
x += 1
y += 1
#Map
def map_():
pygame.draw.rect(screen, (255,255,255),
(0, (pygame.Surface.get_height(screen) / 2), 400, 50))
pygame.draw.rect(screen, (255,255,255),
(399, ((pygame.Surface.get_height(screen) / 2) - 150),
50, 200))
pygame.draw.rect(screen, (255,255,255),
(399.5, ((pygame.Surface.get_height(screen) / 2) -150),
400, 50))
pygame.draw.rect(screen, (255,255,255),
(799.5, ((pygame.Surface.get_height(screen) / 2) - 150),
50, 300))
pygame.draw.rect(screen, (255,255,255),
(799.5, ((pygame.Surface.get_height(screen) / 2) + 150),
740, 50))
pygame.display.update()
#spawning of tower
def tower(event):
if event.type == pygame.KEYUP and event.key == pygame.K_q:
mouse_pos = pygame.mouse.get_pos()
tower_x = mouse_pos[0]
tower_y = mouse_pos[1]
pygame.draw.rect(screen, (255,100,100), (tower_x, tower_y, 50, 50))
FPS = 60
#Creation of window
os.environ['SDL_VIDEO_CENTERED'] = '1' #called before pygame.init()
info = pygame.display.Info() #called before set_mode()
screen_width, screen_height = info.current_w, info.current_h

screen = pygame.display.set_mode((screen_width - 10, screen_height - 50))
#main loop
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
enemy_movement()
#map_()
#tower(event)
My code is not running, idk if its my device or code, its that the pygame window is freezing (eg closing the window takes very long)

strange lionBOT
#

@warm mountain

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

fierce gyro
#
time.sleep(100)
fierce gyro
#

That is telling the OS to ignore your program for 100 seconds, which will cause it to freeze.

fierce gyro
#

When you're creating a UI, you can't use sleep. UI libraries typically supply their own "run this code in the future" functionality.

#

Tkinter has an after method, but I don't know what Pygame's version is.

warm mountain
#

what the diff of delay and wait?

#

@fierce gyro

fierce gyro
fierce gyro
warm mountain
#

oh

#

now i see it

#

ill test it rq

#

@fierce gyro code still freezing

#

def enemy_movement():
global x, y
pygame.draw.rect(screen, (255,0,0), (x, y, 50, 50))
pygame.time.delay(10000)
pygame.draw.rect(screen, (0,0,0),
(x, y, 50, 50))
x += 1
y += 1

strange lionBOT
#

Hey @warm mountain!

It looks like you're trying to paste code into this channel.

Discord has support for Markdown, which allows you to post code with full syntax highlighting. Please use these whenever you paste code, as this helps improve the legibility and makes it easier for us to help you.

To do this, use the following method:
```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
You can **edit your original message** to correct your code block.
fierce gyro
#

The best I can do is link to docs and SO since I've never use Pygame like I mentioned. I just know time.sleep can't be used in applications that use an event loop.

warm mountain
#

ah i c

strange lionBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.