Pyglet shaps形状控件的种类和用法(共12种)

简介: Pyglet shaps形状控件的种类和用法(共12种)

pyglet.shapes

pyglet.shapes 是 pyglet 库中的一个模块,它提供了一系列预定义的2D图形类,这些类可以用于简化在 pyglet 窗口中绘制基本形状的过程。通过使用 pyglet.shapes,你可以轻松地在屏幕上绘制圆形、矩形、多边形等,而无需深入了解底层的细节。pyglet.shapes 中的每个图形类通常都有以下共同的特征和用法:


初始化参数:每个图形类在创建时都接受一系列的初始化参数,这些参数定义了图形的大小、位置、颜色以及其他一些属性。例如,pyglet.shapes.Circle 接受 x、y(圆心坐标)、radius(半径)、color(颜色)等参数。


批处理(Batching):为了高效渲染,pyglet.shapes 中的图形通常与一个 pyglet.graphics.Batch 对象相关联。批处理允许你将多个图形组合在一起,并一次性提交给GPU进行渲染,从而提高了渲染性能。一旦图形被创建并添加到批处理对象中,它们就可以通过调用批处理对象的 draw() 方法来渲染。这通常发生在窗口的 on_draw 事件中。


pyglet.shapes 共有12个形状类,分别是:


1. 圆弧 Arc

Arc(x, y, radius, segments=None, angle=6.283185307179586, start_angle=0, closed=False, color=(255, 255, 255, 255), batch=None, group=None)
Create an Arc.
The Arc's anchor point (x, y) defaults to its center.
:Parameters:
`x` : float
X coordinate of the circle.
`y` : float
Y coordinate of the circle.
`radius` : float
The desired radius.
`segments` : int
You can optionally specify how many distinct line segments the arc should be made from. If not specified it will be automatically calculated using the formula:`max(14, int(radius / 1.25))`.
`angle` : float
The angle of the arc, in radians. Defaults to tau (pi * 2), which is a full circle.
`start_angle` : float
The start angle of the arc, in radians. Defaults to 0.
`closed` : bool
If True, the ends of the arc will be connected with a line. defaults to False.


2. 贝塞尔曲线 BezierCurve

BezierCurve(*points, t=1.0, segments=100, color=(255, 255, 255, 255), batch=None, group=None)
Create a Bézier curve.
The curve's anchor point (x, y) defaults to its first control point.
:Parameters:
`points` : List[[int, int]]
Control points of the curve.
`t` : float
Draw `100*t` percent of the curve. 0.5 means the curve is half drawn and 1.0 means draw the whole curve.
`segments` : int
You can optionally specify how many line segments the curve should be made from.


3. 带边框矩形 BorderedRectangle

BorderedRectangle(x, y, width, height, border=1, color=(255, 255, 255), border_color=(100, 100, 100), batch=None, group=None)
Create a rectangle or square.
The rectangle's anchor point defaults to the (x, y) coordinates, which are at the bottom left.
:Parameters:
`x` : float
The X coordinate of the rectangle.
`y` : float
The Y coordinate of the rectangle.
`width` : float
The width of the rectangle.
`height` : float
The height of the rectangle.
`border` : float
The thickness of the border.
`border_color` : (int, int, int, int)
The RGB or RGBA fill color of the border, specified as a tuple of 3 or 4 ints in the range of 0-255. RGB colors will be treated as having an opacity of 255.
The alpha values must match if you pass RGBA values to both this argument and `border_color`. If they do not, a `ValueError` will be raised informing you of the ambiguity.


4. 矩形边框 Box

Box(x, y, width, height, thickness=1, color=(255, 255, 255, 255), batch=None, group=None)
Create an unfilled rectangular shape, with optional thickness.
The box's anchor point defaults to the (x, y) coordinates, which are at the bottom left.
Changing the thickness of the box will extend the walls inward; the outward dimesions will not be affected.
:Parameters:
`x` : float
The X coordinate of the box.
`y` : float
The Y coordinate of the box.
`width` : float
The width of the box.
`height` : float
The height of the box.
`thickness` : float
The thickness of the lines that make up the box.


5. 圆形 Circle

Circle(x, y, radius, segments=None, color=(255, 255, 255, 255), batch=None, group=None)
Create a circle.
The circle's anchor point (x, y) defaults to the center of the circle.
:Parameters:
`x` : float
X coordinate of the circle.
`y` : float
Y coordinate of the circle.
`radius` : float
The desired radius.
`segments` : int
You can optionally specify how many distinct triangles the circle should be made from. If not specified it will be automatically calculated using the formula: `max(14, int(radius / 1.25))`.


6. 椭圆 Ellipse

Ellipse(x, y, a, b, segments=None, color=(255, 255, 255, 255), batch=None, group=None)
Create an ellipse.
The ellipse's anchor point (x, y) defaults to the center of the ellipse.
:Parameters:
`x` : float
X coordinate of the ellipse.
`y` : float
Y coordinate of the ellipse.
`a` : float
Semi-major axes of the ellipse.
`b`: float
Semi-minor axes of the ellipse.


