The reason the code has no affect is because you modify the value of x
, a local variable to the function scope, and not the player position. Call player.setx(x)
to actually update the player position after updating the local variable x
. If you want to bound your positions you could also use the built in min
/max
methods:
def move_left():
x = player.xcor()
x -= playerspeed
player.setx(max(x, -280))
def move_right():
x = player.xcor()
x += playerspeed
player.setx(min(x, 280))
solved Python: How come the math operators won’t work?