I have created a script using PYGAME to animate a pendulum motion simulation. I want to press a "q" key to stop the simulation and close the window. I tryed many different codes but all return some error. The lest version of the event part of the code was:
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == KEYDOWN:
if event.key == K_t:
is_tracing = not(is_tracing)
if event.key == K_c:
trace.fill(WHITE)
if event.key == K_q:
done = True
break
The "c" and "t" events are running fina and the "q" idead stops the simulation, but the simulation window frezes and I need to reestart the kernel do make it run again. Any idea of how to make it close the window without Killing the kernel?
I am runing the code at Jupter Notebook 6.1.4 from Anaconda Navigator 1.10 and Python 3.8.5
Any idea of how to make it close the window without Killing the kernel?
Try using pygame.quit(). From pygame FAQ
Make sure, you invoke pygame.quit() on exiting your application or game.
# ... running = True
while running:
event = pygame.event.wait ()
if event.type == pygame.QUIT:
running = False # Be IDLE friendly
pygame.quit ()
Related
Hi I am writing a simple chess program, unfortunately I have run in some unexpected problems, namely. After I added a list which keeps track of all the figures positions, I cannot close the window using the method which I till now used. which was:
for event in pygame.event.get():
# print(event)
# Checking if the user clicks the red quit cross
if event.type == pygame.QUIT:
# run control the mainloop, wheather True or not
run = False
After adding the list, this stopped working, so I use now:
for event in pygame.event.get():
# print(event)
if event.type == pygame.QUIT:
pygame.display.quit()
pygame.quit()
I tried to add some exception handling:
try:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.display.quit()
except pygame.error():
print('Program closed')
However the except statement is not reached and an error is printed: (pygame.error: video system not initialized)
Can You please tell me how to handle this exception properly, or suggest a different way of braking the mainloop.
remove the () from the exception catching, namely:
except pygame.error instead of except pygame.error()
I acctually figured it out on my own, so just in case someone is looking for the anwser. The issue was that I was trying to call the redraw function after pygame.display.quit(). Thus after moving both the redraw call and the keys = pygame.key.get_pressed() [wich also utilises the module pygame] the program terminates without any error call. The code should look as follows:
while run:
pygame.time.delay(100)
window_redrawing()
# Gathering user keyboard input
keys = pygame.key.get_pressed()
# making sure the window can be closed
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.display.quit()
run = False
fig_pos.clear()
So I have this code that looks after the user inputs for a pac-man style game.
def receiving_inputs(self):
while True:
events = pg.event.get()
for event in events:
if event.type == pg.KEYDOWN:
if event.key == pg.K_UP:
self.move = 'n'
elif event.key == pg.K_RIGHT:
self.move = 'e'
elif event.key == pg.K_DOWN:
self.move = 's'
elif event.key == pg.K_LEFT:
self.move = 'w'
time.sleep(1/60)
threading.Thread(target=self.receiving_inputs).start()
When I press any keys on my keyboard I do not get any events, however, moving the mouse around will return an event using this code.
The annoying thing is that this exact code works perfectly when not in a thread. i.e when in the program's main loop.
Just fyi I want to use a thread here to minimize the number of times pygame doesn't register a key press (which I'm assuming is due to other things in the mainloop).
Thanks in advance.
You don't get any events at all, because you have to get the events in the main thread.
See the documentation of pygame.event:
[...] The event subsystem should be called from the main thread.
It is only possible to post events from other thread, but the event queue has to be handled in the main thread.
I am creating a chat system and want to insert a game made with Pygame. In the state_machine file of the chat system, I set a state of the client as S_GAMING. When the client type "game" in the S_LOGGEDIN state, the state will be changed into S_GAMING and when the game is over, the user can quit the game either by clicking the "x" button or by waiting for 3 seconds.
I have some trouble closing the game window without exiting the chat system.
when I used pygame.quit(), the window will froze.
When I tried pygame.quit() and sys.exit() or quit(), I exited the chat system as well.
This is a part of the game.py file:
run = True
gameOver = False
timer = 0
while run:
clock.tick(27)
#check events
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if not gameOver:
drawWindow()
for each in barriers:
if each.isHit:
gameOver = True
else:
if timer < 81:
win.blit(game_over,(0,0))
timer +=1
pygame.display.update()
else:
run = False
pygame.display.quit()
pygame.quit()
This is a part of the chat_state_machine file:
elif self.state == S_GAMING:
import game
self.state = S_LOGGEDIN
I know this is a specific question and I can provide more code if the above is not clear.
Hopefully, I can get an answer. I am all ears.
Here is a simple solution: Once you exit pygame the code should throw an exception, in that case you can switch run to False and start a new loop
import pygame
pygame.init()
screen = pygame.display.set_mode((600, 400))
clock = pygame.time.Clock()
run = True
while run:
try:
screen.fill((0,0,0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
clock.tick(120)
pygame.display.update()
except:
run = False
while not run:
#Your other code here
pass
So I have this code that looks after the user inputs for a pac-man style game.
def receiving_inputs(self):
while True:
events = pg.event.get()
for event in events:
if event.type == pg.KEYDOWN:
if event.key == pg.K_UP:
self.move = 'n'
elif event.key == pg.K_RIGHT:
self.move = 'e'
elif event.key == pg.K_DOWN:
self.move = 's'
elif event.key == pg.K_LEFT:
self.move = 'w'
time.sleep(1/60)
threading.Thread(target=self.receiving_inputs).start()
When I press any keys on my keyboard I do not get any events, however, moving the mouse around will return an event using this code.
The annoying thing is that this exact code works perfectly when not in a thread. i.e when in the program's main loop.
Just fyi I want to use a thread here to minimize the number of times pygame doesn't register a key press (which I'm assuming is due to other things in the mainloop).
Thanks in advance.
You don't get any events at all, because you have to get the events in the main thread.
See the documentation of pygame.event:
[...] The event subsystem should be called from the main thread.
It is only possible to post events from other thread, but the event queue has to be handled in the main thread.
The game freezes instead of quitting. I have to exit from Idle. Any ideas on what to do?
def quit_game():
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
quit_game()
This is an IDLE bug. I would recommend using a real IDE such as pycharm. However to fix this issue, have a look at the pygame FAQ. They offer this solution:
# ... running = True
while running:
event = pygame.event.wait ()
if event.type == pygame.QUIT:
running = False # Be IDLE friendly
pygame.quit ()
It may work....
def quit_game():
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
quit_game()