Python : Pausable clock

Class inheriting from sf.Clock allowing to pause the timer.

class PausableClock

from PySFML import sf
 
class PausableClock(sf.Clock):
 
	def __init__(self):
		self.elapsed_time = 0
		self.pause = False
		sf.Clock.__init__(self)
 
	def Pause(self):
		if self.pause:
			sf.Clock.Reset(self)
			self.pause = False
		else:
			self.elapsed_time = self.elapsed_time + sf.Clock.GetElapsedTime(self)
			self.pause = True
 
	def Reset(self):
		self.elapsed_time = 0
		self.pause = False
		sf.Clock.Reset(self)
 
	def GetElapsedTime(self):
		if self.pause:
			return self.elapsed_time
		else:
			return self.elapsed_time + sf.Clock.GetElapsedTime(self)

Notes

timer.GetElapsedTime() and sf.Clock.GetElapsedTime(timer) :

  • If timer is a sf.Clock instance, these instructions are the same;
  • If timer is a PausableClock instance, the first instruction calls the method we defined while the latter calls the method defined by PySFML.

Enhancements

You can easily modify this class to suit your needs, for example with separate Play and Pause methods or with a Stop method.

 
en/sources/py_pausable_clock.txt · Last modified: 2008/07/30 13:59 by remi.k2620
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki