gitmcgee
gitmcgee

Reputation: 177

Pygame holding key down, keys = pygame.key.get_pressed()

I'm trying to make a pygame game on python. I want it so that when holding down a key the Sprite runs in a direction until the key is released, i tried using pygame.key.get_pressed() but it isn't working? Any advice? (also the game is a batman game didn't know what else to do)

def main():
    
    pygame.init()
    clock = pygame.time.Clock()
    gameDisplay = pygame.display.set_mode((W,H))
    

    RED = (255,0,0)
    
    BatmanImg = pygame.image.load('batman.png')
    gothamcity = pygame.image.load('gotham.png')
    
    background = (0,0,0)
    screen = pygame.display.set_mode((W, H))
    player_size = 50
    
    
    
    player_pos = [W/2, H-2*player_size+40]
    game_over = False
    while not game_over:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

            x = player_pos[0]
            y = player_pos[1]
            keys = pygame.key.get_pressed()
            if keys[pygame.K_RIGHT]:
                vel = vec(0, 0)
                acc = vec(0,0)
                acc.x += 5
                vel = acc
                player_pos += vel + 0.5 * acc
                                              
            elif keys[pygame.K_LEFT]:
                vel = vec(0, 0)
                acc = vec(0,0)
                acc.x -= 5
                vel = acc   
                player_pos += vel + 0.5 * acc

                
            if player_pos[0] > W:
                player_pos[0] = 0
            if player_pos[0] < 0:
                player_pos[0] = W            
                   
        
        screen.blit(gothamcity, [0, 0])
        mainchar(player_pos, BatmanImg)
        pygame.draw.rect(screen, RED, (265,540,10,10))
        clock.tick(60)
        pygame.display.update()  
        
main()

Upvotes: 1

Views: 256

Answers (1)

Rabbid76
Rabbid76

Reputation: 210968

Its a matter of indentation Indentation. You have to do the movement in the application loop rather than in the event loop. The event loop is executed only when a event occurs, but the application loop is executed in every frame.
Note, pygame.key.get_pressed() returns the current state of the keys, but a key event occurs only once when a key is pressed (KEYDOWN) or a key is released (KEYUP).

def main():

    pygame.init()
    clock = pygame.time.Clock()
    gameDisplay = pygame.display.set_mode((W,H))


    RED = (255,0,0)

    BatmanImg = pygame.image.load('batman.png')
    gothamcity = pygame.image.load('gotham.png')

    background = (0,0,0)
    screen = pygame.display.set_mode((W, H))
    player_size = 50



    player_pos = [W/2, H-2*player_size+40]
    game_over = False
    while not game_over:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

        #<--| INDENTATION
        
        x = player_pos[0]
        y = player_pos[1]
        keys = pygame.key.get_pressed()
        if keys[pygame.K_RIGHT]:
            vel = vec(0, 0)
            acc = vec(0,0)
            acc.x += 5
            vel = acc
            player_pos += vel + 0.5 * acc
                                        
        elif keys[pygame.K_LEFT]:
            vel = vec(0, 0)
            acc = vec(0,0)
            acc.x -= 5
            vel = acc   
            player_pos += vel + 0.5 * acc

            
        if player_pos[0] > W:
            player_pos[0] = 0
        if player_pos[0] < 0:
            player_pos[0] = W            
                
        
        screen.blit(gothamcity, [0, 0])
        mainchar(player_pos, BatmanImg)
        pygame.draw.rect(screen, RED, (265,540,10,10))
        clock.tick(60)
        pygame.display.update()  
    
main()

Upvotes: 2

Related Questions