Why an infinite "while True:" loop freeze the window and other that look similar not? [duplicate] - python

This question already has answers here:
Pygame window not responding after a few seconds
(3 answers)
Pygame unresponsive display
(1 answer)
How does the for event in pygame.event.get() in python work?
(1 answer)
Closed 3 months ago.
So, a while true loop that only passes, freezes the window. But a while true loop that checks the pool of events for a Quit event won't freeze. Why is that? checking the events take enough time to prevent the program from overloading? What is going on inside the hood?
Loop that freezes the Pygame window:
import pygame
pygame.init()
screen = pygame.display.set_mode((800, 600))
while True:
pass
Loop that won't freeze the Pygame window:
import pygame
pygame.init()
screen = pygame.display.set_mode((800, 600))
running = True
while running:
for e in pygame.event.get():
if e.type == pygame.QUIT:
running = False
So if both are running infinitely (The second one virtually, supposed that the QUIT event is never called), why the first one consumes so many resources that it overloads it an freezes the window, but the second one "behaves" and doesn't overload it, even if the exit condition of the loop is not called? Is there some waiting time in the pygame.event.get() function?
I tried looking for info on Google but I got lost in subprocesses, etc.

Related

Pygame screen crashes misterioslly, I don't idea Why [duplicate]

This question already has an answer here:
Pygame unresponsive display
(1 answer)
Closed 2 years ago.
I Was try to build a game and I was build the interfaces normally, but sundely pygame screen crashed and the pygame screen window becomes unresponsive always I run the a simple program.
import pygame
pygame.init()
pygame.display.init()
pygame.display.set_caption("Yathezz")
tela = pygame.display.set_mode((600,600))
preto = (0,0,0)
def set_telaPartida(tela,nome__jogador,pontos_totais, situacao):
tela.fill(preto)
#if(situacao == 1):
# set_telaLancaDados(tela,nome__jogador,pontos_totais)
# elif(situacao ==2):
# a = 1
# seta tela perguntando o relançamento
#elif(situacao == 3):
# b= 2
#set_telaRelancaDados
while True:
set_telaPartida(tela,"Lucas",100,1)
#set_telaInst2(tela)
pygame.display.update()
I have done some tests and I suspect that the problem are in this line
pygame.display.update()
But I don't have Idea what's happening. Could someone Help me???
You have to handle the events, by either pygame.event.pump() or pygame.event.get(), to keep the window responding. For instance:
run = Ture
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
set_telaPartida(tela,"Lucas",100,1)
#set_telaInst2(tela)
pygame.display.update()
See the documentation of pygame.event.pump():
For each frame of your game, you will need to make some sort of call to the event queue. This ensures your program can internally interact with the rest of the operating system. If you are not using other event functions in your game, you should call pygame.event.pump() to allow pygame to handle internal actions.
pygame.event.get() calls pygame.event.pump(). The pygame.QUIT occurs when the close button is pressed.

Problem launching pygame on macos find nothing on the internet [duplicate]

This question already has answers here:
Why is my PyGame application not running at all?
(2 answers)
Why is nothing drawn in PyGame at all?
(2 answers)
Closed 2 years ago.
i have no problem of syntax or any erorr its just a problems when i run the program
import pygame
pygame.init()
# generate window
pygame.display.set_caption("shooter Game")
pygame.display.set_mode((1080, 720))
running = True
while running:
# if the player close the window
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
print("game closed")
sceenshot of the code and the 🚀 of python that not stoping to jump
You don't update the display, which you have to do with
pygame.display.update()
You can add it right after pygame.display.set_mode((1080, 720))
Try like this and tell me if it works :)

Why does it say exit status 1? [duplicate]

This question already has answers here:
Pygame window freezes when it opens
(1 answer)
Pygame window not responding after a few seconds
(3 answers)
Closed 2 years ago.
I want the code to work. So that I can get a good grade of it. I have tried a lot of things so that the code can work event.
import pygame.sys
from pygame.locals import *
pygame.init()
DISPLAY=pygame.display.set_mode((500,400),0,32)
pygame.disply .set_caption(AMBERMIR)
WHITE=(255,255,255)
BLUE=(0,0,255)
DISPLAY.fill(WHITE)
pygame.draw.rect(DISPLAY,BLUE,(200,150,100,50))
if event.type == QUIT:
pygame.quit()
sys.exit()
You've to implement a main loop which is continuously running. Inside the main loop, an event loop can be handle the events.
Use pygame.event.get() to get and remove all pending events from the loop. The return value of pygame.event.get() is a list of pygame.event.Event objects.
After drawing the scene, the window has to be updated by either pygame.display.update() or pygame.display.flip():
import sys
import pygame
from pygame.locals import *
pygame.init()
AMBERMIR = "my window"
DISPLAY=pygame.display.set_mode((500,400),0,32)
pygame.display.set_caption(AMBERMIR)
WHITE=(255,255,255)
BLUE=(0,0,255)
# main loop
run = True
while run:
# event loop
for event in pygame.event.get():
if event.type == QUIT:
run = False # terminate main loop on QUIT
# clear display
DISPLAY.fill(WHITE)
# draw rectangle
pygame.draw.rect(DISPLAY,BLUE,(200,150,100,50))
# update display
pygame.display.update()
pygame.quit()

