Quit from program in pygame [duplicate] - python

This question already has answers here:
Pygame unresponsive display
(1 answer)
Pygame window not responding after a few seconds
(3 answers)
Closed 2 years ago.
I am trying to understand why my program is loosing respond when I trying exit from game. I make some test and here is code:
import pygame
import time
pygame.init()
run=True
pygame.display.set_caption("test")
win=pygame.display.set_mode((200,200))
while run==True:
pygame.time.delay(16)
keys=pygame.key.get_pressed()
#for event in pygame.event.get():
#if event.type == pygame.QUIT:
#run=False
win.fill((125,125,125))
if keys[pygame.K_q]:
run=False
pygame.display.update()
pygame.quit()
If execute this code - windows can't properly exit from program when user pressed key "q".
If remove comments symbols # form 14,15,16 strings, all will work properly. Hitting "q" key will exit from program normally.
Only one question - why???

You have to handle the events in the application loop. See pygame.event.get() respectively 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 don't handle the events, pygame.key.get_pressed() will stop working. The states of the keys returned by pygame.key.get_pressed() are evaluated internally when the events are handled.
At least you've to call pygame.event.pump():
while run==True:
pygame.time.delay(16)
pygame.event.pump()
keys=pygame.key.get_pressed()
if keys[pygame.K_q]:
run=False
win.fill((125,125,125))
pygame.display.update()

Related

Why do key presses sometimes work and sometimes not? [duplicate]

