Pygame Lever Images Display On Key Click Problem

Viewed 55

so I have a lever image with 3 different directions my problem is when I use the if [pygame.K_p] mutliple times I want to switch between the defferent images but heres my problem VIDEO when I click p it display right and the middle one but not the left one and I have to hold the p for some reason to switch it I want it so when I click p 1 time it switches direction of my lever.

In My Main Loop here what I have done for the first one I said if we click p then move1 should be True then if my move1 is true then it should switch my direction of my lever image to right one then my next one of my image is on the right and we click p again it should switch my image back to 1 the middle one and like repeat I am not sure if I am doing correctly but yeah

switch1 is the *left switch2 is the middle switch3 right one

    if keys[pygame.K_p]:
        move1 = True
        move2 = False


    
    if keys[pygame.K_p]:
        if switch1.direction == "switch3":
            move1 = False
            move2 = True


    if switch1.direction == "switch3" or switch1.direction == "switch1":
        if keys[pygame.K_p]:
            switch1.direction = "switch2"
            if switch1.direction == "switch2":
                move1 = False
                move2 = False
            
    if move1:
        move2 = False
        switch1.direction = "switch3"

        if rop1.y <= 200:
            rop1.y += 2

        else:
            rop1.direction = "nomove"


    if move2:
        move1 = False
        switch1.direction = "switch1"


        if rop1.y <= 200:
            rop1.y -= 2

        else:
            rop1.direction = "nomove"

I am looking for a way to switch my levers with 1 keybind "p" left right or middle repeat Thank You!

enter image description here

enter image description here

enter image description here

1 Answers

For this task you have to use the KEYDOWN event instead of pygame.key.get_pressed().

pygame.key.get_pressed() returns a sequence 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.

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.


Create a list of "switch" Surface. Use a variable to store the index of the current "switch" Surface (current_switch). An additional variable (current_move ) that indicates whether the index needs to be increased or decreased when p is pressed.

current_switch can have the values 0, 1 or 2. 0 means left, 1 is middle and 2 is right. current_move is either -1 or 1. If current_move is -1, the switch moves to the left when p is pressed. If current_move is 1, the switch moves to the right when "p" is pressed. When the switch reaches the left or right position, the current_move must be changed:

import pygame

pygame.init()
window = pygame.display.set_mode((200, 200))
clock = pygame.time.Clock()

files = ['switch_left.png', 'switch_middle.png', 'switch_right.png'] 
switches = [pygame.image.load(f) for f in files]
current_switch = 0
current_move = 1

run = True
while run:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_p:
                
                if current_switch == 0: 
                    current_move = 1
                elif current_switch == len(switches)-1: 
                    current_move = -1
                current_switch += current_move

    window.fill(0)
    swtich_image = switches[current_switch]
    window.blit(swtich_image, swtich_image.get_rect(midbottom = (100, 150)))
    pygame.display.flip()

pygame.quit()
exit()
Related