From 672b5c0462bc461a735c0666abce1bd655945f22 Mon Sep 17 00:00:00 2001 From: Quantum Date: Mon, 4 Nov 2013 17:00:15 -0500 Subject: [PATCH] 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. --- punyverse/__main__.py | 13 ++++++++++--- punyverse/game.py | 11 ++++++----- punyverse/world.json | 2 +- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/punyverse/__main__.py b/punyverse/__main__.py index 8c31774..fc4c679 100644 --- a/punyverse/__main__.py +++ b/punyverse/__main__.py @@ -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() diff --git a/punyverse/game.py b/punyverse/game.py index a4a8433..3f2c56d 100644 --- a/punyverse/game.py +++ b/punyverse/game.py @@ -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 diff --git a/punyverse/world.json b/punyverse/world.json index 84ed40f..9178e08 100644 --- a/punyverse/world.json +++ b/punyverse/world.json @@ -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": {