7. 直线 Line

Line(x, y, x2, y2, width=1, color=(255, 255, 255, 255), batch=None, group=None)
 Create a line.
The line's anchor point defaults to the center of the line's width on the X axis, and the Y axis.
:Parameters:
`x` : float
The first X coordinate of the line.
`y` : float
The first Y coordinate of the line.
`x2` : float
The second X coordinate of the line.
`y2` : float
The second Y coordinate of the line.
`width` : float
The desired width of the line.

8. 多边形 Polygon

Polygon(*coordinates, color=(255, 255, 255, 255), batch=None, group=None)
Create a convex polygon.
The polygon's anchor point defaults to the first vertex point.
:Parameters:
`coordinates` : List[[int, int]]
The coordinates for each point in the polygon.

9. 矩形 Rectangle

Rectangle(x, y, width, height, color=(255, 255, 255, 255), batch=None, group=None)
Create a rectangle or square.
The rectangle's anchor point defaults to the (x, y) coordinates, which are at the bottom left.
:Parameters:
`x` : float
The X coordinate of the rectangle.
`y` : float
The Y coordinate of the rectangle.
`width` : float
The width of the rectangle.
`height` : float
The height of the rectangle.


10. 扇形 Sector

Sector(x, y, radius, segments=None, angle=6.283185307179586, start_angle=0, color=(255, 255, 255, 255), batch=None, group=None)
Create a Sector of a circle.
The sector's anchor point (x, y) defaults to the center of the circle.
:Parameters:
`x` : float
X coordinate of the sector.
`y` : float
Y coordinate of the sector.
`radius` : float
The desired radius.
`segments` : int
You can optionally specify how many distinct triangles the sector should be made from. If not specified it will be automatically calculated using the formula: `max(14, int(radius / 1.25))`.
`angle` : float
The angle of the sector, in radians. Defaults to tau (pi * 2),
which is a full circle.
`start_angle` : float
The start angle of the sector, in radians. Defaults to 0.


11. 星形 Star

Star(x, y, outer_radius, inner_radius, num_spikes, rotation=0, color=(255, 255, 255, 255), batch=None, group=None) -> None
Create a star.
The star's anchor point (x, y) defaults to the center of the star.
:Parameters:
`x` : float
The X coordinate of the star.
`y` : float
The Y coordinate of the star.
`outer_radius` : float
The desired outer radius of the star.
`inner_radius` : float
The desired inner radius of the star.
`num_spikes` : float
The desired number of spikes of the star.
`rotation` : float
The rotation of the star in degrees. A rotation of 0 degrees will result in one spike lining up with the X axis in positive direction.

12. 三角形 Triangle

Triangle(x, y, x2, y2, x3, y3, color=(255, 255, 255, 255), batch=None, group=None)
Create a triangle.
The triangle's anchor point defaults to the first vertex point.
:Parameters:
`x` : float
The first X coordinate of the triangle.
`y` : float
The first Y coordinate of the triangle.
`x2` : float
The second X coordinate of the triangle.
`y2` : float
The second Y coordinate of the triangle.
`x3` : float
The third X coordinate of the triangle.
`y3` : float
The third Y coordinate of the triangle.

所有形状控件都有的3个参数: color, batch, group,color即形状的颜色,关于batch和group批组参数,请参见博文《Pyglet控件的批处理参数batch和分组参数group简析》中的说明。


`color` : (int, int, int, int)
The RGB or RGBA color of the curve, specified as a tuple of 3 or 4 ints in the range of 0-255. RGB colors will be treated as having an opacity of 255.
`batch` : `~pyglet.graphics.Batch`
Optional batch to add the curve to.
`group` : `~pyglet.graphics.Group`
Optional parent group of the curve.


所有形状控件都有的2个方法:

delete(self)
        Force immediate removal of the shape from video memory.
        It is recommended to call this whenever you delete a shape, as the Python garbage collector will not necessarily call the finalizer as soon as the sprite falls out of scope.
draw(self)
        Draw the shape at its current position.
        Using this method is not recommended. Instead, add the shape to a `pyglet.graphics.Batch` for efficient rendering.


ShapeBase

所有形状控件都有从Shape基类继承来的属性:


anchor_position

The (x, y) coordinates of the anchor point, as a tuple.
:Parameters:
`x` : int or float
X coordinate of the anchor point.
`y` : int or float
Y coordinate of the anchor point.


anchor_x

The X coordinate of the anchor point
:type: int or float


anchor_y

The Y coordinate of the anchor point
:type: int or float


batch

User assigned :class:`Batch` object.


color

The shape color.
This property sets the color of the shape.
The color is specified as an RGB tuple of integers '(red, green, blue)'.
Each color component must be in the range 0 (dark) to 255 (saturated).
:type: (int, int, int)


group

User assigned :class:`Group` object.


opacity

