Skip to content

Commit

Permalink
Merge #463
Browse files Browse the repository at this point in the history
463: Adds ordinal direction unit vectors. r=astronouth7303 a=pathunstrom

Primary use case is to reduce the cognitive load in early lessons:

```python
class OurSprite(Sprite):
    def on_update():
       self.facing = Left
```

Co-authored-by: Piper Thunstrom <pathunstrom@gmail.com>
  • Loading branch information
bors[bot] and pathunstrom authored May 9, 2020
2 parents 8577746 + 7252fe4 commit 51fe7a0
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
8 changes: 8 additions & 0 deletions docs/reference/directions.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.. py:currentmodule:: ppb.directions
===============================
Directions
===============================

.. automodule:: ppb.directions
:members:
1 change: 1 addition & 0 deletions docs/reference/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ decisions are made, see the :doc:`/discussion/index` section.
engine
sound
camera
directions
features/index
20 changes: 19 additions & 1 deletion ppb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,28 @@
The classes, modules, and methods exported directly are the most used parts of
the library and intended to be used by users at all levels (barring
make_engine). Advanced features tend to be in their own modules and subpackages.
Exports:
* :class:`~ppb_vector.Vector`
* :class:`BaseScene`
* :class:`Circle`
* :class:`Image`
* :class:`Sprite`
* :class:`Square`
* :class:`Sound`
* :class:`Triangle`
* :mod:`events`
* :class:`Font`
* :class:`Text`
* :mod:`directions`
"""

import logging
import warnings
from typing import Callable

from ppb import directions
from ppb import events
from ppb_vector import Vector
from ppb.assets import Circle
Expand All @@ -34,7 +50,7 @@
__all__ = (
# Shortcuts
'Vector', 'BaseScene', 'BaseSprite', 'Circle', 'Image', 'Sprite',
'Square', 'Sound', 'Triangle', 'events', 'Font', 'Text',
'Square', 'Sound', 'Triangle', 'events', 'Font', 'Text', 'directions',
# Local stuff
'run', 'make_engine',
)
Expand Down Expand Up @@ -78,6 +94,7 @@ def run(setup: Callable[[BaseScene], None] = None, *, log_level=logging.WARNING,
Sample usage:
::
import ppb
def setup(scene):
Expand All @@ -87,6 +104,7 @@ def setup(scene):
Alternatively:
::
import ppb
class Game(ppb.BaseScene):
Expand Down
17 changes: 17 additions & 0 deletions ppb/directions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"""
The ordinal directions.
A collection of normalized vectors to be referenced by name.
Best used for the positions or facings of :class:`Sprites <ppb.Sprite>`.
"""
from ppb_vector import Vector

Up = Vector(0, 1).normalize() #: Unit vector to the top of the screen from center.
Down = Vector(0, -1).normalize() #: Unit vector to the bottom of the screen from center.
Left = Vector(-1, 0).normalize() #: Unit vector to the left of the screen from center.
Right = Vector(1, 0).normalize() #: Unit vector to the right of the screen from center.
UpAndLeft = (Up + Left).normalize() #: Unit vector diagonally up and to the left of the screen from center.
UpAndRight = (Up + Right).normalize() #: Unit vector diagonally up and to the right of the screen from center.
DownAndLeft = (Down + Left).normalize() #: Unit vector diagonally down and to the left of the screen from center.
DownAndRight = (Down + Right).normalize() #: Unit vector diagonally down and to the right of the screen from center.

0 comments on commit 51fe7a0

Please sign in to comment.