Introduction
Pyglet is a multimedia and cross-platform gaming package in Python. Pyglet can be used to create games and other visual applications. The font function in pyglet is used to load any system-installed fonts. The developer can also add additional third-party fonts using the attributes present in the fonts function.
Font
A simple pyglet code will look something like this,
import pyglet
new_window = pyglet.window.Window()
label = pyglet.text.Label('Hey Ninja!', bold = True, font_name ='Cooper', font_size = 16, x = new_window.width//2, y = new_window.height//2, anchor_x ='center', anchor_y ='center')
label1 = pyglet.text.Label('This is a new window created using pyglet', font_name ='Cooper', font_size = 12, x = new_window.width//2, y = new_window.height//2-40, anchor_x ='center', anchor_y ='center')
@new_window.event
def on_draw():
new_window.clear()
label.draw()
label1.draw()
pyglet.app.run()
Let us add a new font using the font function.
from pyglet import *
from pyglet.gl import *
font.add_file(os.path.join(os.path.dirname(os.path.abspath(__file__)), "Zuka Doodle.ttf"))
new_window = pyglet.window.Window()
label = pyglet.text.Label('Hey Ninja!', bold = True, font_name ='Cooper', font_size = 16, x = new_window.width//2, y = new_window.height//2, anchor_x ='center', anchor_y ='center')
label1 = pyglet.text.Label('This is a new window created using pyglet', font_name ='Cooper', font_size = 12, x = new_window.width//2, y = new_window.height//2-40, anchor_x ='center', anchor_y ='center')
PlayLabel = pyglet.text.Label('New Font', font_name='Zuka Doodle', font_size=20, x=new_window.width // 2, y=new_window.height//2-80, anchor_x='center', anchor_y='center')
@new_window.event
def on_draw():
new_window.clear()
label.draw()
label1.draw()
PlayLabel.draw()
pyglet.app.run()
In this example, we downloaded an external font and then added it to our system using the add_file attribute of the font function.
We can also use the have_font method to verify if a font is present in the system.
from pyglet import *
font.add_file(os.path.join(os.path.dirname(os.path.abspath(__file__)), "Zuka Doodle.ttf"))
print(font.have_font("Zuka Doodle"))