Pyglet

pyglet is a library for the Python programming language that provides an object-oriented application programming interface for the creation of games and other multimedia applications. pyglet runs on Microsoft Windows, macOS, and Linux; it is released under the BSD Licence. pyglet was first created by Alex Holkner.

It supports windowed and full-screen operations as well as multiple monitors. Images, video, and sound files in a range of formats can be done natively, with more additional capabilities supplied by the optional AVbin plugin, which uses the Libav package to provide support for audio formats including MP3, Ogg/Vorbis, and Windows Media Audio, and video formats such as DivX, MPEG-2, H.264, WMV, and XviD.

An advantage of pyglet over many other libraries is that it requires no external dependencies, and uses the ctypes library, a pure-Python C compiler. It builds on OpenGL.

Comparison to other libraries

Pygame

pygame is another library used for making games and is much more widely known than pyglet. This is what it would take to display the window shown in the example. It takes more lines of code and also is harder to understand.

Compared to pyglet, pygame is not object-oriented and has less functionality. For example, there is no rich and formatted text, fast drawing commands, etc. The graphics and images have to be drawn again and again and can not be cached. This is one of the performance features of object-oriented programming, where most of the steps do not have to be repeated

import pygame

screen = pygame.display.set_mode((640, 480))

pygame.display.set_caption("Hello world!")

running = True

while running:
    for event in pygame.event.get():
        if event.type in pygame.QUIT:
            running = False

pygame.quit()

Features

As a multimedia library, pyglet comes with batteries included. Using OpenGL gives it speed benefits, and is also written entirely in Python, meaning no external dependencies that have to be installed. The ffmpeg library can be optionally installed to support more audio formats.

Text display and formatting

  • Rich text formatting (bold, italic, underline, color change, background color, indent, lists) (pyglet.text.formats)
  • Built-in layouts to support editable text
  • Carets (pyglet.text.caret.Caret)
  • HTML support (pyglet.text.layout.IncrementalTextLayout)

Image and sprite work

  • Fast image processing and rendering
  • Built-in sprites (pyglet.sprite)
  • Animated images (*.gif)

Graphics

  • OpenGL shaders supported
  • Simple built-in shapes (rectangles, circles, triangles) (pyglet.shapes)
  • Batched rendering (pyglet.graphics.Batch)
  • 3D model rendering

Events and file system

  • Resource management (pyglet.resource)
  • Clock for processing events and time (pyglet.clock.Clock)
  • Window events (pyglet.window.Window)
  • Event dispatchers for your own event dispatching (pyglet.event.EventDispatcher)
  • Context management

These features make pyglet much faster than many other multimedia libraries. Batched rendering is a technique to draw multiple objects with the same amount of time. Batches support Sprites, images, and TextLayouts, which are the basis for all labels and text. Text functions are implemented as well. Multi-level lists are supported, and can be made using HTML. Different parts of the displayed document can have different styles. A caret is built-in for support for editing text. It resembles many of the features of a UI text input caret.

Example

from pyglet.window import Window
from pyglet.app import run

window = Window(caption="Hello world!", width=640, height=480)

run()

In this example, lines 1-2 import the pyglet module's necessary components. Line 4 creates a window, and line 6 calls pyglet to run its event loop. Optionally an update rate (in frames per second) can be specified in a rate parameter.

See also

This article is issued from Wikipedia. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.