Pyglet控件的批处理参数batch和分组参数group简析

先来复习一下之前写的两个例程: 

1. 绘制网格线

import pygletwindow = pyglet.window.Window(800, 600)
color = (255, 255, 255, 255)  # 白色
lines = []for y in range(0, window.height, 40):lines.append(pyglet.shapes.Line(0, y, window.width, y, color=color))
for x in range(0, window.width, 40):lines.append(pyglet.shapes.Line(x, 0, x, window.height, color=color))@window.event
def on_draw():window.clear()for line in lines:line.draw()pyglet.app.run()

2. 模拟按钮

import pygletwindow = pyglet.window.Window(800, 600)GRAY = (220, 220, 220, 255)
BLACK = (0, 0, 0, 255)
RED = (255, 0, 0, 255)pattern = pyglet.image.SolidColorImagePattern(color=GRAY)
image = pattern.create_image(100,50)
button1 = pyglet.sprite.Sprite(image, 280, 200)
button2 = pyglet.sprite.Sprite(image, 280+150, 200)
labelb1 = pyglet.text.Label('按钮1', font_size=16, x=305, y=216, color=BLACK)
labelb2 = pyglet.text.Label('按钮2', font_size=16, x=455, y=216, color=BLACK)
message = pyglet.text.Label('', font_size=16, x=20, y=16)
widgets = [message]
widgets.extend([button1, button2, labelb1, labelb2])@window.event
def on_draw():window.clear()for widget in widgets:widget.draw()@window.event
def on_mouse_press(x, y, button, modifier):X, Y, W, H = button1.x, button1.y, button1.width, button1.heightif x in range(X,X+W+1) and y in range(Y,Y+H+1):message.text = '你点击了按钮1'elif x in range(X+150,X+W+151) and y in range(Y,Y+H+1):message.text = '你点击了按钮2'@window.event
def on_mouse_motion(x, y, dx, dy):X, Y, W, H = button1.x, button1.y, button1.width, button1.heightif x in range(X,X+W+1) and y in range(Y,Y+H+1):labelb1.color = REDelif x in range(X+150,X+W+151) and y in range(Y,Y+H+1):labelb2.color = REDelse:labelb1.color = BLACKlabelb2.color = BLACKpyglet.app.run()

以上两个例程,都使用了控件列表,然后在on_draw事件中遍历,基本上实现了“批处理”画图的功能。但是,实现上pyglet库本身就带有这种批处理功能,这就是各个控件都带有的batch参数。看一个pyglet自带的实例:

import pyglet
from pyglet import shapeswindow = 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()

是不是更简单,使用了pyglet.graphics.Batch()对象,把需要一起画的控件都装进Batch()里,然后再一起画出来。与以下代码的效果是一样的,但省掉了widgets元组和遍历过程,还是原生的批处理参数batch更简单。

import pyglet
from pyglet import shapeswindow = 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()

接下来说说group分组参数,先看一个实例:

import pygletwindow = pyglet.window.Window(800, 500 , caption='Batch&Group')
batch = pyglet.graphics.Batch()  # 创建一个Batch对象
group = [pyglet.graphics.Group(i) for i in range(4)]   # 创建一组Group对象circle1 = pyglet.shapes.Circle(x=250, y=250, radius=80, color=(255, 0, 0, 255), batch=batch, group=group[0])
rectangle1 = pyglet.shapes.Rectangle(x=150, y=200, width=200, height=100, color=(0, 0, 255, 255), batch=batch, group=group[1])circle2 = pyglet.shapes.Circle(x=550, y=250, radius=80, color=(255, 0, 0, 255), batch=batch, group=group[3])
rectangle2 = pyglet.shapes.Rectangle(x=450, y=200, width=200, height=100, color=(0, 0, 255, 255), batch=batch, group=group[2])@window.event
def on_draw():window.clear()batch.draw()  # 在窗口上绘制batch中的所有图形对象pyglet.app.run()