Blend opacity.
This property sets the alpha component of the color of the shape.
With the default blend mode (see the constructor), this allows the shape to be drawn with fractional opacity, blending with the background.
An opacity of 255 (the default) has no effect.  An opacity of 128 will make the shape appear translucent.
:type: int


position

The (x, y) coordinates of the shape, as a tuple.
:Parameters:
`x` : int or float
X coordinate of the sprite.
`y` : int or float
Y coordinate of the sprite.


rotation

Clockwise rotation of the shape in degrees.
It will be rotated about its (anchor_x, anchor_y) position, which defaults to the first vertex point of the shape.
For most shapes, this is the lower left corner. The shapes below default to the points their ``radius`` values are measured from:
* :py:class:`.Circle`
* :py:class:`.Ellipse`
* :py:class:`.Arc`
* :py:class:`.Sector`
* :py:class:`.Star`

visible

True if the shape will be drawn.
:type: bool


x

X coordinate of the shape.
:type: int or float


y

Y coordinate of the shape.
:type: int or float


多个形状的示例代码:


附带两个基类属性 opacity、rotation 的用法

import pyglet
from pyglet import shapes
 
window = pyglet.window.Window(960, 540)
 
circle = shapes.Circle(700, 150, 100, color=(50, 225, 30))
square = shapes.Rectangle(200, 200, 200, 200, color=(55, 55, 255))
rectangle = shapes.Rectangle(250, 300, 400, 200, color=(255, 22, 20))
rectangle.opacity = 128  # 矩形的不透明度属性
rectangle.rotation = 33  # 矩形的旋转角度属性
line = shapes.Line(100, 100, 100, 200, width=19)
line2 = shapes.Line(150, 150, 444, 111, width=4, color=(200, 20, 20))
star = shapes.Star(800, 400, 60, 40, num_spikes=20, color=(255, 255, 0))
widgets = circle, square, rectangle, line, line2, star
 
@window.event
def on_draw():
    window.clear()
    for batch in widgets:
        batch.draw()
 
pyglet.app.run()

运行效果:

本文完。

目录
相关文章
|
Oracle 安全 关系型数据库
搭建 OpenLDAP 自助修改密码系统
让修改open ldap密码变得简单
2024 0
搭建 OpenLDAP 自助修改密码系统
|
网络安全 Python
There was a problem confirming the ssl certificate
There was a problem confirming the ssl certificate
833 0
|
Web App开发 测试技术 虚拟化
|
2月前
|
消息中间件 搜索推荐 关系型数据库
广告竞价为什么要拼毫秒级速度?揭秘 RTB 实时广告系统背后的数据流水线设计
广告竞价为什么要拼毫秒级速度?揭秘 RTB 实时广告系统背后的数据流水线设计
222 3
|
2月前
|
人工智能 安全 芯片
OpenClaw免费安装教程,TopClaw零基础本地部署+1000万token领取
本文介绍TopClaw——一款专为小白设计的OpenClaw开源大模型“傻瓜式”本地部署工具。无需编程基础,Windows/Mac一键安装,中文界面友好,自带1000万免费token,全程离线运行,兼顾高效与隐私安全。
296 2
|
2月前
|
域名解析 缓存 网络协议
阿里云国际站代理商:OSS自定义域名配置教程 解析绑定与HTTPS证书设置
在阿里云上给 OSS 配置自定义域名,到这一步卡住的人远比想象中多。域名绑完了,浏览器里敲进去要么 403,要么直接打不开,排查半天才发现问题出在解析、证书或者 Bucket 权限这一层。
1049 0
|
3月前
|
人工智能
OPC中国和智能体来了是什么关系?
OPC中国是智能体来了旗下的开源人才生态平台,专注OPC一人公司与OPD一人部门的培育孵化;智能体来了则是AI智能体职业培训的能力底座。二者一体两面:前者重生态连接与场景落地,后者强专业训练与能力输出,共同构建“教—训—育—孵”闭环。
OPC中国和智能体来了是什么关系?
|
10月前
|
运维 API 开发工具
打造可编程可集成的实时计算平台:阿里云实时计算 Flink被集成能力深度解析
本文由阿里云Flink团队李昊哲主讲,系统介绍Flink四层开放架构:通过OpenAPI、Git集成、多语言SDK等能力,实现控制面、数据面、开发面与运维面的全面开放。助力企业构建可编程、可嵌入、可治理的实时计算平台,推动数据开发工程化升级。
510 1
|
6月前
|
NoSQL Java 测试技术
全链路压测硬核实战:从方案落地、瓶颈根因定位到全链路性能优化
全链路压测是保障微服务系统稳定性的核心手段,通过模拟真实线上流量,在预发或生产环境验证端到端性能与容错能力,精准暴露跨服务调用、资源竞争等隐藏瓶颈,并提供标准化的定位与优化方案。
890 3
|
5月前
|
人工智能 IDE 机器人
给阿里产品团队的真心话:千问很强,但我们需要它能“干活”!
作为一名一线程序员,建议千问尽快打通项目目录读取功能,并实现手机电脑端同步,做真正能干活的AI工具。