0

I'm trying to do a word game with Pygame and this is the code I developed for changing pages. But when pressed the pages only show for a second then disappear. I tried writing input() but it doesn't work. How can I fix this?

run = True
while run:
    clock.tick(FPS)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        pressed = pygame.key.get_pressed()
        if pressed[pygame.K_1]:
            first_page()
        if pressed[pygame.K_2]:
            second_page()
        if pressed[pygame.K_3]:
            third_page()
        if pressed[pygame.K_4]:
            fourth_page()
    intro()
    pygame.display.update()
pygame.quit()
orion
  • 1
  • 1

2 Answers2

1

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

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
0

That code example is a bit to empty to see your problem clearly, but it appears like the xxxx_page() functions return quite quickly so the main loop can continue running, right?

In that case, intro() gets called as soon as the page function returns and presumably overwrites whatever the page function did.

You shouldn't call intro at every iteration of your main loop, but again it's impossible to know what the functions actually do.

  • thank you for answering! the functions just have some buttons and texts so I don't think they are the problem. I wrote intro() into the if loop as you suggested and it solved the problem but now the functions don't work unless you hold down the key. Anyways thanks for the answer. – orion Jan 13 '22 at 10:08