Skip to content

Commit 3192b7b

Browse files
committed
FIX #193 - Updated documentation
1 parent de5ffee commit 3192b7b

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

doc/builtins.rst

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,76 @@ Actors or ``(x, y)`` coordinate pairs.
624624
* Down is -90 degrees.
625625

626626

627+
.. _angle_movement:
628+
629+
Angle Movement
630+
''''''''''''''
631+
632+
If an actor is rotated and should move based on its rotation, doing so by
633+
adjusting X and Y coordinates manually can be complicated. To make moving
634+
actors around their rotation easier, Pygame Zero provides built-in functions.
635+
636+
.. method:: Actor.move_towards_angle(angle, distance)
637+
638+
Moves the actor the given distance along the given angle.
639+
640+
641+
.. method:: Actor.move_towards_point(point, distance)
642+
643+
Moves the actor the given distance towards the given point of X and Y.
644+
645+
646+
.. method:: Actor.move_forward(distance)
647+
648+
Moves the actor forwards along its current angle by the given distance.
649+
650+
651+
.. method:: Actor.move_backward(distance)
652+
653+
Moves the actor backwards in the opposite direction of its current angle
654+
by the given distance.
655+
656+
.. method:: Actor.move_left(distance)
657+
658+
Moves the actor sideways to the left when viewing its angle as forward.
659+
660+
This does not mean the actor moves along the Y-axis, but instead that if
661+
the actor is pointing to the right, then right is forward to the actor and
662+
left from its perspective would be up in the game window.
663+
664+
.. method:: Actor.move_right(distance)
665+
666+
Moves the actor sideways to the right when viewing its angle as forward.
667+
668+
The same applies here. Right is always in relation to where the actor is
669+
pointing.
670+
671+
These function could be used to have actors always move towards the player,
672+
circle around a point in a level, get pushed away from something or many other
673+
options. As a small example, let's have the spaceship follow the mouse around
674+
the game window::
675+
676+
ship = Actor('ship')
677+
mouse_position = (0, 0)
678+
679+
def on_mouse_move(pos):
680+
# To change mouse_position from within a function,
681+
# we have to declare it global here.
682+
global mouse_position
683+
mouse_position = pos
684+
685+
def update():
686+
# To just read the value of the global variable,
687+
# we don't have to do anything else.
688+
ship.move_towards_point(mouse_position)
689+
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+
696+
627697
.. _transparency:
628698

629699
Transparency

0 commit comments

Comments
 (0)