运行效果:

 group参数主要用于控制控件的显示顺序,与pyglet.graphics.Group(order=i)配合使用,这里order数值越大越在上层,越小则越在下层。当然group除了排序的功能,还有包括同组绑定纹理、着色器或设置任何其他参数的功能,这些功能以后再安排学习。

到此已基本了解pyglet控件参数batch和group的用法,再来看一下graphics.Batch和graphics.Group的自带说明:

class Batch

Help on class Batch in module pyglet.graphics:

class Batch(builtins.object)
Manage a collection of drawables for batched rendering.

Many drawable pyglet objects accept an optional `Batch` argument in their
constructors. By giving a `Batch` to multiple objects, you can tell pyglet
that you expect to draw all of these objects at once, so it can optimise its
use of OpenGL. Hence, drawing a `Batch` is often much faster than drawing
each contained drawable separately.

The following example creates a batch, adds two sprites to the batch, and
then draws the entire batch::

    batch = pyglet.graphics.Batch()
    car = pyglet.sprite.Sprite(car_image, batch=batch)
    boat = pyglet.sprite.Sprite(boat_image, batch=batch)

    def on_draw():
        batch.draw()

While any drawables can be added to a `Batch`, only those with the same
draw mode, shader program, and group can be optimised together.

Internally, a `Batch` manages a set of VertexDomains along with
information about how the domains are to be drawn. To implement batching on
a custom drawable, get your vertex domains from the given batch instead of
setting them up yourself.

Methods defined here:

__init__(self)
    Create a graphics batch.

draw(self)
    Draw the batch.

draw_subset(self, vertex_lists)
    Draw only some vertex lists in the batch.

    The use of this method is highly discouraged, as it is quite
    inefficient.  Usually an application can be redesigned so that batches
    can always be drawn in their entirety, using `draw`.

    The given vertex lists must belong to this batch; behaviour is
    undefined if this condition is not met.

    :Parameters:
        `vertex_lists` : sequence of `VertexList` or `IndexedVertexList`
            Vertex lists to draw.

get_domain(self, indexed, mode, group, program, attributes)
    Get, or create, the vertex domain corresponding to the given arguments.

invalidate(self)
    Force the batch to update the draw list.

    This method can be used to force the batch to re-compute the draw list
    when the ordering of groups has changed.

    .. versionadded:: 1.2

migrate(self, vertex_list, mode, group, batch)
    Migrate a vertex list to another batch and/or group.

    `vertex_list` and `mode` together identify the vertex list to migrate.
    `group` and `batch` are new owners of the vertex list after migration.

    The results are undefined if `mode` is not correct or if `vertex_list`
    does not belong to this batch (they are not checked and will not
    necessarily throw an exception immediately).

    `batch` can remain unchanged if only a group change is desired.

    :Parameters:
        `vertex_list` : `~pyglet.graphics.vertexdomain.VertexList`
            A vertex list currently belonging to this batch.
        `mode` : int
            The current GL drawing mode of the vertex list.
        `group` : `~pyglet.graphics.Group`
            The new group to migrate to.
        `batch` : `~pyglet.graphics.Batch`
            The batch to migrate to (or the current batch).

----------------------------------------------------------------------
Data descriptors defined here:

__dict__
    dictionary for instance variables (if defined)

__weakref__
    list of weak references to the object (if defined)

class Group

Help on class Group in module pyglet.graphics:

class Group(builtins.object)
Group(order=0, parent=None)

Group of common OpenGL state.

`Group` provides extra control over how drawables are handled within a
`Batch`. When a batch draws a drawable, it ensures its group's state is set;
this can include binding textures, shaders, or setting any other parameters.
It also sorts the groups before drawing.

In the following example, the background sprite is guaranteed to be drawn
before the car and the boat::

    batch = pyglet.graphics.Batch()
    background = pyglet.graphics.Group(order=0)
    foreground = pyglet.graphics.Group(order=1)

    background = pyglet.sprite.Sprite(background_image, batch=batch, group=background)
    car = pyglet.sprite.Sprite(car_image, batch=batch, group=foreground)
    boat = pyglet.sprite.Sprite(boat_image, batch=batch, group=foreground)

    def on_draw():
        batch.draw()

:Parameters:
    `order` : int
        Set the order to render above or below other Groups.
        Lower orders are drawn first.
    `parent` : `~pyglet.graphics.Group`
        Group to contain this Group; its state will be set before this
        Group's state.

:Variables:
    `visible` : bool
        Determines whether this Group is visible in any of the Batches
        it is assigned to. If ``False``, objects in this Group will not
        be rendered.
    `batches` : list
        Read Only. A list of which Batches this Group is a part of.

Methods defined here:

__eq__(self, other)
    Return self==value.

__hash__(self)
    Return hash(self).

__init__(self, order=0, parent=None)
    Initialize self.  See help(type(self)) for accurate signature.

__lt__(self, other)
    Return self<value.

__repr__(self)
    Return repr(self).

set_state(self)
    Apply the OpenGL state change.

    The default implementation does nothing.

set_state_recursive(self)
    Set this group and its ancestry.

    Call this method if you are using a group in isolation: the
    parent groups will be called in top-down order, with this class's
    `set` being called last.

unset_state(self)
    Repeal the OpenGL state change.

    The default implementation does nothing.

unset_state_recursive(self)
    Unset this group and its ancestry.

    The inverse of `set_state_recursive`.

----------------------------------------------------------------------
Readonly properties defined here:

batches

order

----------------------------------------------------------------------
Data descriptors defined here:

__dict__
    dictionary for instance variables (if defined)

__weakref__
    list of weak references to the object (if defined)

visible

最后,使用Batch()修改一下之前两个例程:

import pygletwindow = pyglet.window.Window(800, 600, caption='绘制网络线')
color = (255, 255, 255, 255)
lines = pyglet.graphics.Batch()for y in range(0, window.height, 40):exec(f'y{y//40} = pyglet.shapes.Line(0, y, window.width, y, color=color, batch=lines)')
for x in range(0, window.width, 40):exec(f'x{x//40} = pyglet.shapes.Line(x, 0, x, window.height, color=color, batch=lines)')@window.event
def on_draw():window.clear()lines.draw()pyglet.app.run()
import pygletwindow = pyglet.window.Window(800, 600, caption='按钮模拟')GRAY = (220, 220, 220, 255)
BLACK = (0, 0, 0, 255)
RED = (255, 0, 0, 255)batch = pyglet.graphics.Batch()
pattern = pyglet.image.SolidColorImagePattern(color=GRAY)
image = pattern.create_image(100,50)
button1 = pyglet.sprite.Sprite(image, 280, 200, batch = batch)
button2 = pyglet.sprite.Sprite(image, 280+150, 200, batch = batch)
labelb1 = pyglet.text.Label('按钮1', font_size=16, x=305, y=216, color=BLACK, batch = batch)
labelb2 = pyglet.text.Label('按钮2', font_size=16, x=455, y=216, color=BLACK, batch = batch)
message = pyglet.text.Label('', font_size=16, x=20, y=16, batch = batch)@window.event
def on_draw():window.clear()batch.draw()@window.event
def on_mouse_press(x, y, button, modifier):X, Y, W, H = button1.x, button1.y, button1.width, button1.heightif x in range(X,X+W+1) and y in range(Y,Y+H+1):message.text = '你点击了按钮1'elif x in range(X+150,X+W+151) and y in range(Y,Y+H+1):message.text = '你点击了按钮2'@window.event
def on_mouse_motion(x, y, dx, dy):X, Y, W, H = button1.x, button1.y, button1.width, button1.heightif x in range(X,X+W+1) and y in range(Y,Y+H+1):labelb1.color = REDelif x in range(X+150,X+W+151) and y in range(Y,Y+H+1):labelb2.color = REDelse:labelb1.color = BLACKlabelb2.color = BLACKpyglet.app.run()

完。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/704684.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

LeetCode704. 二分查找(C++)

