I have the following code inside a while True loop:
if abs(playerx) < MAXSPEED:
if moveLeft:
playerx -= 1
if moveRight:
playerx += 1
if abs(playery) < MAXSPEED:
if moveDown:
playery += 1
if moveUp:
playery -= 1
if moveLeft == False and abs(playerx) > 0:
playerx += 1
if moveRight == False and abs(playerx) > 0:
playerx -= 1
if moveUp == False and abs(playery) > 0:
playery += 1
if moveDown == False and abs(playery) > 0:
playery -= 1
player.x += playerx
player.y += playery
if player.left < 0 or player.right > 1000:
player.x -= playerx
if player.top < 0 or player.bottom > 600:
player.y -= playery
The intended result is that while an arrow key is pressed, playerx
or playery
increments by one at every iteration until it reaches MAXSPEED
and stays at MAXSPEED
. And that when the player stops pressing that arrow key, his speed decreases until it reaches 0.
To me, this code explicitly says that...
But what actually happens is that playerx
or playery
keeps incrementing regardless of MAXSPEED
and continues moving even after the player stops pressing the arrow key.
I keep rereading but I'm completely baffled by this weird behavior. Any insights? Thanks.