From f334e5dc7f72fc9bac017b0a155733626a91cb3f Mon Sep 17 00:00:00 2001 From: Neill Bogie Date: Thu, 8 Aug 2019 00:05:16 +0100 Subject: [PATCH] Add move_forwards for Actor. Closes #193 --- pgzero/actor.py | 6 ++++++ test/test_actor.py | 25 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/pgzero/actor.py b/pgzero/actor.py index 165478d3..b6348bcc 100644 --- a/pgzero/actor.py +++ b/pgzero/actor.py @@ -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): diff --git a/test/test_actor.py b/test/test_actor.py index b74410c4..64e20e50 100644 --- a/test/test_actor.py +++ b/test/test_actor.py @@ -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')