LeetCode704. 二分查找 题目链接代码 题目链接 https://leetcode.cn/problems/binary-search/description/ 代码 class Solution { public:int search(vector<int>& nums, int target) {int left 0;int right nums.size() - 1;while(left < right){int midd…

外包工作两个月,技术退步让我决心改变

大家好&#xff0c;我是一名大专生&#xff0c;2019年通过校招进入了湖南的一家软件公司。在这里&#xff0c;我从事了接近4年的功能测试工作。然而&#xff0c;今年8月份&#xff0c;我深刻地意识到&#xff0c;我不能继续这样下去了。 长时间在一个舒适的环境里&#xff0c;…

数据库系统概论(超详解!!!) 第一节 绪论

1.四个基本概念 1.数据&#xff08;Data&#xff09; 数据&#xff08;Data&#xff09;是数据库中存储的基本对象 数据的定义&#xff1a;描述事物的符号记录 数据的种类&#xff1a;数字、文字、图形、图像、音频、视频、学生的档案记录等 数据的含义称为数据的语义&…

记生产OOM的故障分析

一、引言 生产上告警&#xff0c;交易堵塞&#xff0c;服务无响应&#xff0c;使用jstack、jmap、jhat命令进行故障分析。 Java虚拟机&#xff08;Java Virtual Machine&#xff0c;简称JVM&#xff09;作为Java语言的核心组件&#xff0c;为Java程序提供了运行环境和内存管理…

docker存储驱动

目录 一、写时复制和用时分配 二、联合文件系统 2.1、aufs ​编辑 2.2、分层的问题 2.3、overlay 2.4 文件系统区别 三、容器跑httpd案例 3.1、案例1&#xff1a;端口映射 3.2、案例2&#xff1a;制作httpd应用镜像 3.3、案例3&#xff1a;docker数据卷挂载 3.4、案…

【hot100】跟着小王一起刷leetcode -- 49. 字母异位词分组

【【hot100】跟着小王一起刷leetcode -- 49. 字母异位词分组 49. 字母异位词分组题目解读解题思路代码实现 总结 49. 字母异位词分组 题目解读 49. 字母异位词分组 ok&#xff0c;兄弟们&#xff0c;咱们来看看这道题&#xff0c;很明显哈&#xff0c;这里的关键词是字母异位…

《最新出炉》系列初窥篇-Python+Playwright自动化测试-27-处理单选和多选按钮-番外篇

1.简介 前边几篇文章是宏哥自己在本地弄了一个单选和多选的demo&#xff0c;然后又找了网上相关联的例子给小伙伴或童鞋们演示了一下如何使用playwright来处理单选按钮和多选按钮进行自动化测试&#xff0c;想必大家都已经掌握的八九不离十了吧。这一篇其实也很简单&#xff1a…

浅谈 TCP 三次握手

文章目录 三次握手 三次握手 首先我们需要明确&#xff0c;三次握手的目的是什么&#xff1f; 是为了通信双方之间建立连接&#xff0c;然后传输数据。 那么建立连接的条件是什么呢&#xff1f; 需要确保通信的双方都确认彼此的接收和发送能力正常&#xff0c;满足这个条件&a…

今天面了个字节拿 38K 出来的测试,让我见识到了基础的天花板

最近内卷严重&#xff0c;各种跳槽裁员&#xff0c;相信很多小伙伴也在准备金九银十的面试计划。 作为一个入职5年的老人家&#xff0c;目前工资比较乐观&#xff0c;但是我还是会选择跳槽&#xff0c;因为感觉在一个舒适圈待久了&#xff0c;人过得太过安逸&#xff0c;晋升涨…

Jeecg项目部署

说明&#xff1a;Jeecg是一款低代码开发平台&#xff0c;简单说是一款现成的项目&#xff0c;该项目集成了许多功能&#xff0c;我们可以在这个项目之上开发自己的业务代码。 本文介绍Jeecg项目的部署&#xff0c;包括后端jeecg-boot项目、前端vue3项目。前端项目在本地Window…

Java的编程之旅19——使用idea对面相对象编程项目的创建

