My problem is the menu, It shows "Press space to play!"
But when i press it, it doesn't work!
I think its a problem with the while loops
I can't display code correctly so here's a link to my code
https://github.com/Donutoftime44/nheon-shooter
I tried putting if statements in my code like
while not done:
if menu:
...
elif not menu:
...
But it still does'nt work!
Can someone help me?
You have
while menu:
for event in pg.event.get():
if event.type == QUIT:
pg.quit()
sys.exit()
if event.type == KEYUP:
if event.key == K_SPACE:
print "playing"
done = True
Because you never change menu the loop never breaks, I believe you may want to add menu = False in if event.key == K_SPACE:
Assuming you're talking about this portion of the code:
while menu:
for event in pg.event.get():
if event.type == QUIT:
pg.quit()
sys.exit()
if event.type == KEYUP:
if event.key == K_SPACE:
print "playing"
done = True
windowdisplay.fill(colors["gray"])
windowdisplay.blit(label, ((maxx / 2) - 100, maxy - 25))
pg.display.update()
It seems like you want to end the loop when the user presses space. In which case, you need:
if event.key == K_SPACE:
menu = False
Setting done equal to True does nothing, since done doesn't appear anywhere else in your code.
Related
I have a problem with my pygame code, which seems correct but doesn't work as it should.
I want to do different actions depending the key pressed; when I put two cases, only one works.
I put my code below :
gamestate.draw(window)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.type == pygame.K_ESCAPE):
finis = True
if (event.type == pygame.KEYDOWN and event.key == pygame.K_UP):
print("ok")
pygame.display.update()
Here, my code print "ok" in the terminal when I press the UP key, but doesn't quit when I keep the escape key pressed.
It isn't this particular case that doesn't work, before it quit when i pressed escape and didn't print "ok" when i pressed the up key.
Do you have any idea to solve my issue ? Thanks a lot !
pygame.K_ESCAPE is not an event type but you check for event.type:
... and event.type == pygame.K_ESCAPE
It should be:
... and event.key == pygame.K_ESCAPE
Also usually if you have to check for multiple keys on pygame.KEYDOWN event, then use nested ifs:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
finis = True
if event.key == pygame.K_UP:
print('ok')
Also you need to call pygame.display.update only ONCE in the loop (most likely at the end) NOT twice
I apologize for the vague title, but essentially I want to exit a game loop in several different ways using pygame.
Given the following code, I want to avoid writing "running = False" twice. A very minor thing I know, but I would like to write both "if" conditions in less lines with "and" and "or", if possible.
running = True
while running:
for e in pg.event.get():
if e.type == pg.QUIT:
running = False
if e.type == pg.KEYDOWN:
if e.key == pg.K_ESCAPE:
running = False
pg.quit()
This is my first time asking a question, so please excuse me if this is too vague or just a dumb question. Thanks anyway in advance!
If the goal it to exit on either of those cases this would work:
running = True
while running:
for e in pg.event.get():
if e.type == pg.QUIT or (e.type == pg.KEYDOWN and e.key == pg.K_ESCAPE):
running = False
pg.quit()
You could shorten it even more using:
while True:
for e in pg.event.get():
if e.type == pg.QUIT or (e.type == pg.KEYDOWN and e.key == pg.K_ESCAPE):
break
pg.quit()
Use some good old Boolean algebra, with parens for grouping!
I think this does the exact same thing, but you might want to double-check:
while True:
for e in pg.event.get():
if e.type == pg.QUIT or (e.type == pg.KEYDOWN and e.key == pg.K_ESCAPE):
pg.quit()
I'm trying to make a game on python, my problem is that I can't get the game to read inputs while running. (Not even the Escape key to exit).
I can't find the problem, I thought it was the lack of the tick method but I just added it and it still doesn't work.
Any help is appreciated.
Thanks.
Here's the code:
Edit Now just the game loop
astronut=Player()
astronut.paintplayer()
while True:
screen.fill(WHITE)
for event in pygame.event.get():
if event.type == QUIT:
terminate()
if event.type == KEYDOWN:
if event.type == K_ESCAPE:
terminate()
if event.type == K_UP:
astronut.acceleration +=1
astronut.speed = astronut.speed + astronut.acceleration
astronut.posY= astronut.posY + astronut.speed
elif astronut.posY<(WINDOWHEIGHT-OVERTHELINE-playerHeight):
astronut.acceleration -=1
astronut.speed = astronut.speed + astronut.acceleration
astronut.posY= astronut.posY + astronut.speed
astronut.paintplayer()
pygame.display.update()
mainClock.tick(FPS)
I think you need to check the event.key instead of event.type:
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
terminate()
...
I am making this multiple choice program, but I need the mouse event to only work after enter is pressed.
Here is my code:
for event in pygame.event.get(): # If user did something
if event.type == pygame.QUIT: # If user clicked close
done = True
elif event.type == pygame.KEYDOWN: # If user pressed a key
if event.key == pygame.K_RETURN: # If user pressed enter
# Makes the start screen go away
enter_pressed = True
# Increments question_number
question_number += 1
# Where I Draw the question screen
Then down below I have this:
for event in pygame.event.get(): # If user did something
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
print("Derp")
Derp won't print when I press left mouse button.
However, when I have it indented like this:
for event in pygame.event.get(): # If user did something
if event.type == pygame.QUIT: # If user clicked close
done = True
elif event.type == pygame.KEYDOWN: # If user pressed a key
if event.key == pygame.K_RETURN: # If user pressed enter
# Makes the start screen go away
enter_pressed = True
# Increments question_number
question_number += 1
# Where I Draw the question screen
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
print("Derp")
Derp does print when I press left mouse button
You could use a boolean indicating wheter or not enter was pressed.
for event in pygame.event.get(): # If user did something
enter_pressed = False
if event.type == pygame.QUIT: # If user clicked close
done = True
elif event.type == pygame.KEYDOWN: # If user pressed a key
if event.key == pygame.K_RETURN: # If user pressed enter
# Makes the start screen go away
enter_pressed = True
# Increments question_number
question_number += 1
# Where I Draw the question screen
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1 and enter_pressed:
print("Derp")
I think that the problem is that you are iterating over all events, and inside that loop you are iterating over all events (again), and that causes some problems
It is hard to tell from what you said, but I have a couple of recommendations for you. First, make sure that the for loop with derp isn't in any if statements you don't want it to be in. Second, make sure you call pygame.event.get() once per game loop or the code will only give you events that happened between the two calls, which will not be all of them. If neither of these things work, try posting the whole code.
Here is some pygame code it works fine except to move the sprite i have to repeatedly tap the arrow keys, is there a way to make the sprite move by holding down the arrow keys? Below is my code:
while True: #main game loop
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYUP:
if event.key == K_RIGHT:
LionCubX+= 10
elif event.key == K_LEFT:
LionCubX-= 10
elif event.key == K_UP:
LionCubY-= 10
elif event.key == K_DOWN:
LionCubY+= 10
DISPLAYSURF.fill(GREEN)
DISPLAYSURF.blit(LionCubImg,(LionCubX,LionCubY))
pygame.display.update()
The problem that I can see here is that you check for events of type KEYUP. This means that your Code only gets executed when you release the key. So change
elif event.type == KEYUP:
to
elif event.type == KEYDOWN:
Another possibility would be that you didn't set key repeat:
pygame.key.set_repeat(1, 30)
Put this before your main game loop to activate key repeat. Also see the docs.
The problem is you're moving your sprite only if a key has been depressed and released KEYUP. You need to move the sprite in the key state KEYDOWN.
Change this:
elif event.type == KEYUP:
To:
elif event.type == KEYDOWN: