earwax.level module

Provides classes for working with levels.

class earwax.level.IntroLevel(game: Game, level: earwax.level.Level, sound_path: pathlib.Path, skip_after: Optional[float] = None, looping: bool = False, sound_manager: Optional[earwax.sound.SoundManager] = NOTHING, play_kwargs: Dict[str, Any] = NOTHING)

Bases: earwax.level.Level

An introduction level.

This class represents a level that plays some audio, before optionally replacing itself in the level stack with self.level.

If you want it to be possible to skip this level, add a trigger for the skip() action.

Variables:
  • level – The level that will replace this one.
  • sound_path – The sound to play when this level is pushed.
  • skip_after

    An optional number of seconds to wait before skipping this level.

    If this value is None, then the level will not automatically skip itself, and you will have to provide some other means of getting past it.

  • looping

    Whether or not the playing sound should loop.

    If this value is True, then skip_after must be None, otherwise AssertionError will be raised.

  • sound_manager

    The sound manager to use to play the sound.

    If this value is None, then the sound will not be playing.

    This value default to earwax.Game.interface_sound_manager.

  • play_kwargs

    Extra arguments to pass to the play() method of the sound_manager.

    When the on_push() event is dispatched, an error will be raised if this dictionary contains a looping key, as 2 looping arguments would be passed to self.sound_manager.play_path.

  • sound

    The sound object which represents the playing sound.

    If this value is None, then the sound will not be playing.

get_default_sound_manager() → Optional[earwax.sound.SoundManager]

Return a suitable sound manager.

on_pop() → None

Destroy any created sound().

on_push() → None

Run code when this level has been pushed.

Starts playing self.sound_path, and optionally schedules an automatic skip.

skip() → Generator[None, None, None]

Skip this level.

Replaces this level in the level stack with self.level.

class earwax.level.Level(game: Game)

Bases: earwax.mixins.RegisterEventMixin, earwax.action_map.ActionMap

A level in a Game instance.

An object that contains event handlers. Can be pushed and pulled from within a Game instance.

While the Game object is the centre of a game, Level instances are where the magic happens.

If the included action() and motion() decorators aren’t enough for your needs, and you want to harness the full power of the Pyglet event system, simply subclass earwax.Level, and include the requisite events. The underlying Game object will do all the heavy lifting for you, by way of the EventMatcher framework.

Variables:
  • game – The game this level is bound to.
  • actions – A list of actions which can be called on this object. To define more, use the action() decorator.
  • motions – The defined motion events. To define more, use the motion() decorator.
  • ambiances – The ambiances for this level.
  • tracks – The tracks (musical or otherwise) that play while this level is top of the stack.
motion(motion: int) → Callable[[MotionFunctionType], MotionFunctionType]

Add a handler to motions.

For example:

@level.motion(key.MOTION_LEFT)
def move_left():
    # ...

This is the method used by earwax.Editor, to make text editable, and earwax.Menu, to make menus searchable.

Parameters:motion – One of the motion constants from pyglet.window.key.
on_cover(level: earwax.level.Level) → None

Code to run when this level has been covered by a new one.

on_pop() → None

Run code when this level is popped.

This event is called when a level has been popped from the level stack of a game.

on_push() → None

Run code when this level is pushed.

This event is called when a level has been pushed onto the level stack of a game.

on_reveal() → None

Code to be run when this level is exposed.

This event is called when the level above this one in the stack has been popped, thus revealing this level.

on_text_motion(motion: int) → None

Call the appropriate motion.

The motions dictionary will be consulted, and if the provided motion is found, then that function will be called.

This is the default event that is used by pyglet.window.Window.

Parameters:motion – One of the motion constants from pyglet.window.key.
start_ambiances() → None

Start all the ambiances on this instance.

start_tracks() → None

Start all the tracks on this instance.

stop_ambiances() → None

Stop all the ambiances on this instance.

stop_tracks() → None

Stop all the tracks on this instance.