I like clocks, timers and callbacks, Irv. I would not like to make a
game without them. :)
Here's a game clock. It's a little dated, but solid.
https://bitbucket.org/gummbum/gummworld2/src/e7dc9eaf45f3f6809d097968c0cb32edf6ea3bb9/gamelib/gummworld2/gameclock.py
Here's a newer game clock. It's not as proven in the field as the
previous one, but it seems solid so far. You can use it without the
library by removing the imports, and modifying the missing dependencies
if you use them (I think there is like one or two SDL2 dependencies).
https://bitbucket.org/gummbum/gsdl2/src/7d5ce4d43cc64141085048ad6ba55b67fb23da2d/gsdl2/_time.py
Here is another game clock by not-Gumm. So you got a few choices.
https://bitbucket.org/dr0id/pyknic/src/008f5a9b811e7c3f5199b2b534f0d796297daab2/pyknic/pyknic/timing.py
Maybe you'll find something here that suits your needs.
Gumm
On 2/10/2017 5:30 PM, Irv Kalb wrote:
I've been working on porting a game that I wrote in a different
language/environment, and I want to make the Python/PyGame code as efficiently
as possible.
I have code in an object, that triggers an event that should occur, say 1
second later. I used pygame.set_timer, created an ID and set 1000
milliseconds. However, in order to catch that event, I need to have code in my
main loop, which passes down knowledge of that event (potentially through
several layers of objects), all the way back down to my object that created the
timer. I have this working, but it doesn't feel like a very clean
implementation.
As another very simple example, imagine that I wanted to make a generic
animation object that would run an animation off of a list of images at some
given speed (independent of the frame rate). Ideally, I want to have a timer
that tells the current instance of the animation object to change to the next
image - directly. It seems like this type of thing should be easy within a
single class, but in practice, as I said, I have had to add code to several
layers to get notification back to animation object(s) where I really want to
get notification.
So, my specific question is this: Is there any way for an object to create a
timer, that results to a callback within the same object?
If not, I guess one way to do this would be to have a central Timer Manager object that I
could call to set up a timer, and pass it a timer ID, amount of time to wait, (just like
a call to set_timer), but also pass in a reference to the current object (self), and a
method name to call back (maybe "animate"). Then, in the main loop, I could
call the Timer Manager with all events, it would check if the event ID matched any it
should be looking for, and if so, call the appropriate method of the given object. I'm
sure I could make it work, but I'm wondering if I am missing something simple.
Thanks in advance,
Irv
PS: Still in Python 2, but I am seriously looking into potentially porting
code to Python 3. In fact, I had a talk with my manager at one of my colleges
today about it.