Bouncing ball game tkinter canvas
The problem is that the speed of the platform is dependent on the auto-repeat speed of your keyboard.
Instead of moving once for each <Right>
or <Left>
event, use a key press to start the platform moving in the desired direction and a key release to stop the platform moving. Then, use after
to repeatedly move the platform in the given direction.
Example:
after_id = Nonedef platform_move(direction): """ direction should be -1 to move left, +1 to move right, or 0 to stop moving """ global after_id speed = 10 if direction == 0: canvas.after_cancel(after_id) after_id = None else: canvas.move(platform, direction*speed, 0) after_id = canvas.after(5, platform_move, direction)canvas.bind_all("<KeyPress-Right>", lambda event: platform_move(1))canvas.bind_all("<KeyRelease-Right>", lambda event: platform_move(0))canvas.bind_all("<KeyPress-Left>", lambda event: platform_move(-1))canvas.bind_all("<KeyRelease-Left>", lambda event: platform_move(0))
The above code doesn't handle the case where you might press both keys at the same time, but that can be handled with a little additional logic. The point is to show how you can use the keys to start and stop an animation.