From b9d246819464873a241861737225b1fbd3db8173 Mon Sep 17 00:00:00 2001 From: James Turk Date: Mon, 22 Apr 2024 19:57:41 -0500 Subject: [PATCH] rects --- src/doodles/examples/rects.py | 13 ++++++++++--- src/doodles/main.py | 16 +--------------- src/doodles/world.py | 18 ++++++++++++++++-- 3 files changed, 27 insertions(+), 20 deletions(-) diff --git a/src/doodles/examples/rects.py b/src/doodles/examples/rects.py index b3e0c9b..b672152 100644 --- a/src/doodles/examples/rects.py +++ b/src/doodles/examples/rects.py @@ -1,5 +1,12 @@ -from doodles import Rectangle +from doodles import Rectangle, Color, world +import random def create(): - for _ in range(100): - Rectangle().random(250) + for _ in range(25): + Rectangle().random(200).color(Color.BLACK).z_index(10) + Rectangle().random(150).color(Color.DARK_BLUE).z_index(15) + Rectangle().random(100).color(Color.DARK_GREY).z_index(20) + Rectangle().random(50).color(Color.LIGHT_GREY).z_index(30) + # Rectangle().random(250).color( + # random.choice((Color.BLACK, Color.LIGHT_GREY, Color.DARK_GREY, Color.WHITE)) + # ) diff --git a/src/doodles/main.py b/src/doodles/main.py index 52d734e..382a79b 100644 --- a/src/doodles/main.py +++ b/src/doodles/main.py @@ -5,9 +5,6 @@ import importlib import typer from .world import world -FPS = 60 -MS_PER_FRAME = 1000 / 60 - def get_examples(): module_path = Path(__file__).parent / "examples" @@ -34,7 +31,6 @@ def load_module(modname): def main(modname: str = None): - pygame.init() world.init() examples = get_examples() @@ -44,9 +40,6 @@ def main(modname: str = None): else: load_module(examples[ex_index]) - elapsed = 0 - clock = pygame.time.Clock() - while True: for event in pygame.event.get(): if event.type == pygame.QUIT: @@ -59,14 +52,7 @@ def main(modname: str = None): elif event.key == pygame.K_LEFT: ex_index = (ex_index - 1) % len(examples) load_module(examples[ex_index]) - elapsed += clock.tick(FPS) - while elapsed > MS_PER_FRAME: - elapsed -= MS_PER_FRAME - world.tick() - world.render() - #print(clock.get_fps()) - pygame.display.flip() - + world.update() if __name__ == "__main__": typer.run(main) diff --git a/src/doodles/world.py b/src/doodles/world.py index ca77c30..53036b4 100644 --- a/src/doodles/world.py +++ b/src/doodles/world.py @@ -34,6 +34,9 @@ class World: WIDTH = 800 HEIGHT = 600 + FPS = 60 + MS_PER_FRAME = 1000 / FPS + _instance = None def __init__(self): @@ -52,7 +55,10 @@ class World: """ if self.screen: raise ValueError("Can't initialize world twice!") + pygame.init() self.screen = pygame.display.set_mode((world.WIDTH, world.HEIGHT)) + self.clock = pygame.time.Clock() + self._elapsed = 0 def clear(self): self._drawables = [] @@ -64,13 +70,21 @@ class World: for d in self._drawables: d.update() - def render(self): + def update(self): """ - Draw world to screen + Update & draw world to screen. """ + # update + self._elapsed += self.clock.tick(self.FPS) + while self._elapsed > self.MS_PER_FRAME: + self._elapsed -= self.MS_PER_FRAME + self.tick() + + # rendering self.screen.fill(self.background_color) for d in sorted(self._drawables, key=lambda d: d._z_index): d.draw(self.screen) + pygame.display.flip() # our singleton instance