在介绍面向对象编程之前先说一下我们在idea中如何创建项目文件 使用快捷键CtrlshiftaltS新建一个模块&#xff0c;点击“”&#xff0c;再点New Module 点击Next 我这里给Module起名叫OOP,就是面向对象编程的英文缩写&#xff0c;再点击下面的Finish 点Apply或OK均可 右键src…

2024Python自动化测试面试必备知识点!

在准备 Python 自动化测试面试时&#xff0c;以下是一些必备的知识点&#xff0c;可以帮助您在面试中展现实力&#xff1a; 软件测试基础&#xff1a; 熟悉软件测试的基本概念&#xff0c;包括测试类型&#xff08;功能测试、性能测试、安全测试等&#xff09;、测试方法&#…

数据安全治理实践路线(中)

数据安全建设阶段主要对数据安全规划进行落地实施&#xff0c;建成与组织相适应的数据安全治理能力&#xff0c;包括组织架构的建设、制度体系的完善、技术工具的建立和人员能力的培养等。通过数据安全规划&#xff0c;组织对如何从零开始建设数据安全治理体系有了一定认知&…

微服务篇之任务调度

一、xxl-job的作用 1. 解决集群任务的重复执行问题。 2. cron表达式定义灵活。 3. 定时任务失败了&#xff0c;重试和统计。 4. 任务量大&#xff0c;分片执行。 二、xxl-job路由策略 1. FIRST&#xff08;第一个&#xff09;&#xff1a;固定选择第一个机器。 2. LAST&#x…

西门子S7-1500作为智能设备共享功能

本章节介绍了共享设备的功能&#xff0c;优势&#xff0c;使用要求&#xff0c;使用规则&#xff0c;如何将智能设备作为共享设备&#xff0c;实现一个智能设备同时与2个IO控制器进行通信的示例&#xff0c;以及常见问题。 一、共享设备功能概述 信号模块可以被不同的IO控制器…

【MIT-PHP-推荐】imi-ai 是一个 ChatGPT 开源项目

mi-ai 是一个 ChatGPT 开源项目&#xff0c;支持聊天、问答、写代码、写文章、做作业等功能。 项目架构合理&#xff0c;代码编写优雅&#xff0c;简单快速部署。前后端代码完全开源&#xff0c;不管是学习自用还是商用二开都很适合。 本项目现已支持 ChatGPT 聊天 AI 和 Emb…

都说了别用BeanUtils.copyProperties,这不翻车了吧

分享是最有效的学习方式。 博客&#xff1a;https://blog.ktdaddy.com/ 故事 新年新气象&#xff0c;小猫也是踏上了新年新征程&#xff0c;自从小猫按照老猫给的建议【系统梳理大法】完完整整地梳理完毕系统之后&#xff0c;小猫对整个系统的把控可谓又是上到可一个新的高度。…

yolov8学习笔记(二)模型训练

目录 yolov8的模型训练 1、制作数据集&#xff08;标记数据集&#xff09; 2、模型训练&#xff08;标记数据集、参数设置、跟踪模型随时间的性能变化&#xff09; 2.1、租服务器训练 2.2、加训练参数 2.3、看训练时的参数&#xff08;有条件&#xff0c;就使用TensorBoard&…

Open CASCADE学习|视图

目录 Mainwin.h Mainwin.cpp Mainwin.h ​#pragma once#include <QtWidgets/QMainWindow>#include "Displaywin.h"#include "OCC.h"class Mainwin : public QMainWindow{ Q_OBJECTpublic: Mainwin(QWidget* parent nullptr); ~Mainwin();​pri…

【Java程序设计】【C00277】基于Springboot的招生管理系统(有论文)

基于Springboot的招生管理系统&#xff08;有论文&#xff09; 项目简介项目获取开发环境项目技术运行截图 项目简介 这是一个基于Springboot的招生管理系统 本系统分为系统功能模块、管理员功能模块以及学生功能模块。 系统功能模块&#xff1a;在系统首页可以查看首页、专业…