Search code examples
pythonpygame

how do i make this code continuously execute even after releasing the "x" key?


I want the if statement to continuously execute the code beneath it, however I am unsure of what I should do. Any kind of help will be highly appreciated.

p.s sorry if my terminology is wrong as I am new to programming

key = pygame.key.get_pressed()
if key [pygame.K_x]:
    screen.blit (bg, (0, 0))
    player.update()
    kivotos.draw()
    obusutakuru.update()

Solution

  • In a variable remember if the x-key has been pressed

    # outside the game loop
    xKeyPressed = False
    
    .....
    
    # inside the game loop
    key = pygame.key.get_pressed()
    if key[pygame.K_x]:
        xKeyPressed = True
    
    if xKeyPressed:
        screen.blit (bg, (0, 0))
        player.update()
        kivotos.draw()
        obusutakuru.update()