for event in pygame.event.get():
if event.type == pygame.QUIT:
print('closing the window')
running = False
# key down for checking if a key is pressed.
if event.type == pygame.KEYDOWN:
print('some key is pressed')
if event.type == pygame.K_UP:
print("up key is pressed")
elif event.type == pygame.K_RIGHT:
print("Right key is pressed")
elif event.type == pygame.K_w:
print('w is pressed')
else:
print("does'nt recognize")
# key up for checking if a key is released.
if event.type == pygame.KEYUP:
print('some key is released')
if event.type == pygame.K_LEFT or event.type == pygame.K_RIGHT:
print('Keystroke is released')
I ran the code but this is the output:
some key is pressed
does'nt recognize
some key is released
some key is pressed
does'nt recognize
some key is released
closing the window
You are checking if event.type == pygame.K_RIGHT, however, you already know that event.type == pygame.KEYDOWN which is not equal to pygame.K_RIGHT. Instead, check event.key. This represents what key exactly is being pressed, while type just tells you that this is a keyboard event.
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 am trying to detect a single key press no matter how long the key is held down. Example, if the 'a' key is held down for 3 seconds, my current program would print this:
key pressed
'aaaaaaaaaaaaa'
key released
I am trying to get it to where it would only allow a single action per press/release cycle:
key pressed
'a'
key released
key pressed
'a'
key released
What am I doing wrong? Thank you
def update(self):
space_released = True
space_pressed = False
elif event.type == KEYUP:
if event.key == K_SPACE:
self.space_released = True
elif event.type == KEYDOWN:
if event.key == K_SPACE:
self.space_released = False
key = pygame.key.get_pressed()
while space_released == True:
# Print key pressed
return
space_released = False
Use pygame.key.set_repeat() to control how held keys are repeated:
When the keyboard repeat is enabled, keys that are held down will generate multiple pygame.KEYDOWN events. [...]
[...] To disable key repeat call this function with no arguments or with delay set to 0.
Just call pygame.key.set_repeat() to disable repeating keys:
pygame.key.set_repeat()
run = True
while run:
clock.tick(60)
space_released = False
space_pressed = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
elif event.type == KEYUP:
if event.key == K_SPACE:
space_released = True
elif event.type == KEYDOWN:
if event.key == K_SPACE:
space_pressed = True
# [...]
If you want to do something similar with pygame.key.get_pressed (), you have to save the state of pygame.K_SPACE and compare the previous (space_was_pressed) state with the current state (space_is_pressed):
space_was_pressed = 0
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
key = pygame.key.get_pressed()
space_released = True
space_pressed = False
space_is_pressed = key[pygame.K_SPACE]
if space_was_pressed != space_is_pressed:
space_released = not space_is_pressed
space_pressed = space_is_pressed
space_was_pressed = space_is_pressed
while running:
screen.fill((255,255,255))
screen.blit(background,(0,0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# keystroke controlling i am not getting this print message
if event.type == pygame.KEYDOWN:
if event.type == pygame.K_SPACE:
print("space is pressed ")
if event.type == pygame.KEYUP:
if event.type == pygame.K_SPACE:
print("released")
playerx += playerx_change
player(playerx , playery)
cactus(cactusx , cactusy)
pygame.display.update()
game()
This is because event.type cannot be KEYUP AND K_SPACE at the same time .
Rewrite the code as :
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
print("space is pressed ")
if event.type == pygame.KEYUP:
if event.key == pygame.K_SPACE:
print("released")
I want to change the text in the controls screen based on the key which the user presses.
how do I convert pygame.event.get() into a string that shows which key has been pressed?
preferably without many if staments
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
slectedKey = # get key name as a string
print(slectedKey)
The code of the pressed can be get by the event.key attribute.
The unicode representation for the key can be get by the event.unicode attribute.
See pygame.event module.
A unser friendly name of a key can be get by pygame.key.name():
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
print(pygame.key.name(event.key))
Note, if you want to evaluate if a certain key is pressed, the compare event.key to the constants defined in the pygame.key module:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
# [...]
elif event.key == pygame.K_RIGHT:
# [...]
Or store the key in a variable and use it continuously in the application loop:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
slectedKey = event.key
if slectedKey == pygame.K_UP:
# [...]
elif slectedKey == pygame.K_DOWN:
# [...]
If you want to evaluate if a key is is hold down, the use pygame.key.get_pressed():
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE]:
# [...]
elif keys[pygame.K_a]:
# [...]
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: