Table of contents
1.
Introduction
2.
pyglet.clock
2.1.
Measuring time
3.
Scheduling
4.
Using multiple clocks
5.
Classes and Functions
5.1.
class Clock(time_function=<built-in function perf_counter>)
5.2.
get_default()
5.3.
get_fps()
5.4.
get_sleep_time(sleep_idle)
5.5.
schedule(func, *args, **kwargs)
5.6.
schedule_interval_soft(func, interval, *args, **kwargs)
5.7.
schedule_once(func, delay, *args, **kwargs)
5.8.
set_default(default)
5.9.
tick(poll=False)
5.10.
unschedule(func)
6.
FAQs
6.1.
Is pyglet faster than pygame?
6.2.
Is Pyglet any good?
6.3.
What is pyglet used for?
6.4.
Is Pyglet slow?
6.5.
Does pyglet work on Mac?
7.
Conclusion
Last Updated: Mar 27, 2024

pyglet.clock

Author Toohina Barua
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

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, joysticks, OpenGL graphics, loading images and videos, and sound and music playback are all supported. pyglet is compatible with Windows, Mac OS X, and Linux.
In this article, we are going to talk about pyglet.clock for game development. So let us dive in!

pyglet.clock

Scheduling of precise framerate calculation functions.

Measuring time

Most games' basic requirements can be met by combining the tick and get_fps functions:

from pyglet import clock
while True:
    dt = clock.tick()
    # ... update and render ...
    print(f"FPS is {clock.get_fps()}")

 

The dt value returned is the number of seconds since the last "tick" (as a float).
The get fps function takes an average of the framerate over a 1 second sliding window. (The reciprocal of dt can be used to calculate the instantaneous framerate.)
Always keep an eye on the time!

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 LibraryWhy is Pyglet usedAsteroids game using Pyglet, and more, check out our articles on Coding Ninjas Studio. Do upvote our blog to help other ninjas grow. Happy Coding!

 

Live masterclass