|
| 1 | +"""Timestep is a module based on Pygame that implements a decoupled update and rendering to make any code framerate independant. Based on https://gafferongames.com/post/fix_your_timestep/#the-final-touch. Created so you never have to worry about multiplying by deltatime again.""" |
| 2 | + |
| 3 | +import pygame |
| 4 | +import time |
| 5 | + |
| 6 | + |
| 7 | +class Character: |
| 8 | + """Base class for any sprites or objects""" |
| 9 | + |
| 10 | + def __init__(self, x: int, y: int, image: pygame.Surface) -> None: |
| 11 | + self.image = image |
| 12 | + self.rect = self.image.get_rect() |
| 13 | + self.rect.topleft = x, y |
| 14 | + self.vel = pygame.math.Vector2(0, 0) |
| 15 | + self.prev_pos = pygame.math.Vector2(self.rect.topleft) |
| 16 | + |
| 17 | + def update(self) -> None: |
| 18 | + """Update """ |
| 19 | + self.prev_pos = self.__get_rect_pos() |
| 20 | + |
| 21 | + def draw(self, surface: pygame.Surface, alpha: float) -> None: |
| 22 | + pos = self.prev_pos.lerp(self.__get_rect_pos(), alpha) |
| 23 | + surface.blit(self.image, pos) |
| 24 | + |
| 25 | + def __get_rect_pos(self) -> pygame.math.Vector2: |
| 26 | + """Return the position of the Charater's rect as a Vec2""" |
| 27 | + return pygame.math.Vector2(self.rect.topleft) |
| 28 | + |
| 29 | + |
| 30 | +class Timestep: |
| 31 | + """Class that does all the calculations to make the game framerate independant""" |
| 32 | + |
| 33 | + def __init__(self, step: float) -> None: |
| 34 | + self.__step = step |
| 35 | + self.__accumulator = 0 |
| 36 | + self.__last_time = time.time() |
| 37 | + |
| 38 | + def __calc_dt(self) -> float: |
| 39 | + now = time.time() |
| 40 | + dt = now - self.__last_time |
| 41 | + self.__last_time = now |
| 42 | + return dt |
| 43 | + |
| 44 | + def update(self) -> None: |
| 45 | + """Override this function with event loop and movement""" |
| 46 | + pass |
| 47 | + |
| 48 | + def render(self, alpha: float) -> None: |
| 49 | + """Override this function with blits and drawing of sprites""" |
| 50 | + pass |
| 51 | + |
| 52 | + def run_game(self) -> None: |
| 53 | + """Updates and renders the game framerate independantly. Should only be called within a while loop.""" |
| 54 | + dt = self.__calc_dt() |
| 55 | + self.__accumulator += dt |
| 56 | + |
| 57 | + while self.__accumulator >= self.__step: |
| 58 | + self.update() |
| 59 | + |
| 60 | + self.__accumulator -= self.__step |
| 61 | + |
| 62 | + alpha = self.__accumulator / self.__step |
| 63 | + |
| 64 | + self.render(alpha) |
0 commit comments