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

运行效果:

本文完。

目录
相关文章
|
2月前
|
XML 搜索推荐 Java
Android App开发之自定义图形中位图与图形互转、剪裁图形内部区域、给图形添加部件的讲解及实战(附源码 简单易懂)
Android App开发之自定义图形中位图与图形互转、剪裁图形内部区域、给图形添加部件的讲解及实战(附源码 简单易懂)
42 0
|
2月前
LabVIEW通过编程将图形类控件的X轴显示为时间戳
LabVIEW通过编程将图形类控件的X轴显示为时间戳
18 0
|
2月前
|
API C语言 图形学
EasyX图形库学习(一、窗口创建函数initgraph、背景颜色设置setbkcolor、图形绘制函数)
EasyX图形库学习(一、窗口创建函数initgraph、背景颜色设置setbkcolor、图形绘制函数)
|
8月前
|
开发者 Windows
EasyX趣味化编程note2,绘制基本图形(上)
EasyX趣味化编程note2,绘制基本图形
50 0
|
8月前
EasyX趣味化编程note2,绘制基本图形(下)
EasyX趣味化编程note2,绘制基本图形(上)
85 0
|
XML 前端开发 数据可视化
【图形基础篇】03 # 声明式图形系统:如何用SVG图形元素绘制可视化图表?
【图形基础篇】03 # 声明式图形系统:如何用SVG图形元素绘制可视化图表?
109 0
【图形基础篇】03 # 声明式图形系统:如何用SVG图形元素绘制可视化图表?
|
定位技术
Threejs使用Shapes实现不规则几何体,自定义绘图
Threejs使用Shapes实现不规则几何体,自定义绘图
847 0
Threejs使用Shapes实现不规则几何体,自定义绘图
|
计算机视觉
Qt实用技巧:组合图形的比例变换
Qt实用技巧:组合图形的比例变换
Qt实用技巧:组合图形的比例变换
|
程序员 C语言 开发工具
Qt编写自定义控件59-直方动态图
一、前言 直方动态图类似于音乐播放时候的柱状图展示,顶部提供一个横线条,当柱状上升的时候,该线条类似于帽子的形式冲到顶端,相当于柱状顶上去的感觉,给人一种动态的感觉,听音乐的同时更加赏心悦目,原理比较简单,就是用2个定时器,一个定时器间隔比较短,负责快速把柱状图从底部冲到设置的值,同时横线条跟随一起冲上去,一个定时器负责慢慢的跌落值到0,然后横线条缓慢下降,下降速度比柱状图的速度要慢一些,产生一种对比的效果,看起来更像是跌落的感觉。
1089 0