Answer the question
In order to leave comments, you need to log in
How to do braking physics in pygame (Python)?
It became interesting to play with writing mini toys .. and since the subject of my study is Python, I chose the pygame library. I want to make braking physics so that when you release the button, for example to the right, the character (still a cube) does not stop immediately, but after a little "driving".
for i in pygame.event.get():
if i.type == pygame.QUIT:
work = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and Player.x > 0:
Player.x -= Player.speed
if keys[pygame.K_RIGHT] and Player.x < 640 - Player.width:
Player.x += Player.speed
Answer the question
In order to leave comments, you need to log in
To do this, you just need to study physics of the 8th grade.
Acceleration = 0
User presses the button - Acceleration = N (distance units / unit time 2 )
Releases - acceleration = 0
Deceleration will be some kind of constant. Let it be N / 2. This is an imitation of some kind of friction force.
Each tick you increase the speed by (acceleration - deceleration) * (tick time / your unit of time)
If the speed is 0, then the deceleration is replaced by zero
If you need to instantly set the speed when you press it, you process it separately.
The maximum speed limit (if any) must also be handled
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question