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.
Window
from pyglet import *
from pyglet.gl import *
from pyglet.window import Window
new_window = Window(width=640, height=480)
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')
@new_window.event
def on_draw():
new_window.clear()
label.draw()
@new_window.event
def on_key_press(symbol, modifiers):
print(chr(symbol), "key pressed")
@new_window.event
def on_hide():
print("Window is minimized")
pyglet.app.run()
In the above code, we created a new window using the window function. We then assigned a key press event using the on_key_press function.
We can hide the mouse cursor using the set_exclusive_mouse function, for example.
from pyglet import *
from pyglet.gl import *
from pyglet.window import Window
new_window = Window(fullscreen=True)
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')
new_window.set_exclusive_mouse(True)
@new_window.event
def on_draw():
new_window.clear()
label.draw()
pyglet.app.run()
We can also work with multiple screens using the window function in pyglet. We can use the following code snippet to access multiple screens,
display = pyglet.canvas.get_display()
screens = display.get_screens()
windows = []
for screen in screens:
windows.append(window.Window(fullscreen=True, screen=screen))
We can further set and fetch the location value of the window using the set_location and get_location functions. For example,
from pyglet import *
from pyglet.gl import *
from pyglet.window import Window
new_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')
new_window.set_location(500, 100)
@new_window.event
def on_draw():
new_window.clear()
label.draw()
value = new_window.get_location()
print(value)
pyglet.app.run()
Developers can also access the cursor details using the on_mouse_motion and on_mouse_press functions. For example,
from pyglet import *
from pyglet.gl import *
from pyglet.window import Window
new_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')
@new_window.event
def on_draw():
new_window.clear()
label.draw()
@new_window.event
def on_mouse_motion(x, y, dx, dy):
print(x,",",y)
@new_window.event
def on_mouse_press(x, y, button, modifiers):
if(button == 1):
print("Left Mouse Button Pressed")
else:
print("Right Mouse Button Pressed")
pyglet.app.run()
Developers can access many more similar attributes present in the window function.