Quote:
|
Originally Posted by Sane
Okay. I finished it. The ratio variable needs a bit of tweaking depending on the ranges of speed you put in, but overall it's pretty good. Any more suggestions or noticable changes it needs?
|
This is a prime situation where objects would be useful - I'd do something along the lines of:
class Actor(object):
def __init__(self, name, speed):
self.speed = speed
# create the generator and bind the next method to self.update
# so that whenever self.update() is called, it calls the next method
# on the generator
self.update = self._update().next
self.name = name
def act(self):
print " >", self.name
def _update(self):
while True:
for i in range(self.speed):
yield None
yield self.act # return a reference which is called later
john = Actor("John", 5)
mary = Actor("Mary", 8)
goblin1 = Actor("Goblin1", 7)
goblin2 = Actor("Goblin2", 6)
actors = [john, mary, goblin1, goblin2]
for i in range(50):
print "Turn",i+1
for actor in actors:
func = actor.update()
if func: # If actor.update() doesn't return None...
func() # call the function (ie self.act) it returns Note how each object maintains its own state - and determines when and how it acts...
--OH.