| 1 |
.. _quickstart: |
| 2 |
|
| 3 |
Writing a pyglet application |
| 4 |
============================ |
| 5 |
|
| 6 |
Getting started with a new library or framework can be daunting, especially |
| 7 |
when presented with a large amount of reference material to read. |
| 8 |
This chapter gives a very quick introduction to pyglet without going into |
| 9 |
too much detail. |
| 10 |
|
| 11 |
.. Note:: |
| 12 |
If you intend to use OpenGL ES with pyglet on devices like the Raspberry |
| 13 |
Pi, please read the :ref:`programming-guide-opengles` section first. |
| 14 |
|
| 15 |
Hello, World |
| 16 |
------------ |
| 17 |
|
| 18 |
We'll begin with the requisite "Hello, World" introduction. This program will |
| 19 |
open a window with some text in it, and wait to be closed. You can find the |
| 20 |
entire program in the `examples/programming_guide/hello_world.py` file. |
| 21 |
|
| 22 |
Begin by importing the :mod:`pyglet` package:: |
| 23 |
|
| 24 |
import pyglet |
| 25 |
|
| 26 |
Create a :class:`pyglet.window.Window` by calling its default constructor. |
| 27 |
The window will be visible as soon as it's created, and will have reasonable |
| 28 |
default values for all its parameters:: |
| 29 |
|
| 30 |
window = pyglet.window.Window() |
| 31 |
|
| 32 |
To display the text, we'll create a :class:`~pyglet.text.Label`. Keyword |
| 33 |
arguments are used to set the font, position and anchorage of the label:: |
| 34 |
|
| 35 |
label = pyglet.text.Label('Hello, world', |
| 36 |
font_name='Times New Roman', |
| 37 |
font_size=36, |
| 38 |
x=window.width//2, y=window.height//2, |
| 39 |
anchor_x='center', anchor_y='center') |
| 40 |
|
| 41 |
The Window dispatches an :meth:`~pyglet.window.Window.on_draw` event whenever |
| 42 |
it's ready to redraw its contents. pyglet provides several ways to attach event |
| 43 |
handlers to objects; a simple way is to use a decorator:: |
| 44 |
|
| 45 |
@window.event |
| 46 |
def on_draw(): |
| 47 |
window.clear() |
| 48 |
label.draw() |
| 49 |
|
| 50 |
Within the above :meth:`~pyglet.window.Window.on_draw` handler, the window is |
| 51 |
cleared to the default background color (black), and the label is drawn. |
| 52 |
|
| 53 |
Finally, call:: |
| 54 |
|
| 55 |
pyglet.app.run() |
| 56 |
|
| 57 |
This will enter pyglet's default event loop, and let pyglet respond to |
| 58 |
application events such as the mouse and keyboard. |
| 59 |
Your event handlers will now be called as required, and the |
| 60 |
:func:`~pyglet.app.run` method will return only when all application |
| 61 |
windows have been closed. |
| 62 |
|
| 63 |
If you are coming from another library, you may be used to writing your |
| 64 |
own event loop. This is possible to do with pyglet as well, but it is |
| 65 |
generally not necessary; see :ref:`programming-guide-eventloop` for details. |
| 66 |
|
| 67 |
Image viewer |
| 68 |
------------ |
| 69 |
|
| 70 |
Most games and applications will need to load and display images on the |
| 71 |
screen. In this example we'll load an image from the application's |
| 72 |
directory and display it within the window:: |
| 73 |
|
| 74 |
import pyglet |
| 75 |
|
| 76 |
window = pyglet.window.Window() |
| 77 |
image = pyglet.resource.image('kitten.jpg') |
| 78 |
|
| 79 |
@window.event |
| 80 |
def on_draw(): |
| 81 |
window.clear() |
| 82 |
image.blit(0, 0) |
| 83 |
|
| 84 |
pyglet.app.run() |
| 85 |
|
| 86 |
We used the :func:`~pyglet.resource.image` function of :mod:`pyglet.resource` |
| 87 |
to load the image, which automatically locates the file relative to the source |
| 88 |
file (rather than the working directory). To load an image not bundled with |
| 89 |
the application (for example, specified on the command line), you would use |
| 90 |
:func:`pyglet.image.load`. |
| 91 |
|
| 92 |
The :meth:`~pyglet.image.AbstractImage.blit` method draws the image. The |
| 93 |
arguments ``(0, 0)`` tell pyglet to draw the image at pixel coordinates 0, |
| 94 |
0 in the window (the lower-left corner). |
| 95 |
|
| 96 |
The complete code for this example is located in |
| 97 |
`examples/programming_guide/image_viewer.py`. |
| 98 |
|
| 99 |
Handling mouse and keyboard events |
| 100 |
---------------------------------- |
| 101 |
|
| 102 |
So far the only event used is the :meth:`~pyglet.window.Window.on_draw` |
| 103 |
event. To react to keyboard and mouse events, it's necessary to write and |
| 104 |
attach event handlers for these events as well:: |
| 105 |
|
| 106 |
import pyglet |
| 107 |
|
| 108 |
window = pyglet.window.Window() |
| 109 |
|
| 110 |
@window.event |
| 111 |
def on_key_press(symbol, modifiers): |
| 112 |
print('A key was pressed') |
| 113 |
|
| 114 |
@window.event |
| 115 |
def on_draw(): |
| 116 |
window.clear() |
| 117 |
|
| 118 |
pyglet.app.run() |
| 119 |
|
| 120 |
Keyboard events have two parameters: the virtual key `symbol` that was |
| 121 |
pressed, and a bitwise combination of any `modifiers` that are present (for |
| 122 |
example, the ``CTRL`` and ``SHIFT`` keys). |
| 123 |
|
| 124 |
The key symbols are defined in :mod:`pyglet.window.key`:: |
| 125 |
|
| 126 |
from pyglet.window import key |
| 127 |
|
| 128 |
@window.event |
| 129 |
def on_key_press(symbol, modifiers): |
| 130 |
if symbol == key.A: |
| 131 |
print('The "A" key was pressed.') |
| 132 |
elif symbol == key.LEFT: |
| 133 |
print('The left arrow key was pressed.') |
| 134 |
elif symbol == key.ENTER: |
| 135 |
print('The enter key was pressed.') |
| 136 |
|
| 137 |
See the :mod:`pyglet.window.key` documentation for a complete list |
| 138 |
of key symbols. |
| 139 |
|
| 140 |
Mouse events are handled in a similar way:: |
| 141 |
|
| 142 |
from pyglet.window import mouse |
| 143 |
|
| 144 |
@window.event |
| 145 |
def on_mouse_press(x, y, button, modifiers): |
| 146 |
if button == mouse.LEFT: |
| 147 |
print('The left mouse button was pressed.') |
| 148 |
|
| 149 |
The ``x`` and ``y`` parameters give the position of the mouse when the button |
| 150 |
was pressed, relative to the lower-left corner of the window. |
| 151 |
|
| 152 |
There are more than 20 event types that you can handle on a window. An easy |
| 153 |
way to find the event names and parameters you need is to add the following |
| 154 |
lines to your program:: |
| 155 |
|
| 156 |
event_logger = pyglet.window.event.WindowEventLogger() |
| 157 |
window.push_handlers(event_logger) |
| 158 |
|
| 159 |
This will cause all events received on the window to be printed to the |
| 160 |
console. |
| 161 |
|
| 162 |
An example program using keyboard and mouse events is in |
| 163 |
`examples/programming_guide/events.py` |
| 164 |
|
| 165 |
Playing sounds and music |
| 166 |
------------------------ |
| 167 |
|
| 168 |
pyglet makes it easy to play and mix multiple sounds together. |
| 169 |
The following example plays an MP3 file [#mp3]_:: |
| 170 |
|
| 171 |
import pyglet |
| 172 |
|
| 173 |
music = pyglet.resource.media('music.mp3') |
| 174 |
music.play() |
| 175 |
|
| 176 |
pyglet.app.run() |
| 177 |
|
| 178 |
As with the image loading example presented earlier, |
| 179 |
:func:`~pyglet.resource.media` locates the sound file in the application's |
| 180 |
directory (not the working directory). If you know the actual filesystem path |
| 181 |
(either relative or absolute), use :func:`pyglet.media.load`. |
| 182 |
|
| 183 |
By default, audio is streamed when playing. This works well for longer music |
| 184 |
tracks. Short sounds, such as a gunfire shot used in a game, should instead be |
| 185 |
fully decoded in memory before they are used. This allows them to play more |
| 186 |
immediately and incur less of a CPU performance penalty. It also allows playing |
| 187 |
the same sound repeatedly without reloading it. |
| 188 |
Specify ``streaming=False`` in this case:: |
| 189 |
|
| 190 |
sound = pyglet.resource.media('shot.wav', streaming=False) |
| 191 |
sound.play() |
| 192 |
|
| 193 |
The `examples/media/media_player.py` example demonstrates playback of streaming |
| 194 |
audio and video using pyglet. The `examples/media/noisy/noisy.py` example |
| 195 |
demonstrates playing many short audio samples simultaneously, as in a game. |
| 196 |
|
| 197 |
.. [#mp3] MP3 and other compressed audio formats require FFmpeg to be installed. |
| 198 |
Uncompressed WAV files can be played without FFmpeg. |
| 199 |
|
| 200 |
|
| 201 |
Where to next? |
| 202 |
-------------- |
| 203 |
|
| 204 |
The examples above have shown you how to display something on the screen, |
| 205 |
and perform a few basic tasks. You're probably left with a lot of questions |
| 206 |
about these examples, but don't worry. The remainder of this programming guide |
| 207 |
goes into greater technical detail on many of pyglet's features. If you're |
| 208 |
an experienced developer, you can probably dive right into the sections that |
| 209 |
interest you. |
| 210 |
|
| 211 |
For new users, it might be daunting to read through everything all at once. |
| 212 |
If you feel overwhelmed, we recommend browsing through the beginnings of each |
| 213 |
chapter, and then having a look at a more in-depth example project. |
| 214 |
You can find an example of a 2D game in the :ref:`programming-guide-game` |
| 215 |
section. |
| 216 |
|
| 217 |
To write advanced 3D applications or achieve optimal performance in your 2D |
| 218 |
applications, you can also work with OpenGL directly. pyglet provides raw |
| 219 |
OpenGL bindings which give you direct access to the OpenGL libraries, which |
| 220 |
does require knowledge of `ctypes` to use effectively. To simplify things a |
| 221 |
bit, the `graphics` module provides higher level objects for the most common |
| 222 |
OpenGL constructs. The :ref:`guide_graphics` section goes into more detail. |
| 223 |
|
| 224 |
There are numerous examples of pyglet applications in the ``examples/`` |
| 225 |
directory of the documentation. If you get |
| 226 |
stuck, or have any questions, join us on the `Discord server <https://discord.gg/QXyegWe>`_! |