or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

3d-models.mdapp-clock.mdaudio-video.mdgraphics-rendering.mdgui.mdimages-textures.mdindex.mdinput-devices.mdmath.mdopengl.mdresource-management.mdsprites-shapes.mdtext-rendering.mdwindowing.md
IMPROVEMENTS.mdtile.json

tessl/pypi-pyglet

Cross-platform windowing and multimedia library for Python with OpenGL graphics, event handling, and audio/video playback

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/pyglet@2.1.x

To install, run

npx @tessl/cli install tessl/pypi-pyglet@2.1.0

index.mddocs/

Pyglet 2.1.9 - Usage Specification

Cross-platform windowing and multimedia library for Python with OpenGL graphics, event handling, and audio/video playback.

Installation: pip install pyglet Requirements: Python 3.8+, OpenGL 3.3+ License: BSD

Quick Start

import pyglet

window = pyglet.window.Window(800, 600)

@window.event
def on_draw():
    window.clear()

pyglet.app.run()

Module Navigator

Use this guide to quickly find the right module for your task:

I need to...

Window & Display:

  • Create a window → windowing.md
  • Handle keyboard/mouse input → windowing.md
  • Go fullscreen, resize, multiple monitors → windowing.md

2D Graphics:

Text:

Images & Textures:

Audio & Video:

Timing & Scheduling:

Math:

Input Devices:

GUI:

  • Buttons, sliders, text entry → gui.md

Resource Management:

3D:

Core Patterns

Pattern 1: Simple 2D Game

import pyglet
from pyglet.window import key

# Setup
window = pyglet.window.Window(800, 600)
batch = pyglet.graphics.Batch()

# Load resources
player_img = pyglet.image.load('player.png')
player = pyglet.sprite.Sprite(player_img, x=400, y=300, batch=batch)

# Input
keys = key.KeyStateHandler()
window.push_handlers(keys)

# Update
def update(dt):
    speed = 200 * dt
    if keys[key.LEFT]: player.x -= speed
    if keys[key.RIGHT]: player.x += speed

pyglet.clock.schedule_interval(update, 1/60)

# Render
@window.event
def on_draw():
    window.clear()
    batch.draw()

pyglet.app.run()

Pattern 2: Resource Loading

import pyglet

# Set asset paths
pyglet.resource.path = ['assets/images', 'assets/sounds']
pyglet.resource.reindex()

# Load and cache automatically
player_img = pyglet.resource.image('player.png')
jump_sound = pyglet.resource.media('jump.wav', streaming=False)

Pattern 3: Efficient Rendering

# Always use batching for multiple objects
batch = pyglet.graphics.Batch()

# Use groups for layering
background = pyglet.graphics.Group(order=0)
foreground = pyglet.graphics.Group(order=1)

# Add objects to batch
bg_sprite = pyglet.sprite.Sprite(bg_img, batch=batch, group=background)
player = pyglet.sprite.Sprite(player_img, batch=batch, group=foreground)

# Draw everything at once
@window.event
def on_draw():
    window.clear()
    batch.draw()  # Single call renders all

Decision Trees

When to use streaming vs static loading?

Audio file size:
├─ < 1 MB (SFX)      → streaming=False
└─ > 1 MB (music)    → streaming=True

How to handle input?

Input type:
├─ Continuous (movement)  → KeyStateHandler + schedule
├─ Discrete (jump, shoot) → @window.event on_key_press
└─ Game controller        → pyglet.input.get_controllers()

When to use schedule() vs schedule_interval()?

Update type:
├─ Every frame (smooth animation)    → schedule(func)
├─ Fixed rate (physics, game logic)  → schedule_interval(func, 1/60)
└─ Occasional (spawn, autosave)      → schedule_interval(func, delay)

Performance Best Practices

  1. Always use Batch for multiple sprites/shapes - 10-100x faster
  2. Use Groups to minimize OpenGL state changes
  3. Texture atlases for sprite sheets - reduces texture binding
  4. streaming=False for small audio, True for music
  5. Set visible=False instead of deleting offscreen objects
  6. Preallocate objects in init, don't create in update loop
  7. Use schedule_interval() for physics (consistent timing)

Common Pitfalls

  1. Origin is bottom-left (not top-left) - y increases upward
  2. Always multiply by dt for movement - ensures frame-rate independence
  3. Call window.clear() in on_draw - forgetting causes artifacts
  4. Use batch.draw() not sprite.draw() - individual draws are very slow
  5. Unschedule functions when done - prevents memory leaks
  6. Color values are 0-255 (not 0.0-1.0 floats)
  7. Enable vsync for smooth rendering - Window(vsync=True)

Architecture Example

import pyglet

class Game:
    def __init__(self):
        self.window = pyglet.window.Window(800, 600, vsync=True)
        self.batch = pyglet.graphics.Batch()
        self.keys = pyglet.window.key.KeyStateHandler()
        self.window.push_handlers(self.keys)

        # Setup
        self.load_resources()
        self.setup_entities()

        # Events
        self.window.event(self.on_draw)
        pyglet.clock.schedule_interval(self.update, 1/60)

    def load_resources(self):
        pyglet.resource.path = ['assets']
        pyglet.resource.reindex()
        # Load images, sounds, etc

    def setup_entities(self):
        # Create sprites, shapes, etc
        pass

    def update(self, dt):
        # Game logic
        pass

    def on_draw(self):
        self.window.clear()
        self.batch.draw()

    def run(self):
        pyglet.app.run()

if __name__ == '__main__':
    Game().run()

Module Reference

ModulePurposeLink
pyglet.appEvent loop, application lifecycleapp-clock.md
pyglet.windowWindow creation, input eventswindowing.md
pyglet.sprite2D sprite renderingsprites-shapes.md
pyglet.shapesGeometric primitivessprites-shapes.md
pyglet.imageImage loading, texturesimages-textures.md
pyglet.textText rendering, fontstext-rendering.md
pyglet.mediaAudio/video playbackaudio-video.md
pyglet.graphicsLow-level rendering, shadersgraphics-rendering.md
pyglet.mathVector/matrix mathmath.md
pyglet.inputGame controllers, joysticksinput-devices.md
pyglet.guiUI widgetsgui.md
pyglet.resourceResource managementresource-management.md
pyglet.model3D model loading3d-models.md
pyglet.glOpenGL bindingsopengl.md

Version Info

import pyglet
print(pyglet.version)  # '2.1.9'

Note for AI Agents: This index is designed for quick navigation. For detailed API references, class signatures, and comprehensive examples, see the module-specific documentation files linked above.