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

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.

Related

When R is pressed. Call a function that resets all variables to default [duplicate]

This question already has answers here:
Pygame level/menu states
(2 answers)
Reset and restart pygame program doesn't work
(1 answer)
Closed 2 days ago.
I can't make the restart button
please help me, please, I want to make a reset button in my project, but I do not know how, I want, provided that when I press r, all these games are reset, that is, if I have a record of 1500 and press r so that the record becomes 0 again, the project tetris works with pygame, if I need the code, I can throw it off when R is pressed. Call a function that resets all variables to their default values.
You can detect when 'r' is pressed using keyboard events. Here is a small example:
import pygame
gameDisplay = pygame.display.set_mode((800, 600))
def main():
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_r:
print('r pressed!')
# Reset variables here or call a function to reset them
gameDisplay.fill((255, 255, 255))
pygame.display.update()
main()

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

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.

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

Pygame keeps crashing on mac [duplicate]

This question already has answers here:
Why is my PyGame application not running at all?
(2 answers)
Closed 2 years ago.
I started learning pygame, wrote a simple program to display some text on the screen.
import pygame, time
pygame.init()
window = pygame.display.set_mode((600,300))
myfont = pygame.font.SysFont("Arial", 60)
label = myfont.render("Hello Pygame!", 1, (255, 255, 0))
window.blit(label, (100, 100))
pygame.display.update()
time.sleep(15)
pygame.quit()
But it keeps crashing.
I am using python2.7
The issue is that you are running the code only once and not repeating the lines of code that need to be repeated for every frame.
Then you are calling pygame.quit() without exiting the Python thread with quit() which results in the windows just "crashing" or not responding.
To fix this problem:
Include some code inside a while loop that will run on every frame and thus keep the program running and responding.
Make sure that initialization code is only ran once.
Add in some event handling to let the user exit the program when the "X" button is clicked.
Some useful additions:
Included a Clock which allows for an FPS-cap.
Filled the screen with black every frame
Exited the game properly with pygame.quit() to exit the pygame window and sys.exit() to exit the Python thread.
A Clock in pygame game allows you to specify an FPS. At the end of every main game loop iteration (frame) you call clock.tick(FPS) to wait the amount of time that will ensure the game is running at the specified framerate.
Here is the revised code example:
import pygame
import sys
# this code only needs to be ran once
pygame.init()
window = pygame.display.set_mode((600,300))
myfont = pygame.font.SysFont("Arial", 60)
label = myfont.render("Hello Pygame!", 1, (255, 255, 0))
clock = pygame.time.Clock()
FPS = 30
while True:
#allows user to exit the screen
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# this code should be ran every frame
window.fill((0, 0, 0))
window.blit(label, (100, 100))
pygame.display.update()
clock.tick(FPS)
I hope that this answer has helped you and if you have any further questions please feel free to post a comment below!

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