tuxemon.clock module

class tuxemon.clock.Clock(time_function=<built-in function perf_counter>)[source]

Bases: Scheduler

Schedules stuff like a Scheduler, and includes time limiting functions

WIP

Parameters:

time_function (Callable[[], float]) –

class tuxemon.clock.ScheduledItem(func, last_ts, next_ts, interval)[source]

Bases: object

A class that describes a scheduled callback.

This class is never created by the user; instead, pygame creates and returns an instance of this class when scheduling a callback.

If you hold on to instance of this class, do not modify any values of it.

Parameters:
  • func (Any) –

  • last_ts (float) –

  • next_ts (float) –

  • interval (float) –

func
interval
last_ts
next_ts
class tuxemon.clock.Scheduler(time_function=<built-in function perf_counter>)[source]

Bases: object

Class for scheduling functions.

Parameters:

time_function (Callable[[], float]) –

call_scheduled_functions(dt)[source]

Call scheduled functions that elapsed on the last update_time.

Parameters:
  • dt (float) – The elapsed time since the last update to pass to each

  • function. (scheduled) –

Returns:

Returns True if any functions were called, otherwise False.

Return type:

bool

get_idle_time()[source]

Get the time until the next item is scheduled.

Returns:

Time until the next scheduled event in time units, or None if there is no event scheduled.

Return type:

float | None

get_interval()[source]

Get the average amount of time passed between each tick.

Useful for calculating FPS if this clock is used with the display. Returned value is averaged from last 10 ticks.

Value will be 0.0 if before 1st tick.

Return type:

float

Returns:

Average amount of time passed between each tick

schedule(func, delay=0.0, repeat=False, soft=False)[source]

Schedule a function to be run sometime in the future.

The function should have a prototype that includes dt as the first argument, which gives the elapsed time, in time units, since the last clock tick.

def callback(dt):

pass

Limitations

There is a hard limit of 10 items that can be scheduled on next tick. This limit reduces the power and performance impact of the clock on mobile and battery operated computers.

A runtime error will be raised if the maximum is reached.

Unscheduling

If callback returns False (not None), then it will not be scheduled again.

Soft Scheduling

This is useful for functions that need to be called regularly, but not relative to the initial start time. for example: events which need to occur regularly – if all audio updates are scheduled at the same time (for example, mixing several tracks of a music score, or playing multiple videos back simultaneously), the resulting load on the CPU is excessive for those intervals but idle outside. Using the soft interval scheduling, the load is more evenly distributed.

param func:

Function to be called

param delay:

Delay in time unit until it is called

param repeat:

Function will be repeated every ‘delay’ units

param soft:

See notes about Soft Scheduling

returns:

Reference to scheduled item

Parameters:
  • func (Any) –

  • delay (float) –

  • repeat (bool) –

  • soft (bool) –

Return type:

ScheduledItem

set_time(time_stamp)[source]

Set the clock manually and do not call scheduled functions. Return the difference in time from the last time clock was updated.

Parameters:
  • time_stamp (float) – This will become the new value of the clock. Setting the clock

  • results. (to negative values will have undefined) –

Returns:

The number of time units since the last update, or 0.0 if this was the first update.

Return type:

float

tick()[source]

Cause clock to update and call scheduled functions.

This updates the clock’s internal measure of time and returns the difference since the last update (or since the clock was created).

Will call any scheduled functions that have elapsed.

Returns:

The number of time units since the last “tick”, or 0 if this was the first tick.

Return type:

float

unschedule(func)[source]

Remove a function from the schedule.

If the function appears in the schedule more than once, all occurrences are removed. If the function was not scheduled, no error is raised.

Parameters:

func (Any) – The function to remove from the schedule.

Return type:

None