From 5eb52fd0cf77c8bcadcde2ed2a98f592b82ec93c Mon Sep 17 00:00:00 2001 From: James Turk Date: Mon, 22 Apr 2024 01:35:14 -0500 Subject: [PATCH] switching --- src/doodles/examples/circles.py | 4 +-- src/doodles/main.py | 43 ++++++++++++++++++++++++++------- src/doodles/world.py | 3 +++ 3 files changed, 39 insertions(+), 11 deletions(-) diff --git a/src/doodles/examples/circles.py b/src/doodles/examples/circles.py index 5cdc805..b81f443 100644 --- a/src/doodles/examples/circles.py +++ b/src/doodles/examples/circles.py @@ -5,6 +5,6 @@ c = Circle() g = Group().pos(400, 300) for r in range(20, 50, 5): - Circle(g).radius(r).color(*Color.random()).z_index(-r) + Circle(g).radius(r).color(Color.random()).z_index(-r) for r in range(60, 150, 10): - Circle(g).radius(r).color(*Color.random()).z_index(-r) + Circle(g).radius(r).color(Color.random()).z_index(-r) diff --git a/src/doodles/main.py b/src/doodles/main.py index f4bc92f..00d235e 100644 --- a/src/doodles/main.py +++ b/src/doodles/main.py @@ -1,8 +1,5 @@ import sys -import time -import copy -import random -import math +from pathlib import Path import pygame import importlib import typer @@ -11,11 +8,17 @@ from .world import world FPS = 60 MS_PER_FRAME = 1000 / 60 -def main(modname: str): - pygame.init() - world.init() - pygame.display.set_caption("Doodles") +def get_examples(): + module_path = Path(__file__).parent / "examples" + submodules = [ + file.stem for file in module_path.glob("*.py") if file.name != "__init__.py" + ] + return submodules + +def load_module(modname): + pygame.display.set_caption("doodles: " + modname) + world.clear() try: # try fully-qualified first mod = importlib.import_module(modname) @@ -24,7 +27,22 @@ def main(modname: str): try: mod = importlib.import_module("doodles.examples." + modname) except ImportError: - raise ImportError(f"Tried to import {modname} and doodles.examples.{modname}") + raise ImportError( + f"Tried to import {modname} and doodles.examples.{modname}" + ) + return mod + + +def main(modname: str = None): + pygame.init() + world.init() + + examples = get_examples() + ex_index = 0 + if modname: + load_module(modname) + else: + load_module(examples[ex_index]) elapsed = last_update = 0 @@ -33,6 +51,13 @@ def main(modname: str): if event.type == pygame.QUIT: pygame.quit() sys.exit() + elif event.type == pygame.KEYDOWN: + if event.key == pygame.K_RIGHT: + ex_index = (ex_index + 1) % len(examples) + load_module(examples[ex_index]) + elif event.key == pygame.K_LEFT: + ex_index = (ex_index - 1) % len(examples) + load_module(examples[ex_index]) elapsed = pygame.time.get_ticks() - last_update while elapsed > MS_PER_FRAME: elapsed -= MS_PER_FRAME diff --git a/src/doodles/world.py b/src/doodles/world.py index 1cf1d9c..ca77c30 100644 --- a/src/doodles/world.py +++ b/src/doodles/world.py @@ -54,6 +54,9 @@ class World: raise ValueError("Can't initialize world twice!") self.screen = pygame.display.set_mode((world.WIDTH, world.HEIGHT)) + def clear(self): + self._drawables = [] + def add(self, drawable): self._drawables.append(drawable)