Introduction
In this blog, we will discuss keeping track of time in pyglet. But first, we need to know what is pyglet? So, pyglet is a Python library that provides an object-oriented application programming interface for developing games and other multimedia applications. Pyglet is a BSD-licensed program that runs on Microsoft Windows, macOS, and Linux.
In this blog, we will learn how to keep track of time in pyglet, so let's dive right in.
Keeping track of time
The clock module in pyglet allows you to plan functions to run frequently or once in the future. There are also some valuable utilities for calculating and displaying the frame rate of the program. We will look at some useful functions to keep track of time in pyglet in the blog.
Calling functions periodically
An application event loop is used to execute pyglet applications:
pyglet.app.run()
This function does not return until all program windows have been closed after being called. You might be wondering how to run code while the application is executing. Typical apps only need to run code in three situations:
-
A user input event has been generated (such as a mouse movement or key push). In this situation, the relevant code can be connected to the window as an event handler.
-
The location or parameters of an object must be updated in animation or another time-dependent system. This will be referred to as a "periodic" event.
- A particular time has passed, signaling that an action has been completed or dialogue can be dismissed automatically. This will be a "one-shot" event.
To have a function called periodically, such as once every 0.1 seconds:
def update(dt):
# ...
pyglet.clock.schedule_interval(update, 0.1)
The dt, or delta time, the parameter indicates how many "wall clock" seconds had passed since the function's last call (or if it's the first period when the function was scheduled). This may be more or less than the required interval due to latency, load, and timer inaccuracy. Please note that the dt parameter is continuously supplied to scheduled functions, so expect it while writing functions even if you don't need it.
Scheduling functions with a specified interval is ideal for animation, physics simulation, and game state updates. Pyglet ensures that the program uses no more resources than are required to complete the scheduled tasks on time.
Schedule all your update functions for at least the time your application or game requires, this method is contrary to what other toolkits prefer( they use "limiting the frame rate"). Let's take an example to suppose we want to limit the frames rendered per second to 60. Hence the interval given to schedule interval() would be 1/60.0. (or more).
def standard(dt):
# ...
pyglet.clock.schedule(standard)
If you're conducting a benchmark, you might wish to disable sync because pyglet window buffer swaps are synchronized with the display refresh rate by default.
Use schedule_once() for one-time events:
def remove_dialog(dt):
# ...
# Remove the dialog after 5 seconds.
pyglet.clock.schedule_once(remove_dialog, 5.0)
Use pyglet. Clock.unschedule to prevent a scheduled function from being called, such as canceling a periodic function (). This could be beneficial if you want to start a function on a schedule when a user enters a specific value and then unscheduled it when another value is entered.
Sprite movement techniques
Every scheduled function receives a dt parameter, which specifies the amount of time since the previous invocation. This parameter can be used to integrate numerically.
A non-accelerating particle with velocity v, for example, will move a certain distance over a change in time dt. This distance is determined using the formula v*dt. Similarly, a particle moving at a constant speed will experience a velocity change of a*dt.
The code below depicts an easy and convenient way to move the sprite along with the screen at exactly 10 pixes per second:
sprite = pyglet.sprite.Sprite(image)
sprite.dx = 10.0
def upgrade(dt):
sprite.x += sprite.dx * dt
pyglet.clock.schedule_interval(upgrade, 1/60.0)
# upgrade at 60Hz
This is a reliable method for simple sprite movement because the velocity remains constant, independent of the computer's speed or load.
