Permalink
Browse files

added classcode from friday 3/30

  • Loading branch information...
1 parent ae315f2 commit 244e7b351b7f87b737abd8ca3328cd86e610fe5d Alec Goebel committed Mar 30, 2012
@@ -0,0 +1 @@
+Class code from friday lecture on managers and observers 3/30
@@ -0,0 +1,41 @@
+import pygame
+from pygame.locals import *
+
+class KeyListener(object):
+ def on_keydown(self, event): pass
+ def on_keyup(self, event): pass
+
+
+class MouseListener(object):
+ def on_buttondown(self, event): pass
+ def on_buttonup(self, event): pass
+ def on_motion(self, event): pass
+
+class InputManager(object):
+ def __init__(self):
+ self._key = []
+ self._mouse = []
+
+ def add_mouse_listener(self, listener):
+ self._mouse.append(listener)
+
+ def add_key_listener(self, listener):
+ self._key.append(listener)
+
+ def handle_event(self, event):
+ if event.type == KEYDOWN:
+ for listener in self._key:
+ listener.on_keydown(event)
+ elif event.type == KEYUP:
+ for listener in self._key:
+ listener.on_keyup(event)
+
+ elif event.type == MOUSEBUTTONDOWN:
+ for listener in self._mouse:
+ listener.on_buttondown(event)
+ elif event.type == MOUSEBUTTONUP:
+ for listener in self._mouse:
+ listener.on_buttonup(event)
+ elif event.type == MOUSEMOTION:
+ for listener in self._mouse:
+ listener.on_motion(event)
@@ -0,0 +1,100 @@
+#!/usr/bin/env python
+
+import pygame
+from pygame.locals import *
+
+from core.input import InputManager, KeyListener, MouseListener
+
+# world/player.py
+class Player(object):
+ def move(self, direction):
+ print "player moves %d, %d" % direction
+
+ def jump(self):
+ print "player jumps"
+
+
+# core/sound.py
+class SoundManager(object):
+ def play(self, which):
+ print "playing %s sound" % which
+
+
+# controls/player.py
+class PlayerController(KeyListener, MouseListener):
+ def __init__(self, player):
+ self.player = player
+
+ def on_keydown(self, event):
+ if event.key == K_SPACE:
+ self.player.jump()
+
+ def on_motion(self, event):
+ self.player.move( event.rel )
+
+
+
+# controls/sound.py
+class SfxController(KeyListener):
+ def __init__(self, sm, game):
+ self.game = game
+ self.sm = sm
+
+ def on_keydown(self, event):
+ if self.game.paused():
+ return
+
+ if event.key == K_SPACE:
+ self.sm.play("jump")
+
+ if event.key == K_c:
+ self.sm.play("slash")
+
+
+
+class Game(object):
+ size = 800, 600
+
+ def __init__(self):
+ pygame.init()
+ self.screen = pygame.display.set_mode(self.size)
+
+ self.input = InputManager()
+
+ self.player = Player()
+ self.sounds = SoundManager()
+
+ pc = PlayerController(self.player)
+ self.input.add_key_listener(pc)
+ self.input.add_mouse_listener(pc)
+
+ sc = SfxController(self.sounds, self)
+ self.input.add_key_listener(sc)
+
+ def paused(self):
+ return False
+
+
+ def quit(self):
+ self._done = True
+
+ def run(self):
+ self._done = False
+
+ while not self._done:
+ for event in pygame.event.get():
+ if event.type == QUIT:
+ self.quit()
+ else:
+ self.input.handle_event(event)
+
+ # update
+
+ # draw
+ self.screen.fill((0,0,0))
+ pygame.display.flip()
+
+
+if __name__ == "__main__":
+ game = Game()
+ game.run()

0 comments on commit 244e7b3

Please sign in to comment.