So i'm trying to make a topdown game and i'm having problems with player movement. When i press key [W,A,S or D] player moves that way. But i want it to keep moving as long as button is being held down. So i change [playerMoveLeft, playerMoveRight, playerMoveDown or playerMoveUp] to 1
if allowMovement == 1:
if event.key == pygame.K_a:
playerMoveLeft = 1
print("Hello")
if event.key == pygame.K_d:
playerMoveRight = 1
if event.key == pygame.K_s:
playerMoveDown = 1
if event.key == pygame.K_w:
playerMoveUp = 1
if event.key == pygame.K_LSHIFT:
playerMoveSprint = 1
playerSpeed = 2
So as long as one of playerMoves are 1 player should keep moving. Know the problem: When i release the key it sets all playerMoves to 0 what causes the player to stop so i'm trying to find a way to only testfor 1 specific key to be lifted up and that would set thath specific playerMove to 0.
if event.type == pygame.KEYUP:
playerMoveLeft = 0
playerMoveRight = 0
playerMoveUp = 0
playerMoveDown = 0
playerMoveSprint = 0
Below this text there is the Full code area for player movement
for event in pygame.event.get():
if event.type == pygame.QUIT:
valmis = True
if event.type == pygame.KEYDOWN:
############################## CONTROL PLAYER ############################
if allowMovement == 1:
if event.key == pygame.K_a:
playerMoveLeft = 1
print("Hello")
if event.key == pygame.K_d:
playerMoveRight = 1
if event.key == pygame.K_s:
playerMoveDown = 1
if event.key == pygame.K_w:
playerMoveUp = 1
if event.key == pygame.K_LSHIFT:
playerMoveSprint = 1
playerSpeed = 2
if event.type == pygame.KEYUP:
playerMoveLeft = 0
playerMoveRight = 0
playerMoveUp = 0
playerMoveDown = 0
playerMoveSprint = 0
if playerMoveSprint == 1:
playerSpeed = 1
if playerMoveLeft == 1:
player_x -=(playerSpeed)
if playerMoveRight == 1:
player_x +=(playerSpeed)
if playerMoveUp == 1:
player_y -=(playerSpeed)
if playerMoveDown == 1:
player_y +=(playerSpeed)
thanks for every one who helps!
Aucun commentaire:
Enregistrer un commentaire