Better time handling: configurable ticks per second, and no dropped movement.

Note: the original method involves moving on only when update() is called, which is called only between frames. Hence less than the ticks per second setting if it can't paint as fast. This made movement completely FPS independent.

TODO: remove update, merge into on_draw.
This commit is contained in:
Quantum 2013-11-04 17:00:15 -05:00
parent 2a7e089edb
commit 6173cbe094
3 changed files with 17 additions and 9 deletions

View file

@ -3,13 +3,20 @@ INITIAL_WIN_HEIGHT = 540
INITIAL_WIN_WIDTH = 700
WIN_TITLE = "Punyverse"
def main():
import argparse
parser = argparse.ArgumentParser(prog='punyverse', description='Python simulator of a puny universe.')
parser.add_argument('-t', '--ticks', help='Ticks per second for game, more means more responsive, but '
' may run slower, default is 20.', default=20, type=int)
args = parser.parse_args()
import pyglet
from punyverse.game import Applet
from punyverse import game
pyglet.options['shadow_window'] = False
game.TICKS_PER_SECOND = args.ticks
Applet(width=INITIAL_WIN_WIDTH, height=INITIAL_WIN_HEIGHT, caption=WIN_TITLE, resizable=True, vsync=0)
game.Applet(width=INITIAL_WIN_WIDTH, height=INITIAL_WIN_HEIGHT, caption=WIN_TITLE, resizable=True, vsync=0)
pyglet.app.run()

View file

@ -47,10 +47,10 @@ class Applet(pyglet.window.Window):
if key.S in self.keys:
cam.roll -= 4
if self.moving:
cam.move(self.speed)
cam.move(int(self.speed * dt * TICKS_PER_SECOND + 0.5))
if self.running:
self.world.tick += self.tick
self.world.tick += int(self.tick * dt * TICKS_PER_SECOND + 0.5)
for entity in self.world.tracker:
entity.update()
@ -73,8 +73,8 @@ class Applet(pyglet.window.Window):
self.atmosphere = True
self.cloud = not texture.badcard
self.tick = self.world.tick_length
self.ticks = [20, 40, 60, # Second range
self.tick = self.world.tick_length / TICKS_PER_SECOND
self.ticks = [1, 2, 5, 10, 20, 40, 60, # Second range
120, 300, 600, 1200, 1800, 2700, 3600, # Minute range
7200, 14400, 21600, 43200, 86400, # Hour range
172800, 432000, 604800, # 2, 5, 7 days
@ -83,7 +83,8 @@ class Applet(pyglet.window.Window):
63072000, 157680000, 315360000, # 2, 5, 10 years
630720000, 1576800000, 3153600000, # 20, 50, 100 years
]
self.ticks = [i / 20 for i in self.ticks]
self.ticks = [i / TICKS_PER_SECOND for i in self.ticks]
self.ticks = sorted(set(i for i in self.ticks if i))
self.__time_per_second_cache = None
self.__time_per_second_value = None

View file

@ -11,7 +11,7 @@
"model": "used to load a wavefront object instead of a textured sphere"
},
"au": 10000,
"tick": 180,
"tick": 3600,
"length": 63.7,
"bodies": {
"sun": {