Skip to content

Commit 405728e

Browse files
committed
WIP #193 - No overshoot on move_towards_point
Added an optional parameter to `move_towards_point()` named `overshoot` that is `False` by default. Actors will now automatically come to stop at the target when moving towards a point (preventing the rapid back and forth from before) unless `overshoot` is supplied as `True`. Updated documentation to reflect this change.
1 parent bcaebc9 commit 405728e

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

doc/builtins.rst

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -638,10 +638,15 @@ actors around their rotation easier, Pygame Zero provides built-in functions.
638638
Moves the actor the given distance along the given angle.
639639

640640

641-
.. method:: Actor.move_towards_point(point, distance)
641+
.. method:: Actor.move_towards_point(point, distance, [overshoot])
642642

643643
Moves the actor the given distance towards the given point of X and Y.
644644

645+
By default, if the distance to the point is smaller than the given
646+
distance, the actor will only move up to the point but not overshoot it.
647+
If the optional parameter ``overshoot`` is given as True however, the
648+
actor will move past the target point if the given distance is far enough.
649+
645650

646651
.. method:: Actor.move_forward(distance)
647652

@@ -685,13 +690,13 @@ the game window::
685690
def update():
686691
# To just read the value of the global variable,
687692
# we don't have to do anything else.
688-
ship.move_towards_point(mouse_position)
693+
ship.move_towards_point(mouse_position, 5)
689694

690-
*Note:* When using ``move_towards_point()``, if the distance overshoots the target
691-
and the function is called again every frame (for example in ``update()``), the
692-
actor will rapidly jump back and forth since the angle to the target point gets
693-
inverted every frame. To prevent this, check the distance to the target point
694-
and adjust the length of the movement if necessary.
695+
*Note:* When using ``move_towards_point()`` with ``overshoot=True``, if the
696+
function is called every frame (for example in ``update()``), the actor will
697+
rapidly jump back and forth since the angle to the target point gets inverted
698+
every frame. To prevent this, use ``move_towards_point()`` without ``overshoot``
699+
or make sure it is not called rapidly with ``overshoot``.
695700

696701

697702
.. _transparency:

src/pgzero/actor.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,11 +366,15 @@ def move_towards_angle(self, angle, distance):
366366
self.x += move_x
367367
self.y += move_y
368368

369-
def move_towards_point(self, point, distance):
369+
def move_towards_point(self, point, distance, overshoot=False):
370370
"""Figure out the angle to the given point and then
371371
move the actor towards it by the given distance."""
372372
angle = self.angle_to(point)
373-
self.move_towards_angle(angle, distance)
373+
if overshoot:
374+
self.move_towards_angle(angle, distance)
375+
else:
376+
m_distance = min(self.distance_to(point), distance)
377+
self.move_towards_angle(angle, m_distance)
374378

375379
def move_forward(self, distance):
376380
"""Move the actor in the direction it is facing."""

0 commit comments

Comments
 (0)