Skip to content

Commit

Permalink
Improve documentation for inheritance, basic attribute access
Browse files Browse the repository at this point in the history
  • Loading branch information
acbart committed Oct 30, 2023
1 parent 75005c3 commit fae138c
Show file tree
Hide file tree
Showing 7 changed files with 930 additions and 129 deletions.
3 changes: 2 additions & 1 deletion docsrc/_static/custom.css
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ dl.object > dt, dl.data > dt, dl.function > dt, dl.method > dt, dl.attribute > d

.body a {
text-decoration: underline;
}
}

11 changes: 10 additions & 1 deletion docsrc/examples/arrows.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,19 @@ Arrows

This game demonstrates how to have objects that:

* Respond to `'typing'` and `'done typing'` events to "keep moving" until a key is released (even when multiple keys are pressed)
* Respond to ``'typing'`` and ``'done typing'`` events to "keep moving" until a key is released (even when multiple keys are pressed)
* Moving objects in sync


The major trick is to have a field (e.g., ``active``) that gets set to ``True`` during the ``'typing'`` event
and set to ``False`` during the ``'done typing'`` event. Then, in a ``updating`` event, you can check if the field
is ``True`` and move the object accordingly.

For handling left and right keys, you need two boolean fields. For all four direction keys, you would need four boolean fields.
An alternative solution is to use a string field that gets set to ``'left'``, ``'right'``, ``'up'``, or ``'down'`` during the ``'typing'`` event,
and then set to ``'none'`` during the ``'done typing'`` event. Then, in the ``updating`` event, you can check the value of the field
and move the object accordingly.

.. tabs::

.. group-tab:: Dataclasses
Expand Down
23 changes: 23 additions & 0 deletions docsrc/examples/inheritance.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
.. _inheritance:

Extending DesignerObjects
=========================

This game demonstrates how to have:

* A ``DesignerObject`` that has additional extra fields using inheritance

By inheriting from a constructor for a ``DesignerObject``, you can add additional fields.
One drawback is that when calling the constructor for your new class, you must pass in the field as a keyword argument.
This just means you put the name of the field in front of the value you want to pass in.

In this example, we inherit ``MovingEmoji`` from ``Emoji`` so that we can add ``speed`` and ``direction`` fields.
Now, we can freely access ``world.dragon.speed`` the same way we could previously access ``world.dragon.x`` or
``world.dragon.height``.

.. tabs::

.. group-tab:: Dataclasses

.. literalinclude:: ./inheritance/inheritance_dataclass.py
:language: python
45 changes: 45 additions & 0 deletions docsrc/examples/inheritance/inheritance_dataclass.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from designer import *
from dataclasses import dataclass


# Note that we have (Emoji) after the name of the new class!
class MovingEmoji(Emoji):
""" An emoji that can move in a direction at a speed """
speed: int
direction: int

@dataclass
class World:
dragon: MovingEmoji

def create_dragon() -> MovingEmoji:
""" Create a dragon with initial speed 1, moving to the right"""
return MovingEmoji('🐉', speed=1, direction=0)

def create_world() -> World:
""" Create a world with a dragon in it """
return World(create_dragon())

def accelerate_dragon(world: World, key: str):
""" Pressing up or down increases or decreases the dragon's speed """
if key == 'up':
world.dragon.speed += 1
elif key == 'down':
world.dragon.speed -= 1

def spin_dragon(world: World, key: str):
""" Pressing left or right increases or decreases the dragon's direction """
if key == 'left':
world.dragon.direction -= 10
elif key == 'right':
world.dragon.direction += 10

def move_dragon(world: World):
""" Move the dragon forward in its current direction based on its speed """
move_forward(world.dragon, world.dragon.speed, world.dragon.direction)

when('starting', create_world)
when('updating', move_dragon)
when('typing', spin_dragon)
when('typing', accelerate_dragon)
start()
Loading

0 comments on commit fae138c

Please sign in to comment.