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()
备注
某些形状(如线条和三角形)具有多个坐标。如果更新x,y坐标,这也会影响次要坐标。这允许您在不影响其整体尺寸的情况下移动形状。
在 1.5.4 版本加入.
- class ShapeBase
所有Shape对象的基类。
本模块提供了许多默认形状。曲线使用多个顶点进行近似。
如果需要此模块中未提供的形状或功能,可以编写自己的 ShapeBase 通过使用提供的形状作为参考。
方法
- draw()
在当前位置绘制形状。
不建议使用此方法。而是将该形状添加到 pyglet.graphics.Batch 以实现高效渲染。
- delete()
强制立即从视频内存中删除该形状。
建议在删除形状时调用此方法,因为只要精灵超出范围,Python垃圾回收器就不一定会调用终结器。
属性
- x
形状的X坐标。
- 类型:
整型或浮点型
- y
形状的Y坐标。
- 类型:
整型或浮点型
- position
形状的(x,y)坐标,作为一个元组。
- 参数:
- x整型或浮点型
精灵的X坐标。
- y整型或浮点型
精灵的Y坐标。
- rotation
以度为单位的形状顺时针旋转。
它将围绕其默认为图形的第一个顶点的位置(锚点x,锚点y)旋转。
对于大多数形状,这是左下角。下面的形状默认为它们的点
radius
这些值是从以下各项衡量的:
- anchor_x
锚点的X坐标
- 类型:
整型或浮点型
- anchor_y
锚点的Y坐标
- 类型:
整型或浮点型
- anchor_position
锚点的(x,y)坐标,作为一个元组。
- 参数:
- x整型或浮点型
锚点的X坐标。
- y整型或浮点型
锚点的Y坐标。
- color
形状颜色。
此属性设置形状的颜色。
颜色被指定为整数‘(红、绿、蓝)’的RGB元组。每个颜色分量必须在0(暗)到255(饱和)的范围内。
- 类型:
(int,int,int)
- opacity
混合不透明度。
此属性设置形状颜色的Alpha分量。使用默认混合模式(请参见构造函数),这允许以不透明度分数绘制形状,与背景混合。
不透明度为255(默认值)没有任何效果。不透明度为128将使图形显示为半透明。
- 类型:
集成
- visible
如果要绘制形状,则为True。
- 类型:
布尔尔
- group
用户分配
Group
对象。
- batch
用户分配
Batch
对象。
- class Arc(x, y, radius, segments=None, angle=6.283185307179586, start_angle=0, closed=False, thickness=1, color=(255, 255, 255, 255), batch=None, group=None)
基类:
ShapeBase
- angle
圆弧的角度。
- 类型:
浮动
- start_angle
圆弧的起点角度。
- 类型:
浮动
- thickness
- class BezierCurve(*points, t=1.0, segments=100, thickness=1, color=(255, 255, 255, 255), batch=None, group=None)
基类:
ShapeBase
- points
曲线的控制点。
- 类型:
明细表 [[int, int] ]
- t
画 100*t 曲线的百分比。
- 类型:
浮动
- thickness
- class Circle(x, y, radius, segments=None, color=(255, 255, 255, 255), batch=None, group=None)
基类:
ShapeBase
- radius
圆的半径。
- 类型:
浮动
- class Ellipse(x, y, a, b, segments=None, color=(255, 255, 255, 255), batch=None, group=None)
基类:
ShapeBase
- a
椭圆的半长轴。
- 类型:
浮动
- b
椭圆的半短轴。
- 类型:
浮动
- class Sector(x, y, radius, segments=None, angle=6.283185307179586, start_angle=0, color=(255, 255, 255, 255), batch=None, group=None)
基类:
ShapeBase
- angle
扇区的角度。
- 类型:
浮动
- start_angle
扇区的起点角度。
- 类型:
浮动
- radius
地段的半径。
- 类型:
浮动
- class Line(x, y, x2, y2, width=1, color=(255, 255, 255, 255), batch=None, group=None)
基类:
ShapeBase
- x2
形状的第二个X坐标。
- 类型:
整型或浮点型
- y2
形状的第二个Y坐标。
- 类型:
整型或浮点型
- class Rectangle(x, y, width, height, color=(255, 255, 255, 255), batch=None, group=None)
基类:
ShapeBase
- width
矩形的宽度。
- 类型:
浮动
- height
矩形的高度。
- 类型:
浮动
- class Box(x, y, width, height, thickness=1, color=(255, 255, 255, 255), batch=None, group=None)
基类:
ShapeBase
- width
长方体的宽度。
- 类型:
浮动
- height
长方体的高度。
- 类型:
浮动
- class BorderedRectangle(x, y, width, height, border=1, color=(255, 255, 255), border_color=(100, 100, 100), batch=None, group=None)
基类:
ShapeBase
- width
矩形的宽度。
- 类型:
浮动
- height
矩形的高度。
- 类型:
浮动
- border_color
矩形的边框颜色。
此属性设置带边框的矩形的边框颜色。
颜色被指定为整数‘(红、绿、蓝)’的RGB元组或整数‘(红、绿、蓝、阿尔法)`的RGBA元组。在此属性上设置Alpha将更改整个形状的Alpha,包括填充和边框。
每个颜色分量必须在0(暗)到255(饱和)的范围内。
- 类型:
(int,int)
- class Triangle(x, y, x2, y2, x3, y3, color=(255, 255, 255, 255), batch=None, group=None)
基类:
ShapeBase
- x2
形状的第二个X坐标。
- 类型:
整型或浮点型
- y2
形状的第二个Y坐标。
- 类型:
整型或浮点型
- x3
形状的第三个X坐标。
- 类型:
整型或浮点型
- y3
形状的第三个Y坐标。
- 类型:
整型或浮点型