pyglet.shapes
2D形状。
此模块为各种简单的2D形状提供类,如矩形、圆形和线条。这些形状是从OpenGL基元内部生成的,当作为 Batch
。提供了定位、更改颜色、不透明度和旋转的方便方法。 Python in
运算符可用于检查点是否位于形状内部。(这与某些形状近似,例如星形)。
如果此模块中的形状不适合您的需求,您有两种选择:
你的目标 |
最佳方法 |
---|---|
像这里这样的简单形状 |
子类 |
复杂且优化的形状 |
看见 着色器和渲染 了解低级图形API。 |
绘制形状的简单示例:
import pyglet
from pyglet import shapes
window = pyglet.window.Window(960, 540)
batch = pyglet.graphics.Batch()
circle = shapes.Circle(700, 150, 100, color=(50, 225, 30), batch=batch)
square = shapes.Rectangle(200, 200, 200, 200, color=(55, 55, 255), batch=batch)
rectangle = shapes.Rectangle(250, 300, 400, 200, color=(255, 22, 20), batch=batch)
rectangle.opacity = 128
rectangle.rotation = 33
line = shapes.Line(100, 100, 100, 200, width=19, batch=batch)
line2 = shapes.Line(150, 150, 444, 111, width=4, color=(200, 20, 20), batch=batch)
star = shapes.Star(800, 400, 60, 40, num_spikes=20, color=(255, 255, 0), batch=batch)
@window.event
def on_draw():
window.clear()
batch.draw()
pyglet.app.run()
备注
一些收件箱,例如 Line
和 Triangle
,有多个坐标。
这些形状对待他们的 position
作为它们的主要坐标。更改它或其组件( x
或 y
属性)还将所有次坐标移动与前一个坐标相同的偏差 position
值这使您可以移动这些形状而不会扭曲它们。
Added in version 1.5.4.
- class ShapeBase
所有Shape对象的基类。
本模块提供了许多默认形状。曲线使用多个顶点进行近似。
如果需要此模块中未提供的形状或功能,可以编写自己的
ShapeBase
通过使用提供的形状作为参考。方法
- draw() None
Debug method to draw a single shape at its current position. :rtype:
None
警告
避免日常使用这种低效的方法!
常规绘图应向
Batch
并称其为draw()
方法。
- delete() None
强制立即从视频内存中删除该形状。
每当您不再需要该形状时,您通常应该调用此命令。否则,Python可能会在形状超出范围后一段时间才调用形状实例的终结器,并且直到终结器最终被垃圾收集调用之前,形状的视频内存不会被释放。
无需使用当前方法,实现手动垃圾收集也可以满足同样的问题,但这是一项非常先进的技术。
- 返回类型:
属性
- position
获取/设置
(x, y)
形状的坐标。所有形状默认围绕其位置旋转。然而,他们这样做的方式各不相同。
与一个
radius
酒店将以此为中心:其他人默认使用它作为左下角。
- rotation
获取/设置形状的顺时针旋转度。
所有形状都围绕它们旋转
anchor_position
.对于大多数形状,默认为两者:形状的第一个顶点
左下角
与一个
radius
属性围绕测量半径的点旋转。这要么是它们的中心,要么是它们被切掉的圆的中心:这些形状绕其中心旋转:
这些形状围绕其角度点旋转:
- anchor_x
获取/设置锚点的X坐标。
如果您需要同时设置此和
anchor_x
,使用anchor_position
取而代之的是。
- anchor_y
获取/设置锚点的Y坐标。
如果您需要同时设置此和
anchor_x
,使用anchor_position
取而代之的是。
- anchor_position
获取/设置锚的
(x, y)
偏离position
这定义了形状旋转的点。默认为
(0.0, 0.0)
.然而:由于所有锚点更新都会在中央处理器上重新计算形状的折点,因此此属性比更新更快
anchor_x
和anchor_y
分开
- color
获取/设置形状的颜色。
颜色可以设置为:
RGBA的整元组
(red, green, blue, alpha)
一个由MB组成的整组
(red, green, blue)
如果设置了Ruby颜色,则将保留当前Alpha。否则,新的Alpha值将用于形状。每个颜色分量必须在0(深色)到255(饱和)的范围内。
- opacity
获取/设置形状的混合不透明度。
小技巧
要打开/关闭可见性,
visible
可能更有效率!不透明度被实现为形状的Alpha分量
color
.当属于默认混合模式为的组时(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
,下面不透明255
在背景上以分数不透明度绘制:示例值和效果 不透明度
效果
255
(默认)形状完全不透明
128
形状看起来半透明
0
无形
- visible
获取/设置是否绘制形状。
对于绝对显示/隐藏,这是
- group
获取/设置形状的
Group
。通过设置此属性,可以将形状从一个组移植到另一个组。请注意,这可能是一个昂贵(缓慢)的操作。
如果
batch
不是None
,设置此属性也会触发批量迁移。
- batch
获取/设置
Batch
对于这个形状。通过设置此属性,您可以将形状从一个批次迁移到另一个批次,但这可能是一个昂贵(缓慢)的操作。
- __init__()
- __new__(**kwargs)
- class Arc
基类:
ShapeBase
- angle
获取/设置以弧线为单位的弧线角度。
- start_angle
获取/设置弧线的起点角(以弧度为单位)。
- thickness
获取/设置Arc的厚度
- __init__(
- x: float,
- y: float,
- radius: float,
- segments: int | None = None,
- angle: float = 6.283185307179586,
- start_angle: float = 0.0,
- closed: bool = False,
- thickness: float = 1.0,
- color=(255, 255, 255, 255),
- batch: Batch | None = None,
- group: Group | None = None,
创建一个弧线。
Arc的锚点(x,y)默认为其中心。
- 参数:
x (
float
) -- 圆的X坐标。y (
float
) -- 圆的Y坐标。radius (
float
) -- 所需的半径。segments (
int
|None
) -- 您可以选择指定应使用多少个不同的直线段来制作弧线。如果未指定,将使用以下公式自动计算:max(14, int(radius / 1.25))
。angle (
float
) -- 弧线的角度,以弧度为单位。到tau(pi * 2),这是一个完整的圆。start_angle (
float
) -- 弧线的起点角,以弧度为单位。预设为0。closed (
bool
) -- 如果True
,弧线的两端将用一条线连接。默认为False
。thickness (
float
) -- 用于弧形的线的所需厚度或宽度。color -- 弧形的RB或RGBA颜色,指定为0-255范围内的3或4个int的多元组。RB颜色将被视为不透明度为255。
- __new__(**kwargs)
- class BezierCurve
基类:
ShapeBase
- points
获取/设置贝塞尔曲线的控制点。
- t
获取/设置t
100*t
要绘制的曲线的百分比。
- thickness
获取/设置贝塞尔曲线的线厚度。
- __init__(
- *points: tuple[float, float],
- t: float = 1.0,
- segments: int = 100,
- thickness: int = 1.0,
- color: tuple[int, int, int, int] | tuple[int, int, int] = (255, 255, 255, 255),
- batch: Batch | None = None,
- group: Group | None = None,
创建贝塞尔曲线。
曲线的锚点(x,y)默认为其第一个控制点。
- 参数:
points (
tuple
[float
,float
]) -- 曲线的控制点。点可以指定为点对的多个列表或二元组。Ex.(0,0),(2,3),(1,9)t (
float
) -- 画 100*t 曲线的百分比。0.5表示曲线已绘制一半,1.0表示绘制整个曲线。segments (
int
) -- 您可以选择指定应使用多少条直线段来制作曲线。thickness (
int
) -- 用于曲线的线的所需厚度或宽度。color (
tuple
[int
,int
,int
,int
] |tuple
[int
,int
,int
]) -- 曲线的RB或RGBA颜色,指定为0-255范围内的3或4个int的多元组。RB颜色将被视为不透明度为255。
- __new__(**kwargs)
- class Circle
基类:
ShapeBase
- radius
获取/设置圆的半径。
- __init__(
- x: float,
- y: float,
- radius: float,
- segments: int | None = None,
- color: tuple[int, int, int, int] | tuple[int, int, int] = (255, 255, 255, 255),
- batch: Batch | None = None,
- group: Group | None = None,
创建一个圆圈。
圆的锚点(x,y)默认为圆的中心。
- __new__(**kwargs)
- class Ellipse
基类:
ShapeBase
- a
获取/设置椭圆的半长轴。
- b
获取/设置椭圆的半短轴。
- __init__(
- x: float,
- y: float,
- a: float,
- b: float,
- segments: int | None = None,
- color: tuple[int, int, int, int] | tuple[int, int, int] = (255, 255, 255, 255),
- batch: Batch | None = None,
- group: Group | None = None,
创建椭圆。
椭圆的锚点
(x, y)
默认为椭圆的中心。
- __new__(**kwargs)
- class Sector
基类:
ShapeBase
- angle
获取/设置扇形的角度,单位为弧度。
- start_angle
获取/设置扇形的起点角(以弧度为单位)。
- radius
获取/设置扇形的半径。
默认情况下,这以屏幕像素为单位。您的绘制/ GL设置可能会改变其绘制方式。
- __init__(
- x: float,
- y: float,
- radius: float,
- segments: int | None = None,
- angle: float = 6.283185307179586,
- start_angle: float = 0.0,
- color: tuple[int, int, int, int] | tuple[int, int, int] = (255, 255, 255, 255),
- batch: Batch | None = None,
- group: Group | None = None,
创建圆圈的一个扇形。
默认情况下,
(x, y)
用作: * The sector's anchor point * 扇形被切出的圆的中心- 参数:
x (
float
) -- 扇形的X坐标。y (
float
) -- 扇形的Y坐标。radius (
float
) -- 所需的半径。segments (
int
|None
) -- 您可以选择指定该扇形应该由多少个不同的三角形组成。如果未指定,将使用以下公式自动计算: max(14, int(radius / 1.25)) 。angle (
float
) -- 扇形的角度,以弧度为单位。到tau(pi * 2),这是一个完整的圆。start_angle (
float
) -- 扇形的开始角,以弧度为单位。预设为0。color (
tuple
[int
,int
,int
,int
] |tuple
[int
,int
,int
]) -- 圆的RB或RGBA颜色,指定为0-255范围内的3或4个int的多元组。RB颜色将被视为不透明度为255。
- __new__(**kwargs)
- class Line
基类:
ShapeBase
- x2
获取/设置线的第二个X坐标。
- y2
获取/设置线的第二个Y坐标。
- __init__(
- x: float,
- y: float,
- x2: float,
- y2: float,
- width: float = 1.0,
- color: tuple[int, int, int, int] | tuple[int, int, int] = (255, 255, 255, 255),
- batch: Batch | None = None,
- group: Group | None = None,
创建一条线。
线的锚点默认为X轴和Y轴上线宽度的中心。
- __new__(**kwargs)
- class Rectangle
基类:
ShapeBase
- __init__(
- x: float,
- y: float,
- width: float,
- height: float,
- color: tuple[int, int, int, int] | tuple[int, int, int] = (255, 255, 255, 255),
- batch: Batch | None = None,
- group: Group | None = None,
创建矩形或正方形。
矩形的锚点默认为
(x, y)
坐标,位于左下方。
- __new__(**kwargs)
- class Box
基类:
ShapeBase
- __init__(
- x: float,
- y: float,
- width: float,
- height: float,
- thickness: float = 1.0,
- color: tuple[int, int, int, int] | tuple[int, int, int] = (255, 255, 255, 255),
- batch: Batch | None = None,
- group: Group | None = None,
创建未填充的矩形形状,厚度可选。
框的锚点默认为
(x, y)
坐标,位于左下方。改变盒子的厚度会向内延伸墙壁;外部尺寸不会受到影响。
- __new__(**kwargs)
- class BorderedRectangle
基类:
ShapeBase
- border_color
获取/设置有边框矩形的边框颜色。
要设置内部填充的颜色,请参阅
color
。您可以将边框颜色设置为以下之一:
RGBA的整元组
(red, green, blue, alpha)
一个由MB组成的整组
(red, green, blue)
在此属性上设置Alpha将更改整个形状的Alpha,包括填充和边界。
每个颜色分量必须在0(暗)到255(饱和)的范围内。
- __init__(
- x: float,
- y: float,
- width: float,
- height: float,
- border: float = 1.0,
- color: tuple[int, int, int, int] | tuple[int, int, int] = (255, 255, 255),
- border_color: tuple[int, int, int, int] | tuple[int, int, int] = (100, 100, 100),
- batch: Batch | None = None,
- group: Group | None = None,
创建一个有边界的矩形。
矩形的锚点默认为
(x, y)
坐标,位于左下方。- 参数:
x (
float
) -- 矩形的X坐标。y (
float
) -- 矩形的Y坐标。width (
float
) -- 矩形的宽度。height (
float
) -- 矩形的高度。border (
float
) -- 边界的厚度。color (
tuple
[int
,int
,int
,int
] |tuple
[int
,int
,int
]) -- 矩形的Ruby或RGBA填充色,指定为0-255范围内的3或4个int的二元组。RB颜色将被视为不透明度为255。border_color (
tuple
[int
,int
,int
,int
] |tuple
[int
,int
,int
]) -- 边框的RB或RGBA填充色,指定为0-255范围内的3或4个int的多元组。RB颜色将被视为不透明度为255。 如果将RGBA值传递给此参数和 border_color .如果他们不这样做,a ValueError 将通知您模糊性。
- __new__(**kwargs)
- class Triangle
基类:
ShapeBase
- x2
获取/设置三角形第二个顶点的X坐标。
- y2
获取/设置三角形第二个顶点的Y坐标。
- x3
获取/设置三角形第三个顶点的X坐标。
- y3
获取/设置三角形第三个顶点的Y值。
- __init__(
- x: float,
- y: float,
- x2: float,
- y2: float,
- x3: float,
- y3: float,
- color: tuple[int, int, int, int] | tuple[int, int, int] = (255, 255, 255, 255),
- batch: Batch | None = None,
- group: Group | None = None,
创建三角形。
三角形的锚点默认为第一个顶点。
- __new__(**kwargs)
- class Star
基类:
ShapeBase
- outer_radius
获取/设置恒星的外半径。
- inner_radius
获取/设置恒星的内半径。
- num_spikes
恒星的尖峰数目。
- __init__(
- x: float,
- y: float,
- outer_radius: float,
- inner_radius: float,
- num_spikes: int,
- rotation: float = 0.0,
- color: tuple[int, int, int, int] | tuple[int, int, int] = (255, 255, 255, 255),
- batch: Batch | None = None,
- group: Group = None,
创造一个明星。
明星的锚点
(x, y)
默认为明星的屏幕中心。- 参数:
x (
float
) -- 恒星的X坐标。y (
float
) -- 恒星的Y坐标。outer_radius (
float
) -- 恒星所需的外半径。inner_radius (
float
) -- 恒星所需的内半径。num_spikes (
int
) -- 所需的恒星尖峰数量。rotation (
float
) -- 恒星的旋转度。旋转0度将导致一个尖峰与X轴沿正方向对齐。color (
tuple
[int
,int
,int
,int
] |tuple
[int
,int
,int
]) -- 星星的RB或RGBA颜色,指定为0-255范围内的3或4个int的二元组。RB颜色将被视为不透明度为255。group (
Group
) -- 明星的可选家长组。
- __new__(**kwargs)
- class Polygon
基类:
ShapeBase
- __init__(
- *coordinates: tuple[float, float] | Sequence[float],
- color: tuple[int, int, int, int] | tuple[int, int, int] = (255, 255, 255, 255),
- batch: Batch | None = None,
- group: Group | None = None,
创建一个多边形。
多边形的锚点默认为第一个顶点。
- __new__(**kwargs)
- class MultiLine
基类:
ShapeBase
- thickness
获取/设置多线的线厚度。
- __init__(
- *coordinates: tuple[float, float] | Sequence[float],
- closed: bool = False,
- thickness: float = 1.0,
- color: tuple[int, int, int, int] = (255, 255, 255, 255),
- batch: Batch | None = None,
- group: Group | None = None,
从一系列坐标创建多条相连的线。
形状的锚点默认为第一个顶点。
- __new__(**kwargs)