This question already has answers here:
How to get keyboard input in pygame?
(11 answers)
Faster version of 'pygame.event.get()'. Why are events being missed and why are the events delayed?
(1 answer)
Closed last month.
I have some code to check if "q" is pressed and then I make an instance of a class. The problem is that sometimes it recognizes a press of the button and sometimes it doesn't. For example if I press "q" 10 times, sometimes it will recognize the press 3 times, sometimes 6 times, sometimes 7 times. So how can I fix it so it will recognize the press every single time? Some people said that it's probably because I call pygame.event.get() multiple times. Because of that I have tried getting events once in the whole script, but it's still the same result.
Code:
pygame.init()
fpsClock = pygame.time.Clock()
screen = pygame.display.set_mode((1920, 1080)
while True:
#Input
self.events = self.pygame.event.get()
for event in self.events:
if event.type == self.QUIT:
self.pygame.quit()
self.sys.exit()
if event.type == self.pygame.KEYDOWN:
if event.key == self.pygame.K_q:
#makes an instance
pygame.display.flip()
fpsClock.tick(60)

Pygame background not loading [duplicate]

This question already has answers here:
Pygame window not responding after a few seconds
(3 answers)
Why is nothing drawn in PyGame at all?
(2 answers)
Closed 2 years ago.
MacOSX 10.15.2, Python 3.8 and Pygame 2.0.0.
Hello! I am currently trying to make a dark purple (87,61,122) background in Pygame, but it only appears as a black background, seemingly frozen and loading forever.
Here is my code:
import pygame
bgcolor = [87,61,122]
pygame.init()
screen = pygame.display.set_mode((1600,900))
screen.fill(bgcolor)
pygame.display.flip
Is there anything wrong with the code or is Pygame just refusing to co-operate?
pygame.display.flip is not a function call. You missed the parentheses.
However, you have to handle the events in the application loop. See pygame.event.get() respectively 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. This will update the contents of the entire display.
The typical PyGame application loop has to:
handle the events by either pygame.event.pump() or pygame.event.get().
update the game states and positions of objects dependent on the input events and time (respectively frames)
clear the entire display or draw the background
draw the entire scene (draw all the objects)
update the display by either pygame.display.update() or pygame.display.flip()
import pygame
bgcolor = [87,61,122]
pygame.init()
screen = pygame.display.set_mode((1600,900))
clock = pygame.time.Clock()
run = True
while run:
clock.tick(60)
# handle events
for event in pygame.event.get():
if event.type == QUIT:
run = False
# clear display
screen.fill(bgcolor)
# draw scene
# [...]
# update display
pygame.display.flip()
pygame.quit()
exit()
change pygame.display.flip by pygame.display.flip()
or pygame.display.update()
not that without IDLE, this script will display a window and directly destroy it.
To add like loop:
import pygame
bgcolor = [87,61,122]
pygame.init()
screen = pygame.display.set_mode((1600,900))
loop = True
while loop:
for event in pygame.event.get():
if event.type == pygame.QUIT:
loop = False #Quiting
screen.fill(bgcolor)
pygame.display.update()

Why does my game crash once while loop is run (Pygame) [duplicate]

This question already has answers here:
Pygame unresponsive display
(1 answer)
Pygame window not responding after a few seconds
(3 answers)
Closed 2 years ago.
my code:
def shop_button(self):
click = pygame.mouse.get_pressed()
if click[0] == 1:
self.open_shop() # draws shop
self.inshop = True
while self.inshop:
pygame.time.delay(10)
mousepos = pygame.mouse.get_pos()
if click[0] == 1 and b.mouse_on_button(mousepos[0], mousepos[1], b.escapex, b.escapex2, b.escapey, b.escapey2):
self.inshop = False
whenever you click on the shop button this function is run, however, while self.inshop is inside an if statement like such the game instantly stops responding and crashes. This could be a very simple solution as I am semi-new to pygame and the python language as a whole. However, with that being said I can't seem to find a solution anywhere else.
The game does not crash, it does not respond, because you have a loop inside the main application loop. The inner loop has no event handling and update of the display.
def shop_button(self):
# [...]
while self.inshop:
pygame.time.delay(10)
mousepos = pygame.mouse.get_pos()
# [...]
The position which is returned by pygame.mouse.get_pos() is read from an internal cache. The cache is updated when the events are handled by either pygame.event.pump() or pygame.event.get().
That means if you retrieve the position in a loop, without handling the events, the the position does not reflect the current mouse position, it is continuously the same position, the position at the point when pygame.event.get() called, before the loop.
Furthermore the display Surface won't change, without updating the display, by either pygame.display.flip() or pygame.display.update().
You have a main application loop, use it. The main application loop has to:
handle the events by either pygame.event.pump() or pygame.event.get().
update the game states and positions of objects dependent on the input events and time (respectively frames)
clear the entire display or draw the background
draw the entire scene (blit all the objects)
update the display by either pygame.display.update() or pygame.display.flip()
Never implemented nested application loops. You have to change the control flow and the design of your applcaition.

pygame window keeps not responding

After a long time trying to install pygame for 2.7 it finally installs and I now have it downloaded, but there is now the problem that it keeps not responding after a couple seconds of being open. Any answer will be appreciated, the code that I have so far is just.
import pygame
pygame.init()
pygame.display.set_mode((640,480))
so I need some help please.
So what you want to do, like what skrx said, is a while loop to continuously keep the code inside the while loop and the pygame window running, and as well as a for event loop, in order to be able to shut down the window. Here's how you can do it:
import pygame
pygame.init()
pygame.display.set_mode((640, 480)) # opens the display
while True: # the while loop that will keep your display up and running!
for event in pygame.event.get(): # the for event loop, keeping track of events,
if event.type == pygame.QUIT: # and in this case, it will be keeping track of pygame.QUIT, which is the X or the top right
pygame.quit() # stops pygame
There are other ways of stopping the while loop, and you could do this:
running = True
while running: # the while loop that will keep your display up and running!
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
Hope this helps!

How should I properly program an immediate exit procedure in pygame? [duplicate]

This question already has answers here:
How to wait some time in pygame?
(3 answers)
Move an object every few seconds in Pygame
(2 answers)
Closed 1 year ago.
I have this piece of code in the end of my main function in a python pygame script. This seems to be the correct way to handle an exit with a simple keypush.
running = True
while running == True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
break
elif event.type == pygame.KEYDOWN:
running = False
break
if running == False:
break
draw(screen, background)
pygame.quit() # quits pygame
sys.exit()
However in this draw function I also have the following line right before the closing accolade:
pygame.time.delay(randint(2,5) * 1000)
This line of code makes the game wait anywhere from 2 till 5 seconds. Apparently this delay code creates an extra delay of about 3 loops in detecting the key pushes.
My question is: how do I properly execute an immediate exit with a key push without waiting for the delay?
Edit: Solution (credit to HÃ¥ken Lid)
animate_event = pygame.USEREVENT
pygame.time.set_timer(animate_event, 1000)
while True:
if pygame.event.get(pygame.QUIT): break
if pygame.event.get(pygame.KEYDOWN): break
for event in pygame.event.get():
if event.type == animate_event:
animate(screen, background)
# reset event
pygame.time.set_timer(animate_event, randint(2,5)*1000)
pygame.quit()
sys.exit()

Categories

Resources