Table of contents
1.
Introduction
2.
What is Pyglet?
3.
Shapes in Pyglet
4.
Creating a Shape
5.
Anchor Points
6.
Complete Program
7.
Frequently Asked Questions
7.1.
Which is better, pygame or pyglet?
7.2.
What can you do with pyglet?
7.3.
What are the advantages of Pyglet?
8.
Conclusion
Last Updated: Mar 27, 2024

Shapes in Pyglet

Author Sagar Mishra
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

This article will take you through the steps of creating shapes in pyglet. Pyglet is easy to use, and it has a powerful library for developing visually advanced GUI applications like multimedia, desktop games, etc. If you haven't heard about the term Pyglet, then you should check out this article.

This article will discuss the Shapes in Pyglet and their components. So without wasting your time, let's understand this topic in detail.  

What is Pyglet?

Source Image- Link

Pyglet is a Python programming language library that provides an object-oriented application programming interface to create games and other multimedia applications. Pyglet runs on Microsoft Windows, Linux, and macOS, released under a BSD Licence. Pyglet makes it simple and easy to play and mix many sounds in your game.

Shapes in Pyglet

The shapes module is a simplified choice for creating and manipulating colored shapes. It consists of circles, rectangles, and lines. Shapes can be positioned, resized, and rotated where applicable, and the user can change their color and opacity. All shapes are carried out using OpenGL primitives to be drawn efficiently with Batched rendering. In the following examples, Batch will omit for brevity; however, in general, you always want to use Batched rendering for performance.

Creating a Shape

There is a lot of shapes that can be created with specific size, position, and color:

circle = shapes.Circle(x=120, y=150, radius=120, color=(50, 225, 30))
square = shapes.Rectangle(x=250, y=250, width=250, height=250, color=(55, 55, 255))

 

Users can set the opacity and also change the color. The range of opacity is 0-255 for various levels of transparency:

circle.opacity = 110

 

The size of Shapes can also be changed after creation:

square.width = 200
circle.radius = 99 

Anchor Points

Anchor Point of any Shape can be set similar to images in Pyglet. It pertains to the center of the shape on the x and y-axis. For Rectangles, it's the bottom left corner. And for Circles, the default anchor point is the circle's center. 

Depending on how you want to position your Shapes, this may be changed. For Rectangles, this is especially useful if you'll rotate it since Shapes will rotate around the anchor point. As an example, a Rectangle is created, and the anchor point is set to the center:

rectangle = shapes.Rectangle(x=500, y=500, width=100, height=50)
rectangle.anchor_x = 30
rectangle.anchor_y = 60
# or, set at the same time:
rectangle.anchor_position = 50, 25

 

# Here, The rectangle is then rotated around its anchor point:

rectangle.rotation = 45

 

If you want to create a large number of shapes, you can optionally set the default anchor points:

shapes.Rectangle._anchor_x = 120
shapes.Rectangle._anchor_y = 60

Complete Program

rectangle= shapes.Rectangle(x=250, y=250, width=250, height=250, color=(55, 55, 255))
#Set opacity
rectangle.opacity = 110

#Set Shapes
rectangle.width = 200
rectangle.height = 99 

#Add anchor point
#Set anchor to center
rectangle = shapes.Rectangle(x=500, y=500, width=100, height=50)
rectangle.anchor_x = 30
rectangle.anchor_y = 60
# or, set at the same time:
rectangle.anchor_position = 50, 25

# Here, The rectangle is then rotated around its anchor point:
rectangle.rotation = 45

Frequently Asked Questions

Which is better, pygame or pyglet?

Speed-wise, Pyglet is faster than pygame out-of-the-box, and speed is usually a concern while developing with pygame (you need to update the smallest parts of the screen, and remembering what has been modified can be tedious).

What can you do with pyglet?

Pyglet is a powerful and simple to use library for developing visually rich GUI packages like games, multimedia, and many more on Windows, Linux, and Mac OS.

What are the advantages of Pyglet?

Pyglet has the advantage of multi-monitor desktops and multiple windows. It can load images, music, sound, and video in nearly all formats. It permits us to apply it for both industrial and different open-source projects.

Conclusion

In this article, we have extensively discussed the topic of Shapes in Pyglet and its components in detail. We hope that this blog has helped you enhance your knowledge regarding the subject of Shapes in Pyglet and if you would like to learn more, check out our articles on Web Applications.

Still, the knowledge never stops, have a look at more related articles: PygletPygame, and many more. Do upvote our blog to help other ninjas grow. 

A ninja never stops learning, so to feed your quest to learn and become more advanced and skilled, head over to our practice platform Coding Ninjas Studio to practice advanced-level problems. Attempt mock tests, read interview experiencesinterview bundles, and much more! 

You can refer to programming fundamentals and SQL practice questions to sharpen your skills. If you need guidance at any stage while learning, then you can go to our Guided path.

Thank you for reading. 

Happy coding!

 

Live masterclass