Skip to content
Advertisement

How to make sprite move to point along a curve in pygame

I’m doing a pygame project for practice and I need a sprite to move to some point on screen and I did it, but it moves in a straight line and I would like to learn how to make it move to the same point in a curve.

JavaScript

This movement looks like

this

and I would like it to be like

this

Advertisement

Answer

There are many many ways to achieve what you want. One possibility is a Bézier curve:

JavaScript

p0, p1 and p2 are the control points and t is a value in the range [0,0, 1,0] indicating the position along the curve. p0 is the start of the curve and p2 is the end of the curve. If t = 0, the point returned by the bezier function is equal to p0. If t=1, the point returned is equal to p2.

Also see PyGameExamplesAndAnswers – Shape and contour – Bezier


Minimal example:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement