Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add move_forwards(dist) for Actor. Closes #193 #194

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pgzero/actor.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,12 @@ def draw(self):
s = self._build_transformed_surf()
game.screen.blit(s, self.topleft)

def move_forwards(self, distance):
"""Move this actor forwards a distance, according to its angle."""
angleInRads = radians(self.angle)
self.x += cos(angleInRads) * distance
self.y += sin(angleInRads) * -distance

def angle_to(self, target):
"""Return the angle from this actors position to target, in degrees."""
if isinstance(target, Actor):
Expand Down
25 changes: 25 additions & 0 deletions test/test_actor.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,31 @@ def test_rotation(self):
a.angle += 1.0
self.assertEqual(a.pos, (100.0, 100.0))

def test_move_forwards_ne(self):
"""move fwd should change pos correctly when facing roughly NE"""
a = Actor('alien', pos=(100.0, 200.0))
# the angle for a 3 4 5 triangle
a.angle = 53.1301024
a.move_forwards(5)
self.assertAlmostEqual(a.x, 103)
self.assertAlmostEqual(a.y, 196)

def test_move_forwards_sw(self):
"""Move fwd should change pos correctly when facing roughly SW"""
a = Actor('alien', pos=(100.0, 200.0))
a.angle = 180 + 53.1301024
a.move_forwards(5)
self.assertAlmostEqual(a.x, 97)
self.assertAlmostEqual(a.y, 204)

def test_move_forwards_zero(self):
""""""
a = Actor('alien', pos=(100.0, 200.0))
a.angle = 53.1301024
a.move_forwards(0)
self.assertEqual(a.x, 100.0)
self.assertEqual(a.y, 200.0)

def test_opacity_default(self):
"""Ensure opacity is initially set to its default value."""
a = Actor('alien')
Expand Down