Python pyglet Library

2021.08.05

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

Introduction to 'pyglet' Library

Pyglet is easy to use but powerful library for developing visually rich GUI applications like games, multimedia etc on Windows, Mac OS and Linux. This library is created purely in Python and it supports many features like windowing, user interface event handling, Joysticks, OpenGL graphics, loading images, and videos, and playing sounds and music. pyglet is provided under the BSD open-source license, allowing you to use it for both commercial and other open-source projects with very little restriction.

Features:

  1. No external dependencies or installation requirements: For development of most of the applications, pyglet does not need any external libraries or installation of packages which helps in simplifying distribution and installation
  2. Take advantage of multiple windows and multi-monitor desktops: Sometimes multi-monitor desktop setups are use for game development and pyglet is designed in such a way that it lets you use as many windows as needed and also allows fullscreen games and application across multiple screens.
  3. Load images, sound, music and video in almost any format
  4. pyglet is provided under the BSD open-source license, allowing you to use it for both commercial and other open-source projects with very little restriction.
  5. It supports development in Python 2 as well as Python 3

Installation:

Since pyglet is created entirely in Python, no special tasks are needed to be done for installation. pyglet can be install in many ways, the most basic installation requires typing the following command in your terminal:

pip install pyglet

Basic use of Library with sample program:

Before we do anything else in pyglet we’ll want to make a window. A window is literally an empty box that appears on the screen:

import pyglet

# create a pyglet window

win = pyglet.window.Window()

# run our pyglet app, and show the window

pyglet.app.run()

After running the above code it pop's up with a black window.

import pyglet

# create a pyglet window

win = pyglet.window.Window()

# override the method that draws when the window loads

@win.event

def on_draw():

print('Draw triggered!')

# run our pyglet app, and show the window

pyglet.app.run()

 

Let's take the black window and overriding an event method. These events are functions that trigger when a certain thing happens to/in this window. We will be overriding the on_draw function on the window object we created. This event is triggered once at the beginning when the window is loaded.

We override this function by using a decorator, @win.event, that is builtin to the window object we created.

Once we run the above code it should produce a black window like before and also output a printed Draw triggered! on the command line. This confirms for us that on_draw is triggered once on the window load.

 

Now let me draw a line in our window. The way drawing lines works in pyglet is that you grab an opengl context and then create vertices in this context, the vertices then get connected, in the order you declared them, in the way the context specifies. As  glBegin(GL_LINES) used creates a context where we can draw. Every two vertices we create in this context will be linked together as a line. We use the glVertex3f function to make a vertex in 3 dimensions taking 3 floats. The first 2 floats are x,y coordinates on the scale of the size of the window (if your window is 400x400 and example for the first 2 values could be 100.0, 100.0, the z value is a value between -1 and 1 that represents depth. Let’s see the output:

import pyglet

# import all of opengl functions

from pyglet.gl import *

win = pyglet.window.Window()

@win.event

def on_draw():

# create a line context

glBegin(GL_LINES)

# create a line, x,y,z

glVertex3f(100.0,100.0,0.25)

glVertex3f(200.0,300.0,-0.75)

glEnd()

pyglet.app.run()

Now let's make a Line Spin.

import math

import pyglet

import numpy as np

from pyglet.gl import *

win = pyglet.window.Window()

# get all the points in a circle centered at 0.

def PointsInCircum(r, n=25, pi=3.14):

return [(math.cos(2*pi/n*x)*r,math.sin(2*pi/n*x)*r) for x in range(0,n+1)]

pts = np.array(PointsInCircum(20))

# function that increments to the next

# point along a circle

frame = 0

def update_frame(x, y):

global frame

if frame == None or frame == pts.shape[0]-1:

frame = 0

else:

frame += 1

@win.event

def on_draw():

# clear the screen

glClear(GL_COLOR_BUFFER_BIT)

# draw the next line

# in the circle animation

# circle centered at 100,100,0 = x,y,z

glBegin(GL_LINES)

glVertex3f(100,100,0)

glVertex3f(pts[frame][1]+100,pts[frame][0]+100,0)

glEnd()

# every 1/10 th get the next frame

pyglet.clock.schedule(update_frame, 1/10.0)

pyglet.app.run()

Note:

 if you put a number outside the range [-1,1] for z the vertex and thus the line will not appear.