From: Kevin A. <ka...@us...> - 2004-09-25 21:47:50
|
Update of /cvsroot/pythoncard/PythonCard/samples/gravity In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv4104 Modified Files: gravity.py Log Message: switched to using new util.time function changed drawing to throttle to a specific number of frames per second Index: gravity.py =================================================================== RCS file: /cvsroot/pythoncard/PythonCard/samples/gravity/gravity.py,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** gravity.py 25 Sep 2004 21:12:12 -0000 1.2 --- gravity.py 25 Sep 2004 21:47:36 -0000 1.3 *************** *** 12,20 **** # contains some cruft ! from PythonCard import clipboard, dialog, graphic, model import wx import os import random - import time --- 12,19 ---- # contains some cruft ! from PythonCard import clipboard, dialog, graphic, model, util import wx import os import random *************** *** 128,148 **** avgfps = 0 frame = 0 # hack to force a difference ! # between time.time() and startTime # to avoid a division by zero exception on fast machines # aka my Windows box ;-) ! startTime = time.time() - 0.001 while self.animate: ! canvas.clear() ! for ball in self.sprites: ! ball.move() ! if wx.Platform == '__WXMAC__': ! canvas.redraw() ! else: ! canvas.refresh() ! #time.sleep(.01) # give the user a chance to click Stop - frame += 1 - self.statusBar.text = "Average FPS: %f" % (frame / (time.time() - startTime)) wx.Yield() --- 127,160 ---- avgfps = 0 frame = 0 + + seconds = util.time() + # number of frames to display a second + # on faster boxes this will throttle the number + # of frames calculated and displayed, but on slower + # boxes it won't prevent slowdown + fps = 30 + # amount of time that should pass before drawing another frame + timePerFrame = 1.0 / fps + print timePerFrame + # hack to force a difference ! # between time and startTime # to avoid a division by zero exception on fast machines # aka my Windows box ;-) ! startTime = util.time() - 0.001 while self.animate: ! newSeconds = util.time() ! if newSeconds - seconds >= timePerFrame: ! seconds = newSeconds ! canvas.clear() ! for ball in self.sprites: ! ball.move() ! if wx.Platform == '__WXMAC__': ! canvas.redraw() ! else: ! canvas.refresh() ! frame += 1 ! self.statusBar.text = "Average FPS: %f" % (frame / (seconds - startTime)) # give the user a chance to click Stop wx.Yield() |