气轻pyglet01标签(Label)

import pyglet

from pyglet import shapes

 

class LabelGenerator:

    def __init__(self,coordinate):

        self.x,self.y,self.limit = coordinate

        self.inc = 5

 

    def OnShow( self,LabelBuf,fsize,c,label ):

        ita = False

        if ‘斜体’ in label:

            ita = True

        label = pyglet.text.Label(label, font_name=’Times New Roman’,

            font_size=fsize, color=c, x=self.x, y=self.y, italic = ita,

            anchor_x=’center’, anchor_y=’center’, batch=batch)

        LabelBuf.Append(label)

 

        if self.x < self.limit:

            self.x += self.inc

 

class LabelManager:

    def __init__(self):

        self.dt = 0

        self.pos = [[100, 500, 500],

                    [100, 400, 600],

                    [100, 300, 300],

                    [100, 200, 400]]

        self.i = 0

        self.label = [‘pyglet.text.Label’,’改变颜色’,’改变字体大小’,’采用斜体’]

        self.fsize = [18,22,26,30]

        self.c = [(192, 100, 0,255),(0, 255, 0,255),

                  (0, 0, 255,255),(0, 100, 100,255)]

 

    def LabelAppend( self,labelslist,dt ):

        self.dt += dt

        n = len(labelslist)

        if self.dt > 4 and n < 4:

            self.dt = 0

            labelslist.Append(LabelGenerator(self.pos[self.i]))

            self.i += 1

 

    def LabelDraw(self,labelslist,showlist):

        for i, t in enumerate(labelslist):

            t.OnShow(showlist,self.fsize[i],self.c[i],self.label[i])

 

WIDTH = 800

HEIGHT = 600

 

title = “pyglet label”

 

window = pyglet.window.Window(WIDTH, HEIGHT, title)

window.set_location(100, 100)

 

pyglet.gl.glClearColor(0.8,0.8,0.8,0)

batch = pyglet.graphics.Batch()

 

LabelList = []

ShowList = []

labelshow = LabelManager()

labelshow.LabelAppend(LabelList,5)

 

labelshow.LabelDraw(LabelList,ShowList)

 

@window.event

def on_draw():

   

    window.clear()

    batch.draw()

   

def callback(dt):

    for s in ShowList:

        s.delete()

    labelshow.LabelDraw(LabelList,ShowList)

    labelshow.LabelAppend(LabelList,dt)

 

pyglet.clock.schedule_interval(callback,1/30)

 

pyglet.App.run()

 

执行结果

 

资源下载: