Python Pyglet Tutorial | Pyglet Module In Python Tutorial

Chapter 180: Pyglet

Pyglet is a Python module used for visuals and sound. It has no dependencies on other modules. See [pyglet.org][1] for the official information. [1]: https://pyglet.org

Section 180.1: Installation of Pyglet

Install Python, go into the command line and type:

Python 2:

pip install pyglet

Python 3:

pip3 install pyglet

Section 180.2: Hello World in Pyglet

import pyglet
window = pyglet.window.Window()
label = pyglet.text.Label('Hello, world',
font_name='Times New Roman',
font_size=36,
x=window.width//2, y=window.height//2,
anchor_x='center', anchor_y='center')
@window.event
def on_draw():
window.clear()
label.draw()
pyglet.app.run()

Section 180.3: Playing Sound in Pyglet

sound = pyglet.media.load(sound.wav)
sound.play()

Section 180.4: Using Pyglet for OpenGL

import pyglet
from pyglet.gl import *
win = pyglet.window.Window()
@win.event()
def on_draw():

OpenGL goes here. Use OpenGL as normal.

pyglet.app.run()

Section 180.5: Drawing Points Using Pyglet and OpenGL

import pyglet
from pyglet.gl import *

GoalKicker.com – Python® Notes for Professionals 698

win = pyglet.window.Window()
glClear(GL_COLOR_BUFFER_BIT)
@win.event
def on_draw():
glBegin(GL_POINTS)
glVertex2f(x, y) #x is desired distance from left side of window, y is desired distance from bottom of window

make as many vertexes as you want

glEnd
To connect the points, replace GL_POINTS with GL_LINE_LOOP.

Leave a Comment