0

Currently attempting to add a sprint feature to a simple pygame python program, however when i hold down the shift key, instead of anything occurring, the shift key is never being registered as any kind of input.

import pygame
import random
pygame.init()

s1 = pygame.display.set_mode([1000, 1000])
s2 = pygame.display.set_mode([1000, 1000])
s3 = pygame.display.set_mode([1000, 1000])
s4 = pygame.display.set_mode([1000, 1000])
s5 = pygame.display.set_mode([1000, 1000])

pleft = 500
ptop = 500

playcolor = (200, 200, 200)

play = True

while play:
    player = pygame.Rect(pleft, ptop, 20, 60)
    playerspeed = 2
    stamina = 20
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            play = False
            
            
    keys = pygame.key.get_pressed()
    if keys[pygame.K_w]:
        ptop -= playerspeed
        direct = "w"
    if keys[pygame.K_s]:
        ptop += playerspeed
        direct = "s"
    if keys[pygame.K_d]:
        pleft += playerspeed
        direct = "d"
    if keys[pygame.K_a]:
        pleft -= playerspeed
        direct = "a"
    if keys[pygame.K_LSHIFT]:
        if stamina > 0:
            playerspeed = 4
            stamina -= 2



    s1.fill((100, 215, 0))

    pygame.draw.rect(s1, playcolor, player)

    pygame.display.flip()

I have attempted to change the code so that the sprint is toggled via the event loop instead of within the same code portion as the movement keys, however that has also not worked. if i put in a print statement that triggers when the shift key is pressed, however the print statement also does not trigger when the shift key is held down, which is what led me to realize the key was not being registered in the first place. and yes, the program can recognize multiple keys being pressed at the same time and respond correctly

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Unrelated but you could use a match case for this – Aimarekin Feb 01 '23 at 17:28
  • 3
    The problem is that `playerspeed = 2` is set in the application loop instead of before the application loop. So the `playerspeed` is reset in every frame. – Rabbid76 Feb 01 '23 at 17:31

0 Answers0