[Tutor] Working with some sort of Timer

Ryan Strunk ryan.strunk at gmail.com
Thu Feb 17 04:33:29 CET 2011


Hello everyone,

I'm a first-time poster and--in fact--first time programmer, so a lot of
this is new to me. I hope to learn a lot about programming from this list,
and I hope you all won't be shy about such feedback as "you're top-posting
again, silly newbie."

I am currently in the midst of producing an audio-only boxing game similar
to Nintendo's Mike Tyson's Punch-Out!!, and I'm running into difficulty when
it comes to working with timed events.

I have begun developing a round class whose job it is to keep an eye on the
clock and the match as a whole, checking for things like how long a fighter
has been lying on the canvas. The relevant code currently looks like this:

def heartbeat(self):
   if self.round_clock_counter > self.round_clock_max:
       #todo, call end_round
       return
   if global_vars.player.fatigue < 100:
       global_vars.player.fatigue += 1
   self.round_clock = delay(1, heartbeat)

(My delay function simply encompasses calling a Timer and starting it.)

I am hoping to use the above-referenced self.round_clock as a measuring
stick to call various punching methods which I programmed into an Opponent
class. For example: at 1 minute, 30 seconds, perform maneuver x. It strikes
me that using threading.Timer to set up all of the possible maneuvers would
be an inelegant solution to the problem, but I'm not certain which other
modules/functions I could use to better carry this out.

My first question, then, pertains to the above implementation of a round
clock. I know that this code can work, and it has the additional benefit of
eventually keeping the round alive even when a fighter is being counted
after the bell rings. At the same time, however, the code carries the burden
of being infinitely recursive without the proper checks in place.
Nevertheless, would the code generally be considered acceptable, or is there
a more elegant solution I should use?

Second, I would welcome any suggestions anyone has as to how I might time
events within a round. Even pointers to specific modules or sections of the
Python documentation would be greatly appreciated.

Thank you all for any help you can provide, and I look forward to learning
and growing with everyone.

Best,
Ryan



More information about the Tutor mailing list