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 Library, Why is Pyglet used, Asteroids game using Pyglet, and more, check out our articles on Coding Ninjas Studio. Do upvote our blog to help other ninjas grow. Happy Coding!
