I gave diordnas proposal a spin, and ran into some strange errors.
When taking a closer look at the code, I realized that I did not
understand what (s)he was doing.
So I adjusted the skeletton to my level of python skills and even got
it to work (in a 2 second test..)
Here it goes:
import pyglet.clock, time
class GameClock(pyglet.clock.Clock):
def __init__(self):
self.dt = 0.0
self.paused = False
self.pause_start = 0
self.total_pause_time = 0
super(GameClock, self).__init__(time_function=self.game_time)
pyglet.clock.set_default(self)
def tick(self,poll=False):
self.dt = super(GameClock,self).tick(poll)
if self.paused: self.total_pause_time += self.dt
def game_time(self):
if self.paused:
return self.pause_start
else:
return time.time() - self.total_pause_time
def pause(self):
if not self.paused:
self.pause_start = time.time()
self.toggle_pause()
def unpause(self):
if self.paused:
self.toggle_pause()
def toggle_pause(self):
self.paused = not self.paused
I do not know if one approach is better. what is a drawback of mine is
that I store the total pause time and substract it every time I return
the current time. So if there are long periods of pause, a high number
has to be substracted each frame when the game runs again. The game
"stays" in the past so to speak. I can't think of any other way to do
it atm, but this may be because of my bad design with scheduling
callbacks.
Cheers
Bastian
On 18 Jul., 21:00, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote:
> I just did some actual testing and came up with a better solution that
> actually works. Drop it in 'gameclock.py', import it, and then you can
> gameclock.pause(), gameclock.unpause(), and gameclock.toggle_pause()
> at will. I also included gameclock().dt(), because I like to treat
> delta_t as a read-only global.
>
> import pyglet.clock, time
>
> class GameClock(pyglet.clock.Clock):
> def __init__(self):
> self.dt = 0.0
> self.paused = False
> self.pause_start = 0
> self.total_pause_time = 0
> super(GameClock, self).__init__(time_function=self.game_time)
>
> def tick(self,poll=False):
> self.dt = super(GameClock,self).tick(poll)
> if self.dt > 0.2: self.dt = 0 #sanity check
> if self.paused: self.dt = 0
>
> def game_time(self):
> if self.paused:
> return self.pause_start - self.total_pause_time
> else:
> return time.time() - self.total_pause_time
>
> def pause(self):
> if not self.paused: self.toggle_pause
>
> def unpause(self):
> if self.paused: self.toggle_pause()
>
> def toggle_pause(self):
> self.paused = not self.paused
> if self.paused: self.total_pause_time += time.time() -
> self.pause_start
> else: self.pause_start = time.time()
>
> #convenience functions
> def dt(): return pyglet.clock.get_default().dt
> def toggle_pause(): pyglet.clock.get_default().toggle_pause()
> def pause(): pyglet.clock.get_default().pause()
> def unpause(): pyglet.clock.get_default().unpause()
>
> pyglet.clock.set_default(GameClock())
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"pyglet-users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/pyglet-users?hl=en
-~----------~----~----~----~------~----~------~--~---