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.
- 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
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
- Begin the program by importing the library:
- Using the default constructor, we can create windows which display the app contents:
new_window = pyglet.window.Window()
- 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')
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()
- Finally to run the app the following line is appended at the end of the source code:
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course