Skip to content

Instantly share code, notes, and snippets.

@aperley
Created November 20, 2014 02:14
Show Gist options
  • Save aperley/7c520b1d8c4c2d906e45 to your computer and use it in GitHub Desktop.
Save aperley/7c520b1d8c4c2d906e45 to your computer and use it in GitHub Desktop.
Modular Graphics
class Game(AnimationClass):
def __init__:
self.gameObjects = ...
def onTimerFired:
for obj in self.gameObjects:
obj.update()
for otherObj in self.gameObjects:
if obj == otherObj: continue
if obj.collide(otherObj):
handle collision here...
def redrawAll:
for obj in self.gameObjects:
obj.draw(self.canvas)
def onKeyPressed:
figure out what we want to do
if involvesPlayer:
player.doThing
elif involves something else
self.doThatThing
class GameObject(object):
def draw(self, canvas):
raise NotImplementedError
def update(self):
self.x += self.dx
self.y += self.dy
other updating here...
def collide(self, other):
return true if bounding boxes collide
class Player(GameObject):
implement player-specific stuff
class UserControlledPlayer(GameObject):
implement stuff for the player controlled by arrow keys
class Zombie(Player):
implemented zombie stuff, maybe subclass this for different zombies
class Obstacle(GameObject):
implement obstacle-specific stuff
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment