氣輕pyglet22 導入字體
?
Pyglet可以導入字體,函數(shù)font.add_file(fontname)可導入指定字體,函數(shù)font.add_directory(dirname)可導入文件夾dirname中左右字體。
?
from pyglet.gl import *
from pyglet.window import mouse
from pyglet import font
import os
##from WindowShot import *
?
WIDTH = 800
HEIGHT = 600
?
wtitlt = 'pyglet font'
?
class Window(pyglet.window.Window):
?
??? def __init__(self,*args,**kwargs):
??????? super().__init__(*args,**kwargs)
??????? self.batch = pyglet.graphics.Batch()
??????? pyglet.clock.schedule_interval(self.callback,1/30)
?
??????? font.add_directory('C:\Windows\Fonts')????????? # 添加指定文件夾中字體
??????? self.LabelList = []
??????? self.ShowList = []
??????? self.labelshow = LabelManager()
??????? self.labelshow.LabelAppend(self.LabelList,5)
?
??????? self.labelshow.LabelDraw(self.LabelList,self.ShowList,self.batch)
?
??? def on_mouse_press(self, x, y, symbol, modifiers):
??????? if symbol == mouse.LEFT:
??????????? self.close()
?
??? def on_draw(self):
??????? window.clear()
??????? self.batch.draw()
?
??? def callback(self,dt):
??????? for s in self.ShowList:
??????????? s.delete()
??????? self.labelshow.LabelDraw(self.LabelList,self.ShowList,self.batch)
??????? self.labelshow.LabelAppend(self.LabelList,dt)
?
??????? if self.labelshow.i == 4 and self.LabelList[-1].finish:
??????????? pyglet.app.exit()
?
class LabelManager:
??? def __init__(self):
??????? self.dt = 0
??????? self.pos = [[800,?? 0, 200, 500],
??????????????????? [800, 800, 350, 350],
??????????????????? [0, 0, 500, 200],
??????????????????? [0, 600, 650,? 50]]
??????? self.i = 0
??????? self.label = ['中文字體 華文彩云','中文字體 華文琥珀','日本語フォント','日本語フォント']
??????? self.fnames = ['華文彩云',
????????????????? '華文琥珀',
????????????????? 'MS PMincho',
????????????????? 'UD Digi Kyokasho N-B',
????????????????? ]
??????? 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,batch):
??????? for i, t in enumerate(labelslist):
???????? ???t.OnShow(showlist,self.fnames[i],self.c[i],self.label[i],batch)
?
class LabelGenerator:
??? def __init__(self,coordinate):
??????? self.x,self.y,self.tx,self.ty = coordinate
??????? self.inc = 5
??????? self.finish = False
?
??? def OnShow( self,LabelBuf,fname,c,label,batch ):
??????? label = pyglet.text.Label(label, font_name=fname,
??????????? font_size=26, color=c, x=self.x, y=self.y,
??????????? anchor_x='center', anchor_y='center', batch=batch)
??????? LabelBuf.append(label)
?
??????? if self.x != self.tx:
??????????? self.x = MovetoSite( self.tx,self.x,self.inc )
??????? if self.y != self.ty:
??????????? self.y = MovetoSite( self.ty,self.y,self.inc )
??????? if self.x == self.tx and self.y == self.ty:
??????????? self.finish = True
?
def MovetoSite( p0,p,dp ):
?
??? newp = p - (p-p0)/abs(p-p0)*dp
??? if abs(newp - p0) < dp:
??????? newp = p0
?
??? return newp
?
if __name__ == '__main__':
??? window = Window(width=WIDTH,height=HEIGHT,caption=wtitlt)
??? window.set_location(200, 200)
??? pyglet.gl.glClearColor(0.8,0.8,0.8,0)
??? pyglet.app.run()
?
執(zhí)行結(jié)果