"Sound of sorting" [duplicate]

This question already has answers here:
pygame.event.get() not returning any events when inside a thread
(1 answer)
Is it possible to make the keyboard module work with pygame and threading
(1 answer)
Closed 1 year ago.
I was writing a small program in python 2.7.10 with pygame. Im using multithreading to play sounds (with winsound) and still draw things fast. I was trying to replicate a similar idea of some videos i saw on youtube of sorting algorithms. Unfortunately it runs for about 10 seconds then randomly crashes, the sounds keep going but the pygame window goes not responding and the drawing does not change. Even if i put a print "hi" in the code somewhere it will still be printing in the console but pygame will still have gone not responding. Cant think of whats wrong but it may be something with the multithreading (im very new to it!)
from random import randint
import pygame,winsound,threading,time
from pygame.locals import *
screen=pygame.display.set_mode([1000,500])
def bubbleSort(listName):
sorting=True
while sorting:
listChanged=False
for i in range(len(listName)-1):
draw(listName,1000/len(listName),500/11,i)
if listName[i] < listName[i+1]:
listName[i],listName[i+1]=listName[i+1],listName[i]
threading.Thread(target=playsound,args=([listName[i]])).start()
listChanged=True
if not listChanged:
sorting=False
time.sleep(10)
pygame.quit()
def playsound(frequency):
winsound.Beep(frequency*100,100)
def draw(listName,width,height,comparing):
print "hi"
screen.fill([0,0,0])
for i in range(len(listName)):
if i == comparing or i == comparing+1:
pygame.draw.rect(screen,[0,255,0],[width*(len(listName)-i),500-(height*listName[i]),width,500])
else:
pygame.draw.rect(screen,[255,255,255],[width*(len(listName)-i),500-(height*listName[i]),width,500])
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
pygame.display.update()
list1=[]
for i in range(100):
list1.append(randint(1,10))
threading.Thread(target=bubbleSort,args=([list1])).start()

Why does pygame freeze at pygame.event.get() when you move/drag the window? [duplicate]

This question already has answers here:
Pygame window not responding after a few seconds
(3 answers)
Pygame unresponsive display
(1 answer)
Closed 1 year ago.
Basically I have a loop (tick, set_caption, screen_fill, event.get(), send_frame_event, flip, repeat)
When I drag the window around on windows 7, the loop stops looping, I ended up stuck in pygame.event.get(), I have tried to define certain events only for get e.g. get([pygame.QUIT]) to no avail.
Simply calling pygame.event.clear() has the same freeze effect when dragging/moving the window.
Is there a workaround?
Not full code, but should be enough:
def start(self):
self.running = True
Clock = pygame.time.Clock()
while self.running:
self.p += 25
tickFPS = Clock.tick(self.fps)
pygame.display.set_caption("Press Esc to quit. FPS: %.2f" % (Clock.get_fps()))
self.screen.fill([self.p&0xFF,(255-self.p)&0xFF,255])
self.handleEvents()
self.raiseEvent("updateFrame")
pygame.display.flip()
def handleEvents(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
self.running = False
full code at: http://pastie.org/private/wm5vqq3f7xe0xlffy1fq
Try placing a call to pygame.event.pump() inside your mainloop (or handleEvents function)
No idea if this will help or not:
I realize the problem is with moving the window window, not with re-sizing the window.
Perhaps there is some similarity between moving and re-sizing?
I found this in the documentation on re-sizing:
Then the display mode is set, several events are placed on the pygame event queue. pygame.QUIT is sent when the user has requested the program to shutdown. The window will receive pygame.ACTIVEEVENT events as the display gains and loses input focus. If the display is set with the pygame.RESIZABLE flag, pygame.VIDEORESIZE events will be sent when the user adjusts the window dimensions. Hardware displays that draw direct to the screen will get pygame.VIDEOEXPOSE events when portions of the window must be redrawn.
and:
Note that when the user resizes the game window, pygame does not automatically update its internal screen surface. You must call set_mode() every time VIDEORESIZE is sent. This really should be more clear in the documentation.
Perhaps when moving the game window, something similar happens?

Categories