Scheduling
You can programme a function to run every time the clock strikes:
def callback(dt):
print(f"{dt} seconds since last callback")
clock.schedule(callback)
A function is called every "n" seconds by the schedule interval method:
clock.schedule_interval(callback, .5) # called twice in a second
The schedule once method causes a function to be called once every "n" seconds:
clock.schedule_once(callback, 5) # called in 5 seconds
Any additional args or keyword args you specify to the callback function will be passed on to all schedule methods:
def move(dt, velocity, sprite):
sprite.position += dt * velocity
clock.schedule(move, velocity=5.0, sprite=alien)
You can use unschedule to cancel a function that has been scheduled using any of these methods:
clock.unschedule(move)
Using multiple clocks
The clock functions are all relayed to a Clock instance that is created when the module is loaded. You can get this instance right now:
clk = clock.get_default()
You can also use your own clock instead of the default one:
myclk = clock.Clock() clock.set_default(myclk)
Each clock has its own set of pre-programmed functions and FPS counter. Each clock must be individually "ticked."
Multiple and derived clocks could help you separate "game-time" from "wall-time," or synchronise your clock with an audio or video stream rather than the system clock.
Classes and Functions
class Clock(time_function=<built-in function perf_counter>)
Framerate is calculated and limited using this class.
It's also used to call pre-programmed functions.
get_default()
Get the default Clock for Pyglet.
Return the Clock instance that is used by all clock functions at the module level.
get_fps()
Calculate the average frequency of clock updates.
The result is a sliding average of the last "n" updates, where "n" is a number that covers roughly one second. This is the rate at which the internal clock is updated, not the rate at which the window is redrawn. Platform events, such as rapidly moving the mouse, cause the clock to refresh more frequently.
get_sleep_time(sleep_idle)
On the default clock, get the time until the next item is scheduled.
schedule(func, *args, **kwargs)
Schedule func to run every second on the default clock.
The first argument to func is dt (the time since the last function call), followed by any *args and **kwargs.
schedule_interval_soft(func, interval, *args, **kwargs)
Schedule func to run every second on the default clock.
To distribute CPU load more evenly over time, the clock will move the interval out of phase with other scheduled functions.
The first argument to func is dt (the time since the last function call), followed by any *args and **kwargs.
schedule_once(func, delay, *args, **kwargs)
After a delay of seconds, schedule func to be called once.
The default clock is used by this function. A float can be a delay. The first argument to func is dt (the time since the last function call), followed by any *args and **kwargs.
The func is queued and will be scheduled on the default clock as soon as it is created if no default clock is set.
set_default(default)
Set the clock that all module-level functions will use as the default.
A Clock instance is used by default.
tick(poll=False)
Indicate that one frame on the default clock has passed.
Any scheduled functions that haven't been called yet will be called now.
unschedule(func)
Remove func from the schedule of the default clock.
If the function was never scheduled, no error is raised.
FAQs
Is pyglet faster than pygame?
Pyglet is definitely faster and has better performance than pygame, and nowadays, speed is always a concern when developing games.
Is Pyglet any good?
Pyglet is significantly faster than pygame right out of the box, which is always a concern when working with pygame (you have to update the smallest parts of the screen, and remembering what has changed can be tedious).
What is pyglet used for?
Python's pyglet library is a cross-platform windowing and multimedia library for creating games and other visually rich applications. Windowing, user interface event handling, game controllers and joysticks, OpenGL graphics, image and video loading, and sound and music playback are all supported.
Is Pyglet slow?
Images (and sprites) in Pyglet can be rotated around any anchor. If you look at pyglet/sprite.py, you'll notice that it uses the Python math module, which explains why it's so slow.
Does pyglet work on Mac?
Pyglet is a powerful yet simple Python library for creating games and other visually rich applications on Windows, Mac OS X, and Linux. Windowing, UI event handling, joysticks, OpenGL graphics, image and video loading, and sound and music playback are all supported.
Conclusion
In this article, we saw the theoretical and practical implementation of pyglet.clock in game development.
We hope that this blog has helped you enhance your knowledge regarding pyglet.clock in game development. If you would like to learn more about Pyglet Library, Why is Pyglet used, Asteroids game using Pyglet, and more, check out our articles on Coding Ninjas Studio. Do upvote our blog to help other ninjas grow. Happy Coding!
