PYGAME, check for second press - python

So I want to change the variable from False to True on the first key press, and then on the second press (of the same key for e.g. "g") I want the variable to change back to "False"
Here is an example of not working code:
...
if event.type == pygame.KEYUP:
if event.key == pygame.K_g:
show_location=True
if event.type == pygame.KEYUP:
if event.key == pygame.K_g:
show_location=False
...
Can someone explain to me how to do it right?

Rather than hard-coding True or False in your assignment, just negate the current value:
if event.type == pygame.KEYUP:
if event.key == pygame.K_g:
show_location = not show_location

Related

Pygame : event.type works only for one case

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

Getting an error whenever I try to rerun the gameloop

while gameStart == True:
startScreen()
for event in pygame.event.get():
if event.type == pygame.QUIT:
youLose = True
gameStart = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
moveY = -snek.fast
moveX = 0
gameStart = False
if event.key == pygame.K_ESCAPE:
youLose = True
gameStart = False
I have no trouble when I run the game the first time around. I'm trying to get the game to reset after I've lost but I keep getting the error:
if event.type == pygame.KEYDOWN:
UnboundLocalError: local variable 'event' referenced before assignment
I don't understand why this is giving me an error, and even less why it gives an error only on the second time around. I've tried indenting the block so as to include it inside the for loop, but it makes my game crash.
The keydown if-else statement should be inside the for-loop
while gameStart == True:
startScreen()
for event in pygame.event.get():
if event.type == pygame.QUIT:
youLose = True
gameStart = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
moveY = -snek.fast
moveX = 0
gameStart = False
if event.key == pygame.K_ESCAPE:
youLose = True
gameStart = False

How to correct Syntax error with my elif, if statement. what am I doing wrong?

import sys
import pygame
def check_events(ship):
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
# Move the ship to the right.
ship.moving_right = True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
ship.moving_right = False
def update_screen(ai_settings, screen, ship):
'''Update images on the screen and flip to the new screen.'''
Error:
File "/home/mark/python_work/game_functions.py", line 8
elif event.type == pygame.KEYDOWN:
^
SyntaxError: invalid syntax
[Finished in 0.2s with exit code 1]
In your function, your first condition should start with an if.
def check_events(ship):
if event.type == pygame.KEYDOWN: # <--- note the change here
if event.key == pygame.K_RIGHT:
# Move the ship to the right.
ship.moving_right = True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
ship.moving_right = False
You have an elif without an if preceding it. Just change the first elif to an if.
You cannot use elif before an if statement. Elif cannot be used without an if statement.
This is the right syntax.
import sys
import pygame
def check_events(ship):
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
# Move the ship to the right.
ship.moving_right = True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
ship.moving_right = False
def update_screen(ai_settings, screen, ship):
'''Update images on the screen and flip to the new screen.'''

How to move an object when a key is held down?

I know this question can be seen as a duplicate, but I spent some hours searching and figuring out what is wrong at my code.
My problem is that my object, called player, doesn't move constantly when left or right key is being held down:
for event in pygame.event.get():
if event.type == QUIT:
self.terminate()
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
self.terminate()
if event.key == K_LEFT or event.key == K_a:
self.moveRight = False
self.moveLeft = True
if event.key == K_RIGHT or event.key == K_d:
self.moveLeft = False
self.moveRight = True
if event.type == KEYUP:
if event.key == K_LEFT or event.key == K_a:
self.moveLeft = False
if event.key == K_RIGHT or event.key == K_d:
self.moveRight = False
# Move the player around
if self.moveLeft :
# Moves the player object to left with self.PLAYERMOVERATE pixels.
self.player.setLeftRight(-1 * self.PLAYERMOVERATE)
if self.moveRight :
self.player.setLeftRight(self.PLAYERMOVERATE)
I also tried this alternative:
while True:
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
self.player.setLeftRight(-1 * self.PLAYERMOVERATE)
if keys[pygame.K_RIGHT]:
self.player.setLeftRight(self.PLAYERMOVERATE)
I think that the problem is that you are not handling the input in the main game loop.
In your code you seem to be handling the input inside a method of the object Player. This is not how input should be handled. In your second code example there is a while True: loop which will mean the loop is never exited from and thus the method's execution is never finished. I suspect that there may be a similar issue in your first example.
Instead you should:
Create all objects and classes.
Write the main game loop.
The main game loop should handle input, then process the logic of the game and then render whatever should be rendered.
Here is a short code example.
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit() # Exit from pygame window
quit() # End python thread
if event.type == KEYDOWN:
if event.key == K_LEFT or event.key == K_a:
player.moveRight = False
player.moveLeft = True
if event.key == K_RIGHT or event.key == K_d:
player.moveLeft = False
player.moveRight = True
if event.type == KEYUP:
if event.key == K_LEFT or event.key == K_a:
player.moveLeft = False
if event.key == K_RIGHT or event.key == K_d:
player.moveRight = False
# Move player using method
if player.moveLeft:
# Move player
# ...
# Render player
I hope that this helped you and if you have any further questions please feel free to post them in the comment section below!

Sprite lagging in pygame

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:

Categories