Pyglet Starter
import pyglet
from pyglet.shapes import Rectangle
# Create a window
window = pyglet.window.Window(width=800, height=600, caption='My Game')
# Create a rectangle
rect = Rectangle(x=50, y=50, width=200, height=100, color=(50, 225, 30))
@window.event
def on_draw():
window.clear()
rect.draw()
@window.event
def on_key_press(symbol, modifiers):
if symbol == pyglet.window.key.RIGHT:
rect.x += 10 # Move the rectangle to the right
# Start the application
pyglet.app.run()
To enhance the simple Pyglet game so that clicking on the rectangle increments and displays a counter on the game window, we need to add a few more features. We’ll handle mouse click events, check if the click is within the rectangle’s bounds, and then update and display a counter accordingly.
Updated Python Script for the Simple Game with Click Interaction
import pyglet
from pyglet.shapes import Rectangle
from pyglet.text import Label
# Create a window
window = pyglet.window.Window(width=800, height=600, caption='My Game')
# Create a rectangle
rect = Rectangle(x=50, y=50, width=200, height=100, color=(50, 225, 30))
# Initialize a counter
counter = 0
# Create a label to display the counter
counter_label = Label(f'Count: {counter}', font_size=18, x=window.width // 2, y=window.height - 30, anchor_x='center')
@window.event
def on_draw():
window.clear()
rect.draw()
counter_label.draw()
@window.event
def on_key_press(symbol, modifiers):
if symbol == pyglet.window.key.RIGHT:
rect.x += 10 # Move the rectangle to the right
@window.event
def on_mouse_press(x, y, button, modifiers):
# Check if the click is within the rectangle's bounds
if (rect.x <= x <= rect.x + rect.width) and (rect.y <= y <= rect.y + rect.height):
global counter
counter += 1 # Increment the counter
counter_label.text = f'Count: {counter}' # Update the label text
# Start the application
pyglet.app.run()
Explanation of the Updated Features
- Label for Displaying the Counter:
- We import
Label
frompyglet.text
to create a text label. counter_label
is initialized with a starting text that displays the counter. It is positioned at the top center of the window.
- Handling Mouse Clicks:
- The
on_mouse_press
function is added to handle mouse click events. It receives parametersx
andy
which are the coordinates of the mouse click, andbutton
andmodifiers
which provide additional information about which mouse button was clicked and whether any keyboard modifiers were held down. - Inside this function, there’s a check to determine if the click occurred within the bounds of the rectangle. This is done by comparing the click coordinates
x
andy
with the rectangle’s position and dimensions. - If the click is within the rectangle, the counter is incremented and the label text is updated to reflect the new count.
Running the Updated Game
With these changes, clicking on the rectangle in the game window will increment a counter displayed at the top of the window. This simple interaction introduces you to handling mouse events and updating visual elements based on user interactions, which are common tasks in game development.
Make sure you have Pyglet installed and run this script in a Python environment. This example will help you understand basic event handling and display updates in Pyglet, which you can build upon for more complex game functionalities.