Skip to content

Instantly share code, notes, and snippets.

@acbart
Created November 6, 2019 06:17
Show Gist options
  • Select an option

  • Save acbart/372d83ba2c2cbcd95c8dd79008c48c84 to your computer and use it in GitHub Desktop.

Select an option

Save acbart/372d83ba2c2cbcd95c8dd79008c48c84 to your computer and use it in GitHub Desktop.
"""
This file contains a basic Arcade Window class. It is necessary
to run your game.
You do not need to open or read this file. It must
be in the same folder as your other files.
Change Log:
- 0.0.1: Initial version
"""
__version__ = '0.0.1'
import arcade
class Cisc108Game(arcade.Window):
"""
An Arcade Window subclass that allows you to specify its
functions and is built around a World data model.
Args:
window_width (int): The width of the game window.
window_height (int): The height of the game window.
window_caption (str): The title of the game window.
an_initial_world (World): The initial state of the world.
draw_world (World->None): A function that draws a world.
update_world (World->None): A function that updates the world.
handle_key (World,int->None): A function that handles keyboard input.
handle_mouse (World,int,int,str->None): A function that handles mouse clicks.
handle_motion (World,int,int->None): A function that handles mouse movement.
Attributes:
world (World): The current state of the world.
"""
def __init__(self, window_width, window_height, window_caption,
an_initial_world, draw_world, update_world,
handle_key, handle_mouse, handle_motion=None):
super().__init__(window_width, window_height, window_caption)
self.world = an_initial_world
self.draw_world = draw_world
self.update_world = update_world
self.handle_key = handle_key
self.handle_mouse = handle_mouse
self.handle_motion = handle_motion
def on_draw(self):
""" Called when it is time to draw the world """
arcade.start_render()
self.draw_world(self.world)
def on_update(self, delta_time: float):
""" Called every frame """
self.update_world(self.world)
def on_key_press(self, key: int, modifiers: int):
""" Called when the keyboard is pressed """
self.handle_key(self.world, key)
def on_mouse_press(self, x: int, y: int, button: int, modifiers: int):
""" Called when the mouse is pressed """
button_str = ('left' if button == arcade.MOUSE_BUTTON_LEFT
else 'right' if button == arcade.MOUSE_BUTTON_RIGHT
else 'middle' if button == arcade.MOUSE_BUTTON_MIDDLE
else 'unknown')
self.handle_mouse(self.world, x, y, button_str)
def on_mouse_motion(self, x: int, y: int, dx: int, dy: int):
""" Called when the mouse is moved """
if self.handle_motion is not None:
self.handle_motion(self.world, x, y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment