Habib Ismail
Habib Ismail

Reputation: 47

pygame continue loop when key pressed

I'm trying to make my timer keep adding when we click e but Im not sure why I have to hold e for the timer to keep adding up my timer name is (tons) is there a way I could keep adding my timer when we click e instead of stopping when we are not clicking e anymore I tried the 'if event.type == pygame.K_e' but its the same thing I have to hold e

        if keys[pygame.K_e]: # if we click e then it should keep adding tons
            tons += 1
            print(tons)

game loop V

run = True
while run:
    # Making game run with fps
    clock.tick(fps)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    
# telling what to do when we say the word 'key'
    keys = pygame.key.get_pressed()

            
    if hit:
        if keys[pygame.K_e]: # if we click e then it should keep adding tons
            tons += 1
            print(tons)
            
        if tons > 10:
            playerman.direction = "att"
            if health2 > -21:
                health2 -= 0.3
            else:
                playerman.direction = "Idle"
                hit = False
                
            if tons > 30:
                tons = 0
                playerman.direction = "Idle"

Upvotes: 2

Views: 766

Answers (1)

sloth
sloth

Reputation: 101042

but Im not sure why I have to hold e for the timer to keep adding up

Because that's how you coded it. Look at your code:

 if keys[pygame.K_e]: # if we click e then it should keep adding tons
    tons += 1

tons gets incremented if and only if e is pressed.

is there a way I could keep adding my timer when we click e instead of stopping when we are not clicking e anymore

Just set a flag instead, something like this:

pressed_e = False
run = True
while run:
    for event in pygame.event.get():
        ...
        if event.type == pygame.KEYDOWN and event.key == pygame.K_e:
            pressed_e = True

    if pressed_e: # if we click e then it should keep adding tons
        tons += 1
    ...

Upvotes: 2

Related Questions