如何:pyglet中基元的gridarray和形状的动态填充

2024-09-28 20:54:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在网络模拟工具中可视化网络负载。我想将网络节点显示为一个窗口中的正方形网格(例如,4x4网格网络),在此窗口中,我可以根据节点上的流量单独选择每个正方形的填充颜色(流量信息必须从转储文件中读取,但这将在以后使用)。我想看看能不能用pyglet。例如,假设我们有一个2x2网络(4个正方形)。我想要一个2x2的正方形矩阵,我可以改变每个元素的填充颜色。我是初学者,到目前为止,我已经学会了画一个单一的填充正方形后,查阅了大量的参考资料。参见代码:

import sys, time, math, os, random
from pyglet.gl import *

window = pyglet.window.Window()

label = pyglet.text.Label('Simulation', 
                          font_name='Times New Roman', 
                          font_size=16,
                          color=(204,204,0,255),      #red font (255,0,0) opacity=255
                          x=window.width, y=window.height,
                          anchor_x='right', anchor_y='top') 

class FilledSquare:
    def __init__(self, width, height, xpos, ypos):
        self.xpos = xpos
        self.ypos = ypos
        self.angle = 0
        self.size = 1
        x = width/2.0
        y = height/2.0
        self.vlist = pyglet.graphics.vertex_list(4, ('v2f', [-x,-y, x,-y, -x,y, x,y]), ('t2f', [0,0, 1,0, 0,1, 1,1]), ('c3B',(0,255,0,0,255,0,0,255,0,0,255,0)))
    def draw(self,w,h,x,y):
        self.width=w
        self.height=h
        self.xpos=x
        self.ypos=y
        glPushMatrix()
        glTranslatef(self.xpos, self.ypos, 0)
        self.vlist.draw(GL_TRIANGLE_STRIP)        
        glPopMatrix()


@window.event
def on_draw():
    window.clear()
    glClearColor(0, 0.3, 0.5, 0)
    glClear(GL_COLOR_BUFFER_BIT)
    label.draw()
    square1.draw(60,60,460,240)


square1 = FilledSquare(30, 30, 100, 200)
pyglet.app.run()
  1. 我认为这样,填充颜色在初始化时是静态的。我怎样才能通过填充颜色?在
  2. 如何创建一个二维的FilledSquare数组(可能是类似square[i][j]的东西,我可以在循环中访问它。在

如果有可能,请告诉我。在


Tags: self网络节点颜色defwindowwidthpyglet
1条回答
网友
1楼 · 发布于 2024-09-28 20:54:22

我从pyglet用户google组的Adam那里得到了一些建议。它们在这里:

  1. 由于您使用的是立即模式,所以您最好不要将顶点颜色作为顶点列表的一部分传递,而是在绘制之前使用glColor*设置颜色

    glColor3f(0.0, 1.0, 0.0)

  2. 您可以将对象放入列表中,这样就可以

squares = [[FilledSquare(...), FilledSquare(...)], [FilledSquare(...), FilledSquare(...)]]

谢谢亚当(如果你看到这个),真的很有用。在

相关问题 更多 >