This question already has answers here:
Why does the IPython REPL tell me "SyntaxError: unexpected EOF while parsing" as I input the code?
(9 answers)
Closed 1 year ago.
I am trying to create a game from a book by Eric Matthes and I am getting a error for some reason I am getting the syntax error before this it was a indenation error. Let me know if you guys can help me in this, thanks.
import sys
import pygame
def check_events(ship):
"""Respond to keypresses and mouse events."""
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
# Move the ship to the right.
I ran your code, the only error I got was with the comment on the last line. therefore, it looks like the comment inside the "if" statement is the problem. To solve it, I replaced this with something else (I just used "pass" for testing) and that worked. Hope this helps!
import sys
import pygame
def check_events(ship):
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
pass
Related
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 ()
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.
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.
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()