Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 days ago.
Improve this question
I am experiencing the following error code:
elif event.type == pygame.KEYDOWN and not active:
^ SyntaxError: invalid syntax
This is my code:
active = False
for entry in self.textEntries:
if entry.clicked:
active = True
#Event loop. In-game control is routed through here
#Will probably need something more robust soon.
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.quit()
elif event.type == pygame.KEYDOWN and not active:
if event.key == pygame.K_SPACE:
self.paused = not self.paused
elif event.key == pygame.K_g:
#toggle draw grid
self.options['draw_grid'] = not self.options['draw_grid']
I checked if the code was indented properly, but I couldn't find any problems.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 11 months ago.
Improve this question
I am using pygame to create something and I need to have a delay for about 10 seconds that is interrupted when a button is pushed. When I tried to do this, it paused on the delay and it was unable to detect for button pushes. I was wondering if this is even possible or if there are any alternatives. I have tried:
pygame.time.delay(10000)
if hit1: #hit1 is a bool representing keydown
Blue1() #my function to draw a shape
I happened to have come across the same problem 2 years ago (when I was 12) so I think I understand what you mean.
First import sleep from time
from time import sleep
Then you can try this
#code before 10 sec count
interrupt = false
millisecondPassed = 0
while true:
sleep(0.01)
millisecondPassed += 1
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_b:
interrupt = true
if millisecondPassed >= 10000 or interrupt == true:
break
#code after interruption
As for reference:
10 seconds = 10000 milliseconds
Originally after I did some searching here, I found a question with the exact same problem I had:
Pygame window not responding after a few seconds . I reviewed all the answers and tried them, but none of them worked. I tried using for loops to loop through every single event;
run = True
while run:
for event in pygame.event.get():
if event == pygame.QUIT()
run = False
But the window still closed. I also tried:
run = True
while run:
event = pygame.event.get()
if event == pygame.QUIT():
run = False
Which had the same results as the one above.
Can anyone help?
Edit: I use PyCharm and MacOS Catalina.
pygame.QUIT is a constante, but pygame.QUIT() is a call statement. Remove the braces. Anyway, the condition won't work, because you have to compare the type attribute of the event to the event type constant (see pygame.event). Furthermore the : is missing at the end of the if-statement.
if event == pygame.QUIT()
if event.type == pygame.QUIT:
Furthermore the Indentation is not correct:
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
import pygame
pygame.init()
#create the screen
screen = pygame.display.set_mode((800, 600))
running = True
while running:
for event pygame.event.get():
if event.type == pygame.QUIT:
running = False
you are missing an "in" in your for loop
import pygame
pygame.init()
pygame.display.set_mode((800, 600))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
also, check out the Stackoverflow guide in order to use proper font for your code, have a great day
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
import sys
import pygame
def run_game():
pygame.init()
screen = pygame.display.set_mode((1200, 800))
pygame.display.set_caption("Alien game")
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.display.flip()
run_game()
I’m starting playing with the pygame module and I want to display a window but when I run the program I face this problem pygame error: video system not initalized even I have put pygame.init() in a function and I call it later in the program, I have tried to put pygame.init() after the importing statements but I face another problem in pygame.display.flip(), the error say pygame.error: Display mode not set
it's due to indentations.
the main loop isn't in your run_game function, but in the main part of the code, because it miss an indent.
All of your while loop need 1 more indent.
Here you start your infinite loop before calling run_game, and so without initializing pygame.
Here is the correct code
import sys
import pygame
def run_game():
pygame.init()
screen = pygame.display.set_mode((1200, 800))
pygame.display.set_caption("Alien game")
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.display.flip()
run_game()
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I keep getting a syntax error in my code at elif not running: but I don't understand why. Everything seems to be formatted correctly, but yet I still get that syntax error.
running = True
def checker():
global running
if running:
if label.image == colorPhoto:
label.image = blackPhoto
label.configure(image = blackPhoto)
#GPIO.output(16,True)
#root.after(2000,checker)
elif label.image == blackPhoto:
label.image = colorPhoto
label.configure(image = colorPhoto
#GPIO.output(16,False)
#root.after(2000,checker)
elif not running:
label.image = colorPhoto
label.configure(image = colorPhoto)
#GPIO.output(16,False)
def press():
global running
if not running:
running = True
checker()
if running:
running = False
You miss a closing bracket at label.configure(image = colorPhoto