Timer#

Timer classes schedule actions in future frames.

The timer and loop decorators are convenience helpers for one-time and repeating actions.

API Reference#

Timer#

class miniworlds.tools.timer.Timer(time)[source]#

Timer that calls act() repeatedly after a frame interval.

Subclass this and override act() when you need custom timer logic. For most use-cases prefer ActionTimer or the @timer decorator.

Parameters:

time – Frame interval.

Examples

class BlinkTimer(Timer):
    def act(self):
        actor.visible = not actor.visible
act()[source]#

Hook method that is executed when the timer interval is reached.

Subclasses override this method with the action that should happen after the configured number of frames.

tick()[source]#

Count frames and call act() when the interval is reached.

This method is called automatically once per frame by the world.

ActionTimer#

class miniworlds.tools.timer.ActionTimer(time, method, arguments=None)[source]#

Calls a method after time frames.

Parameters:
  • time – Frames to wait before calling the method.

  • method – Method to call.

  • arguments – Optional single argument passed to the method.

Examples

ActionTimer(48, player.move, 2)

@timer(frames=24)
def moving():
    player.move()
__init__(time, method, arguments=None)[source]#

Create a one-shot action timer.

Parameters:
  • time – Frames to wait before calling the method.

  • method – Method to call.

  • arguments – Optional single argument passed to the method.

act()[source]#

Calls the stored method once and then unregisters the timer.

This is the behavior behind @timer and one-shot delayed actions.

LoopActionTimer#

class miniworlds.tools.timer.LoopActionTimer(time, method, arguments=None)[source]#

Calls a method after time frames repeatedly until the timer is unregistered.

Examples

LoopActionTimer(48, player.move, 2)

@loop(frames=24)
def moving():
    player.move()
act()[source]#

Calls the stored method once and then unregisters the timer.

This is the behavior behind @timer and one-shot delayed actions.

timer#

miniworlds.tools.timer.timer(*args, **kwargs)[source]#

Decorate a function to run once after a number of frames.

Parameters:

frames – Frames to wait before the function runs.

Examples

@timer(frames=24)
def moving():
    player.move()

loop#

miniworlds.tools.timer.loop(*args, **kwargs)[source]#

Decorate a function to run repeatedly after a frame interval.

Parameters:

frames – Frames between function calls.

Examples

@loop(frames=24)
def moving():
    player.move()