Pygame pressed key works temporaryly? [duplicate]
pressed = pygame.key.get_pressed()
is not an event. You have to call it the application loop not in the event loop.
If you want top detect when a key is pressed you need to use the KEYDOWN
event.
You need to draw the pages in the application loop. Add state indicating which page to display:
page = 'None'
run = True
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_1:
page = "1"
if event.key == pygame.K_2:
page = "2"
if event.key == pygame.K_3:
page = "3"
if event.key == pygame.K_4:
page = "4"
if page == "1":
first_page()
elif page == "2":
second_page()
elif page == "3":
third_page()
elif page == "4":
fourth_page()
else:
intro()
pygame.display.update()
pygame.quit()
The keyboard events (see pygame.event module) occur only once when the state of a key changes. The KEYDOWN
event occurs once every time a key is pressed. KEYUP
occurs once every time a key is released. Use the keyboard events for a single action or a step-by-step movement.pygame.key.get_pressed()
returns a list with the state of each key. If a key is held down, the state for the key is True
, otherwise False
. Use pygame.key.get_pressed()
to evaluate the current state of a button and get continuous movement