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()

运行效果:

本文完。

目录
相关文章
|
传感器 Ubuntu 算法
【6. 激光雷达接入ROS】(1)
【6. 激光雷达接入ROS】(1)
1191 0
|
5月前
|
人工智能 安全 芯片
OpenClaw免费安装教程,TopClaw零基础本地部署+1000万token领取
本文介绍TopClaw——一款专为小白设计的OpenClaw开源大模型“傻瓜式”本地部署工具。无需编程基础,Windows/Mac一键安装,中文界面友好,自带1000万免费token,全程离线运行,兼顾高效与隐私安全。
460 2
|
6月前
|
人工智能
OPC中国和智能体来了是什么关系?
OPC中国是智能体来了旗下的开源人才生态平台,专注OPC一人公司与OPD一人部门的培育孵化;智能体来了则是AI智能体职业培训的能力底座。二者一体两面:前者重生态连接与场景落地,后者强专业训练与能力输出,共同构建“教—训—育—孵”闭环。
OPC中国和智能体来了是什么关系?
|
5月前
|
域名解析 缓存 网络协议
阿里云国际站代理商:OSS自定义域名配置教程 解析绑定与HTTPS证书设置
在阿里云上给 OSS 配置自定义域名,到这一步卡住的人远比想象中多。域名绑完了,浏览器里敲进去要么 403,要么直接打不开,排查半天才发现问题出在解析、证书或者 Bucket 权限这一层。
|
5月前
|
数据挖掘 机器人 Shell
DeerFlow 放进企业里怎么落地?我会先跑三条线,而不是急着做万能 Agent
DeerFlow并非万能聊天机器人,而是面向企业“长任务”的可治理Agent平台。本文详解其三大落地场景:研究报告、数据分析与研发自动化,并强调权限管控、沙箱隔离与审计合规,主张小切口试点、稳边界推进。
438 0
|
5月前
|
人工智能 调度 数据库
智能体操作系统的技术拆解:任务编排层的状态机设计思路
2026世界人工智能大会将于7月17—20日在上海举行。本文聚焦“Agent操作系统”这一新提法,剖析其背后任务编排从“能跑通”到“要健壮”的演进逻辑,详解状态机设计、检查点恢复、错误分类及并发控制等核心工程实践。
380 0
|
运维 API 开发工具
打造可编程可集成的实时计算平台:阿里云实时计算 Flink被集成能力深度解析
本文由阿里云Flink团队李昊哲主讲,系统介绍Flink四层开放架构:通过OpenAPI、Git集成、多语言SDK等能力,实现控制面、数据面、开发面与运维面的全面开放。助力企业构建可编程、可嵌入、可治理的实时计算平台,推动数据开发工程化升级。
663 1
|
6月前
|
存储 移动开发 虚拟化
VMware Host Client 9.1 - 界面焕新, 体验升级
formerly ESXi Host Client | ESXi 操作系统的 Web 管理界面
405 0
|
8月前
|
人工智能 IDE 机器人
给阿里产品团队的真心话:千问很强,但我们需要它能“干活”!
作为一名一线程序员,建议千问尽快打通项目目录读取功能,并实现手机电脑端同步,做真正能干活的AI工具。
|
12月前
|
机器学习/深度学习 存储 数据采集
🔥 高频面试题汇总
本文系统梳理了大模型核心技术,涵盖Transformer架构、训练优化、推理部署及显存估算等关键主题,并结合面试场景提供回答框架与实战计算题解析,助力深入理解与高效表达。
541 0
🔥 高频面试题汇总