Permalink
Browse files

Merge branch 'master' of github.com:HampshireCS/CS112-Spring2012

  • Loading branch information...
2 parents 1788a53 + 3c8bad6 commit ae6d71ced0e9bfc5ef17edf1ef0dfaba8d3ac95d Alec Goebel committed Apr 26, 2012
Showing with 41 additions and 0 deletions.
  1. +41 −0 samplecode/signals/signals.py
View
41 samplecode/signals/signals.py
@@ -0,0 +1,41 @@
+"""
+A basic example showing how one might take advantage of using
+PyDispatcher/Louie. To run this example, you must run:
+ sudo easy_install louie
+
+Why use signals? What is this? for now, start here: http://pydispatcher.sourceforge.net/
+"""
+
+from louie import dispatcher
+
+class Player(object):
+ def __init__(self, name):
+ self.name = name
+
+ # connect this object's functions to certain signals
+ dispatcher.connect(self.move, signal="move_player", sender=self.name)
+ dispatcher.connect(self.hear, signal="listen_to")
+
+ def hear(self, sender, message):
+ if sender is not self:
+ print self.name, "hears", sender.name, "say", message
+
+ def move(self, x, y):
+ print "moving", self.name, "to", x, y
+
+ def shout(self, message):
+ dispatcher.send(signal="listen_to", message=message, sender=self)
+
+# create a bunch of players
+players = [ Player(name) for name in ["Alice", "Bob", "Jack", "Jill"] ]
+
+# moves the proper object
+dispatcher.send(signal="move_player", sender="Alice", x=5, y=8)
+dispatcher.send(signal="move_player", sender="Alice", x=0, y=2)
+dispatcher.send(signal="move_player", sender="Bob", x=-1, y=5)
+
+# does nothing because there is no carol
+dispatcher.send(signal="move_player", sender="Carol", x=-1, y=5)
+
+# says the same thing to every player
+players[0].shout("Hi guys!")

0 comments on commit ae6d71c

Please sign in to comment.