Skip to content

Instantly share code, notes, and snippets.

@LennyPhoenix
Last active June 1, 2020 19:52
Show Gist options
  • Save LennyPhoenix/f18b0f484c185181c1092dd8f59ca438 to your computer and use it in GitHub Desktop.
Save LennyPhoenix/f18b0f484c185181c1092dd8f59ca438 to your computer and use it in GitHub Desktop.
Basic application template in python with pyglet.
import pyglet
# Define the application
class Application:
# Define what should be created on initialisation
def __init__(self):
# Create a window
self.window = pyglet.window.Window()
# Push the application as an event handler.
self.window.push_handlers(self)
# Create an empty list of sprites.
self.sprites = []
# Create the draw-batch.
self.batch = pyglet.graphics.Batch()
# Helpful function to add/create a sprite.
def addSprite(self, img, x, y):
# Create the sprite and add it to the draw-batch.
sprite = pyglet.sprite.Sprite(img, x, y, batch=self.batch)
# Add the sprite to our list we made earlier.
self.sprites.append(sprite)
# Draw event, called every draw-frame.
def on_draw(self):
# Clear the window.
self.window.clear()
# Draw everything using the batch.
self.batch.draw()
# TO ADD AN EVENT:
def EVENT_NAME_HERE(EVENT, ARGS, HERE):
# Code to run when this event happens.
pass
# Run the application.
def run(self):
pyglet.app.run()
# Check if the script is being run directly
if __name__ == "__main__":
# Create an application instance.
application = Application()
# Run the application.
application.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment