Table of contents
1.
Introduction
2.
pyglet.canvas
3.
get_display()
4.
class Display(name=None, x_screen=None)
4.1.
get_default_screen()
4.2.
get_screens()
4.3.
get_windows()
4.4.
name= None
4.5.
x_screen= None
5.
class Screen(display, x, y, width, height)
5.1.
get_best_config(template=None)
5.2.
get_closest_mode(width, height)
5.3.
get_matching_configs(template)
5.4.
get_mode()
5.5.
get_modes()
5.6.
restore_mode()
5.7.
set_mode(mode)
5.8.
display
5.9.
height
5.10.
width
5.11.
x
5.12.
y
6.
class Canvas(display)
7.
FAQs
7.1.
How do I close Pyglet on Windows?
7.2.
Which is better pygame or Unity?
7.3.
Is pygame easy?
7.4.
What is pyglet used for?
7.5.
Does pyglet work on Mac?
8.
Conclusion
Last Updated: Mar 27, 2024

pyglet.canvas

Author Toohina Barua
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Python's pyglet library is a cross-platform windowing and multimedia library for creating games and other visually rich applications. Windowing, user interface event handling, joysticks, OpenGL graphics, loading images and videos, and sound and music playback are all supported. pyglet is compatible with Windows, Mac OS X, and Linux.
In this article, we are going to talk about pyglet.canvas for game development. So let us dive in!

pyglet.canvas

pyglet.canvas is an API for Display and screen management.  
The content area of a pyglet.window is rendered on a Canvas, which could theoretically be an off-screen buffer. A window or the entire screen can be used. Canvases can currently only be made with windows (though windows can be set fullscreen). 
A Display must include windows and canvases. There is only one display on Windows and Mac OS X, which can be accessed using get display (). Multiple displays are supported by Linux, which correspond to discrete X11 display connections and screens. On Linux, get display() returns the default display and screen 0 (localhost:0.0); if a specific screen or display is required, Display can be directly instantiated. 
One or more screens are attached to a display. A Screen usually refers to a physically attached monitor, but a monitor or projector set up to clone another screen will not appear in the list. To get a list of attached screens, use Display.get screens(); these can then be queried for their sizes and virtual positions on the desktop. 
The current mode of a screen determines its size, which can be changed by the application;

get_display()

With get_display(), you can get the default display device. 
If a Display connection already exists, that display will be returned. A default Display is created and returned if none is specified. If there are multiple active display connections, an arbitrary one is returned.

class Display(name=None, x_screen=None)

A display device that can accommodate one or more screens.

get_default_screen()

Get the user's default screen based on their operating system preferences. 
The return type is of Screen. 

get_screens()

The get_screens() function helps to get the available screens.
One Display with multiple Screens is typical of a multi-monitor workstation. This method returns a list of screens that can be enumerated to determine which one should be displayed in full-screen mode.
The default screen will suffice for creating an OpenGL configuration.
The return type is list of Screen.

get_windows()

Using get_window() you can get the windows currently attached to this display.
The return type is sequence of Window.

name= None

It is the name of this display, if applicable. It is given in type str.

x_screen= None

The X11 screen number of this display, if applicable. It takes the type int.

class Screen(display, x, y, width, height)

Fullscreen windows are supported by this virtual monitor. 
The majority of screens are mapped to a physical display such as a monitor, television, or projector. Unless the window is made fullscreen, in which case the window will fill only that virtual screen, selecting a screen for a window has no effect. 
The current resolution of a screen is determined by its width and height attributes. The top-left corner of the screen's global location is determined by the x and y attributes. This is useful for determining whether screens are stacked on top of each other or next to each other. 
To get an instance of this class, call get screens() or get default screen().

get_best_config(template=None)

Get the best GL configuration you can.
In the template, you can specify any required attributes. NoSuchConfigException will be thrown if no configuration matches the template.

get_closest_mode(width, height)

Find the screen mode that best fits a specific size.
A larger mode is returned if no supported mode exactly matches the requested size; or None if no mode is large enough.

get_matching_configs(template)

Get a list of configurations that match a set of criteria.
Each returned config will have values equal to or greater than any attributes specified in the template. An empty list is returned if no configs satisfy the template.

get_mode()

This screen's current display mode is returned.

get_modes()

Get a list of the screen modes that this screen supports.

restore_mode()

Reset the screen mode to the user's preferred setting.

set_mode(mode)

This screen's display mode can be changed.
The mode must have been returned previously by get_mode() or get_modes ().

display

This screen is part of a display.

height

The screen's height in pixels.

width

The screen's width in pixels.

x

On the virtual desktop, the left edge of the screen.

y

On the virtual desktop, the top edge of the screen.

class Canvas(display)

Area for abstract drawing.
Internally, pyglet uses canvases to represent drawing areas, which can be in a window or full-screen.
The display is the Display this canvas was created on.

FAQs

How do I close Pyglet on Windows?

A window is a "heavyweight" object that consumes resources from the operating system. Windows can be set to appear as floating regions or to fill the entire screen (fullscreen). Although we can manually close a window by pressing the close button, there are times when we need to close a window programmatically.

Which is better pygame or Unity?

Build high-quality 3D and 2D games with Unity, then distribute them across mobile, desktop, VR/AR, consoles, and the Web to connect with loyal and enthusiastic players and customers; pygame: An open-source python programming language library for creating multimedia applications.

Is pygame easy?

Python is frequently recommended as the best "first programming language" to learn because of its simple syntax and gradual learning curve. As a result, a lot of new programmers start with Python. Pygame is a Python extension that follows Python's philosophy and aims to be simple to use.

What is pyglet used for?

Python's pyglet library is a cross-platform windowing and multimedia library for creating games and other visually rich applications. Windowing, user interface event handling, game controllers and joysticks, OpenGL graphics, image and video loading, and sound and music playback are all supported.

Does pyglet work on Mac?

On Windows, Mac OS X, and Linux, pyglet is a powerful yet simple Python library for creating games and other visually rich applications. Windowing, user interface event handling, joysticks, OpenGL graphics, loading images and videos, and sound and music playback are all supported.

Conclusion

In this article, we saw the theoretical and practical implementation of pyglet.canvas in game development.
We hope that this blog has helped you enhance your knowledge regarding pyglet.canvas in game development. If you would like to learn more about Pyglet LibraryWhy is Pyglet usedAsteroids game using Pyglet, and more, check out our articles on Coding Ninjas Studio. Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass