Table of contents
1.
Introduction
2.
pyglet.shapes
3.
class Arc
3.1.
draw()
3.2.
anchor_position
3.3.
anchor_x and anchor_y
3.4.
color
3.5.
opacity
3.6.
position
3.7.
rotation
3.8.
visible
3.9.
x and y
4.
class Circle
4.1.
draw()
5.
class Ellipse
5.1.
draw()
5.2.
a and b
6.
class Line
6.1.
x, x2, y, y2
7.
class Rectangle
7.1.
anchor_x and anchor_y
7.2.
height and weight
7.3.
rotation
8.
class BorderedRectangle
8.1.
border_color
9.
class Triangle
9.1.
x, x2, x3 y, y2, y3
10.
class Star
10.1.
inner_radius
10.2.
num_spikes
10.3.
outer_radius
10.4.
rotation
11.
class Polygon
12.
class Sector
13.
Coding Example 
14.
FAQs
14.1.
How do you draw a Pyglet line?
14.2.
How do you make a Pyglet button?
14.3.
What is Python QT?
14.4.
Is pyglet open-source?
14.5.
Why is Qt not popular?
15.
Conclusion
Last Updated: Mar 27, 2024

pyglet.shapes

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.shapes for game development. So let us dive in!

pyglet.shapes

It is used for 2D shapes.
Rectangles, Circles, and Lines are just a few of the simple 2D shapes covered in this module. When drawn as part of a Batch, these shapes are created internally from OpenGL primitives and provide excellent performance. Positioning, changing colour and opacity, and rotating are all done with ease (where applicable). The lower level graphics API is more appropriate for creating more complex shapes than what is provided here. For more information, see the graphics.

class Arc

class Arc(x, y, radius, segments=None, angle=6.283185307179586, start_angle=0, closed=False, color=(255, 255, 255), batch=None, group=None)

draw()

Create a drawing of the shape in its current position.
It is not recommended that you use this method. Add the shape to a pyglet.graphics instead. Rendering is more efficient when done in batches.

anchor_position

The anchor point's (x, y) coordinates as a tuple.

anchor_x and anchor_y

The X and Y coordinate of the anchor point, respectively.

color

The colour of the shape.
The colour of the shape is controlled by this property.
The colour is specified as an RGB tuple (red, green, blue) of integers. Each colour component must be in the 0 (dark) to 255 (light) range (saturated).

opacity

opacity of the blend
The alpha component of the shape's colour is controlled by this property. This allows the shape to be drawn with fractional opacity, blending with the background, using the default blend mode (see the function Object() { [native code] }).
The default opacity of 255 has no effect. The shape will appear translucent with an opacity of 128.

position

As a tuple, the shape's (x, y) coordinates.

rotation

In degrees, the arc rotates clockwise.
The anchor x, anchor y) position of the arc will be rotated.

visible

If the shape will be drawn, this is true.

x and y

X and Y coordinates of the shape, respectively.

class Circle

class Circle(x, y, radius, segments=None, color=(255, 255, 255), batch=None, group=None)

draw()

Create a drawing of the shape in its current position.
It is not recommended that you use this method. Instead, use a pyglet.graphics.Batch to render the shape quickly.

class Ellipse

class Ellipse(x, y, a, b, color=(255, 255, 255), batch=None, group=None)

draw()

Create a drawing of the shape in its current position.
It is not recommended that you use this method. Instead, use a pyglet.graphics.Batch to render the shape quickly.

a and b

The semi-major and semi-minor axes of the ellipse, respectively.

class Line

class Line(x, y, x2, y2, width=1, color=(255, 255, 255), batch=None, group=None)

x, x2, y, y2

x, second x, y, and second y coordinates of the shape, respectively.

class Rectangle

class Rectangle(x, y, width, height, color=(255, 255, 255), batch=None, group=None)

anchor_x and anchor_y

The X and Y coordinate of the anchor point, respectively.

height and weight

The height and width of the rectangle, respectively.

rotation

In degrees, the rectangle is rotated clockwise.
The Rectangle will be rotated in the direction of (anchor x, anchor y).

class BorderedRectangle

class BorderedRectangle(x, y, width, height, border=1, color=(255, 255, 255), border_color=(100, 100, 100), batch=None, group=None)

border_color

The colour of the rectangle's border.
This property determines the colour of a bordered rectangle's border.
The colour is specified as an RGB tuple (red, green, blue) of integers. Each colour component must be in the 0 (dark) to 255 (light) range (saturated).

class Triangle

class Triangle(x, y, x2, y2, x3, y3, color=(255, 255, 255), batch=None, group=None)

x, x2, x3 y, y2, y3

x, second x, third x, y, second y, and third y coordinates of the shape, respectively.

class Star

class Star(x, y, outer_radius, inner_radius, num_spikes, rotation=0, color=(255, 255, 255), batch=None, group=None)

inner_radius

The star's inner radius.

num_spikes

The star's number of spikes.

outer_radius

The star's outer radius.

rotation

The degree of rotation of the star.

class Polygon

class Polygon(*coordinates, color=(255, 255, 255), batch=None, group=None)


A polygon is a geometric figure with a finite number of sides in two dimensions. A polygon's sides are made up of straight line segments that are connected end to end.

class Sector

class Sector(x, y, radius, segments=None, angle=6.283185307179586, start_angle=0, color=(255, 255, 255), batch=None, group=None)

 

A sector is defined as a portion of a circle made up of the circle's arc and two radii. It's a section of the circle made up of a portion of the circumference (arc) and the circle's radii at both ends.

Coding Example 

We will see how we can draw rectangle on window in PYGLET module in python.

# importing pyglet module
import pyglet

# importing shapes from the pyglet
from pyglet import shapes

# width of window
width = 500

# height of window
height = 500

# caption i.e title of the window
title = "Rectangle"

# creating a window
window = pyglet.window.Window(width, height, title)

# creating a batch object
batch = pyglet.graphics.Batch()

# properties of rectangle
# co-ordinates of rectangle
co_x = 150
co_y = 150

# width of rectangle
width = 300

# height of rectangle
height = 200

# color = green
color = (50, 225, 30)

# creating a rectangle
rec1 = shapes.Rectangle(co_x, co_y, width, height, color = color, batch = batch)

# changing opacity of the rect1
# opacity is visibility (0 = invisible, 255 means visible)
rec1.opacity = 250

# creating another rectangle with properties
# x, y co ordinate : 50, 250
# width, height of rectangle : 300, 200
# color = red
color = (255, 25, 25)

# creating rectangle
rec2 = shapes.Rectangle(50, 250, 300, 200, color = color, batch = batch)

# changing opacity of the rec2
# opacity is visibility (0 = invisible, 255 means visible)
rec2.opacity = 100

# window draw event to draw rectangles
@window.event
def on_draw():

# clear the window
window.clear()

# draw the batch
batch.draw()

# run the pyglet application
pyglet.app.run()

FAQs

How do you draw a Pyglet line?

Syntax: shapes.Line(co_x1, co_y1, co_x2, co_y2, width, color = (50, 225, 30), batch=batch)

Argument: It takes starting and end position in form of pair of two integers, width of line, color of line and last is batch object.

Return: It returns Line object.

How do you make a Pyglet button?

If you want to draw a button on the screen, first draw a rectangle, fill it with text, and then listen for mouse clicks to see if it's on this rectangle.

What is Python QT?

The official set of Python bindings (PySide6) that will supercharge your Python applications is provided by the Qt for Python project. While the Qt APIs are well-known, there are several other reasons to consider Qt for Python.

Is pyglet open-source?

Pyglet is released under the BSD open-source licence, which allows it to be used in both commercial and open-source projects with few restrictions.

Why is Qt not popular?

The real reason Qt isn't more popular is that it's written in C++, and fewer people use C++ for desktop apps. Qt isn't a C++ programme. When compared to most other libraries, it necessitates a separate compilation step, making the build process much more complicated.

Conclusion

In this article, we saw the theoretical and practical implementation of pyglet.shapes in game development.
We hope that this blog has helped you enhance your knowledge regarding pyglet.shapes 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