| 1 |
Keeping track of time |
| 2 |
===================== |
| 3 |
|
| 4 |
pyglet's :py:mod:`~pyglet.clock` module allows you to schedule functions |
| 5 |
to run periodically, or for one-shot future execution. |
| 6 |
|
| 7 |
.. _guide_calling-functions-periodically: |
| 8 |
|
| 9 |
Calling functions periodically |
| 10 |
------------------------------ |
| 11 |
|
| 12 |
As discussed in the :ref:`programming-guide-eventloop` section, pyglet |
| 13 |
applications begin execution by entering into an application event loop:: |
| 14 |
|
| 15 |
pyglet.app.run() |
| 16 |
|
| 17 |
Once called, this function doesn't return until the application windows have |
| 18 |
been closed. This may leave you wondering how to execute code while the |
| 19 |
application is running. |
| 20 |
|
| 21 |
Typical applications need to execute code in only three circumstances: |
| 22 |
|
| 23 |
* A user input event (such as a mouse movement or key press) has been |
| 24 |
generated. In this case the appropriate code can be attached as an |
| 25 |
event handler to the window. |
| 26 |
* An animation or other time-dependent system needs to update the position |
| 27 |
or parameters of an object. We'll call this a "periodic" event. |
| 28 |
* A certain amount of time has passed, perhaps indicating that an |
| 29 |
operation has timed out, or that a dialog can be automatically dismissed. |
| 30 |
We'll call this a "one-shot" event. |
| 31 |
|
| 32 |
To have a function called periodically, for example, once every 0.1 seconds:: |
| 33 |
|
| 34 |
def update(dt): |
| 35 |
# ... |
| 36 |
|
| 37 |
pyglet.clock.schedule_interval(update, 0.1) |
| 38 |
|
| 39 |
The `dt`, or `delta time` parameter gives the number of "wall clock" seconds |
| 40 |
elapsed since the last call of this function, (or the time the function was |
| 41 |
scheduled, if it's the first period). Due to latency, load and timer |
| 42 |
imprecision, this might be slightly more or less than the requested interval. |
| 43 |
Please note that the `dt` parameter is always passed to scheduled functions, |
| 44 |
so be sure to expect it when writing functions even if you don't need to |
| 45 |
use it. |
| 46 |
|
| 47 |
Scheduling functions with a set interval is ideal for animation, physics |
| 48 |
simulation, and game state updates. pyglet ensures that the application does |
| 49 |
not consume more resources than necessary to execute the scheduled functions |
| 50 |
on time. |
| 51 |
|
| 52 |
Rather than "limiting the frame rate", as is common in other toolkits, simply |
| 53 |
schedule all your update functions for no less than the minimum period your |
| 54 |
application or game requires. For example, most games need not run at more |
| 55 |
than 60Hz (60 times a second) for imperceptibly smooth animation, so the |
| 56 |
interval given to :py:func:`~pyglet.clock.schedule_interval` would be |
| 57 |
``1/60.0`` (or more). |
| 58 |
|
| 59 |
If you are writing a benchmarking program or otherwise wish to simply run at |
| 60 |
the highest possible frequency, use `schedule`. This will call the function |
| 61 |
as frequently as possible (and will likely cause heavy CPU usage):: |
| 62 |
|
| 63 |
def benchmark(dt): |
| 64 |
# ... |
| 65 |
|
| 66 |
pyglet.clock.schedule(benchmark) |
| 67 |
|
| 68 |
.. note:: By default pyglet window buffer swaps are synchronised to the display refresh |
| 69 |
rate, so you may also want to disable vsync if you are running a benchmark. |
| 70 |
|
| 71 |
For one-shot events, use :py:func:`~pyglet.clock.schedule_once`:: |
| 72 |
|
| 73 |
def dismiss_dialog(dt): |
| 74 |
# ... |
| 75 |
|
| 76 |
# Dismiss the dialog after 5 seconds. |
| 77 |
pyglet.clock.schedule_once(dismiss_dialog, 5.0) |
| 78 |
|
| 79 |
To stop a scheduled function from being called, including cancelling a |
| 80 |
periodic function, use :py:func:`pyglet.clock.unschedule`. This could be |
| 81 |
useful if you want to start running a function on schedule when a user provides |
| 82 |
a certain input, and then unschedule it when another input is received. |
| 83 |
|
| 84 |
|
| 85 |
Sprite movement techniques |
| 86 |
-------------------------- |
| 87 |
|
| 88 |
As mentioned above, every scheduled function receives a `dt` parameter, |
| 89 |
giving the actual "wall clock" time that passed since the previous invocation. |
| 90 |
This parameter can be used for numerical integration. |
| 91 |
|
| 92 |
For example, a non-accelerating particle with velocity ``v`` will travel |
| 93 |
some distance over a change in time ``dt``. This distance is calculated as |
| 94 |
``v * dt``. Similarly, a particle under constant acceleration ``a`` will have |
| 95 |
a change in velocity of ``a * dt``. |
| 96 |
|
| 97 |
The following example demonstrates a simple way to move a sprite across the |
| 98 |
screen at exactly 10 pixels per second:: |
| 99 |
|
| 100 |
sprite = pyglet.sprite.Sprite(image) |
| 101 |
sprite.dx = 10.0 |
| 102 |
|
| 103 |
def move_sprite(dt): |
| 104 |
sprite.x += sprite.dx * dt |
| 105 |
|
| 106 |
pyglet.clock.schedule_interval(move_sprite, 1/60.0) # update at 60Hz |
| 107 |
|
| 108 |
This is a robust technique for simple sprite movement, as the velocity will |
| 109 |
remain constant regardless of the speed or load of the computer. |
| 110 |
|
| 111 |
Some examples of other common animation variables are given in the table |
| 112 |
below. |
| 113 |
|
| 114 |
.. list-table:: |
| 115 |
:header-rows: 1 |
| 116 |
|
| 117 |
* - Animation parameter |
| 118 |
- Distance |
| 119 |
- Velocity |
| 120 |
* - Rotation |
| 121 |
- Degrees |
| 122 |
- Degrees per second |
| 123 |
* - Position |
| 124 |
- Pixels |
| 125 |
- Pixels per second |
| 126 |
* - Keyframes |
| 127 |
- Frame number |
| 128 |
- Frames per second |
| 129 |
|
| 130 |
|
| 131 |
Displaying the frame rate |
| 132 |
------------------------- |
| 133 |
|
| 134 |
A simple way to profile your application performance is to display the frame |
| 135 |
rate while it is running. Printing it to the console is not ideal as this |
| 136 |
will have a severe impact on performance. pyglet provides the |
| 137 |
:py:class:`~pyglet.window.FPSDisplay` class for displaying the frame rate |
| 138 |
with very little effort:: |
| 139 |
|
| 140 |
fps_display = pyglet.window.FPSDisplay(window=window) |
| 141 |
|
| 142 |
@window.event |
| 143 |
def on_draw(): |
| 144 |
window.clear() |
| 145 |
fps_display.draw() |
| 146 |
|
| 147 |
By default the frame rate will be drawn in the bottom-left corner of the |
| 148 |
window in a semi-translucent large font. |
| 149 |
See the :py:class:`~pyglet.window.FPSDisplay` documentation for details |
| 150 |
on how to customise this, or even display another clock value (such as |
| 151 |
the current time) altogether. |
| 152 |
|
| 153 |
|
| 154 |
User-defined clocks |
| 155 |
------------------- |
| 156 |
|
| 157 |
The default clock used by pyglet uses the system clock to determine the time |
| 158 |
(i.e., ``time.time()``). Separate clocks can be created, however, allowing |
| 159 |
you to use another time source. This can be useful for implementing a |
| 160 |
separate "game time" to the real-world time, or for synchronising to a network |
| 161 |
time source or a sound device. |
| 162 |
|
| 163 |
Each of the ``clock_*`` functions are aliases for the methods on a global |
| 164 |
instance of :py:class:`~pyglet.clock.Clock`. You can construct or subclass |
| 165 |
your own :py:class:`~pyglet.clock.Clock`, which can then maintain its own |
| 166 |
schedule and framerate calculation. |
| 167 |
See the class documentation for more details. |