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: Pyglet, Pygame, 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 experiences, interview 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!
