Introduction to pyglet library for game development in Python

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

  • 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
  • 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.
  • Load images, sound, music and video in almost any format
  • 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.
  • 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

Example:



filter_none

edit
close

play_arrow

link
brightness_4
code

import pyglet
  
new_window = pyglet.window.Window()
  
label = pyglet.text.Label('Hello, World !',
                          font_name ='Cooper',
                          font_size = 16,
                          x = new_window.width//2
                          y = new_window.height//2,
                          anchor_x ='center'
                          anchor_y ='center')
  
@new_window.event
def on_draw():
    new_window.clear()
    label.draw()
  
pyglet.app.run()

chevron_right


Output:

Hello-world-pyglet

Explanation

  1. Begin the program by importing the library:
     import pyglet 
  2. Using the default constructor, we can create windows which display the app contents:
     new_window = pyglet.window.Window() 
  3. Label is created to display the text Hello, World!:

    label = pyglet.text.Label('Hello, World !',
                              font_name ='Cooper',
                              font_size = 16,
                              x = new_window.width//2, 
                              y = new_window.height//2,
                              anchor_x ='center', 
                              anchor_y ='center')
    
  4. The on_draw() event is used to draw its contents on to the window.The pyglet library provides several ways to attach event handlers to objects; a simple way is to use a decorator:

    @new_window.event
    def on_draw():
        new_window.clear()
        label.draw()
    
  5. Finally to run the app the following line is appended at the end of the source code:
    pyglet.app.run()
    



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.