|
| 1 | +import argparse |
| 2 | +import logging |
| 3 | +from pathlib import Path |
| 4 | +from textwrap import dedent |
| 5 | + |
| 6 | + |
| 7 | +MAIN_TEMPLATE = dedent( |
| 8 | + """\ |
| 9 | + import pygame |
| 10 | + import spritePro as s |
| 11 | +
|
| 12 | + ASSETS_DIR = "assets" |
| 13 | + IMAGES_DIR = f"{ASSETS_DIR}/images" |
| 14 | + AUDIO_DIR = f"{ASSETS_DIR}/audio" |
| 15 | +
|
| 16 | +
|
| 17 | + class MainScene(s.Scene): |
| 18 | + def on_enter(self, context): |
| 19 | + self.player = s.Sprite(f"{IMAGES_DIR}/player.png", (64, 64), (400, 300), speed=5) |
| 20 | +
|
| 21 | + def update(self, dt): |
| 22 | + self.player.handle_keyboard_input() |
| 23 | + if s.input.was_pressed(pygame.K_SPACE): |
| 24 | + s.debug_log_info("Space pressed") |
| 25 | +
|
| 26 | +
|
| 27 | + def main(): |
| 28 | + s.get_screen((800, 600), "My SpritePro Game") |
| 29 | + s.enable_debug(True) |
| 30 | + s.set_debug_camera_input(3) |
| 31 | + s.debug_log_info("Game started") |
| 32 | + s.set_scene(MainScene()) |
| 33 | +
|
| 34 | + while True: |
| 35 | + s.update(fill_color=(20, 20, 30)) |
| 36 | +
|
| 37 | +
|
| 38 | + if __name__ == "__main__": |
| 39 | + main() |
| 40 | + """ |
| 41 | +) |
| 42 | + |
| 43 | + |
| 44 | +def _project_root_from_target(target: Path) -> tuple[Path, Path]: |
| 45 | + if target.suffix == ".py": |
| 46 | + return target.parent, target |
| 47 | + return target, target / "main.py" |
| 48 | + |
| 49 | + |
| 50 | +def create_project(target: Path) -> Path: |
| 51 | + project_root, main_file = _project_root_from_target(target) |
| 52 | + |
| 53 | + project_root.mkdir(parents=True, exist_ok=True) |
| 54 | + assets_root = project_root / "assets" |
| 55 | + assets_root.mkdir(exist_ok=True) |
| 56 | + (assets_root / "audio").mkdir(exist_ok=True) |
| 57 | + (assets_root / "images").mkdir(exist_ok=True) |
| 58 | + (project_root / "scenes").mkdir(exist_ok=True) |
| 59 | + |
| 60 | + if not main_file.exists(): |
| 61 | + main_file.write_text(MAIN_TEMPLATE, encoding="utf-8") |
| 62 | + |
| 63 | + return project_root |
| 64 | + |
| 65 | + |
| 66 | +def main() -> None: |
| 67 | + parser = argparse.ArgumentParser(description="Create a minimal SpritePro project") |
| 68 | + parser.add_argument( |
| 69 | + "--create", |
| 70 | + metavar="PATH", |
| 71 | + help="Project folder or path to main.py", |
| 72 | + ) |
| 73 | + args = parser.parse_args() |
| 74 | + |
| 75 | + if not args.create: |
| 76 | + args.create = "main.py" |
| 77 | + |
| 78 | + logging.basicConfig(level=logging.INFO, format="%(message)s") |
| 79 | + project_root = create_project(Path(args.create)) |
| 80 | + logging.info("Project created at: %s", project_root.resolve()) |
| 81 | + |
| 82 | + |
| 83 | +if __name__ == "__main__": |
| 84 | + main() |
0 commit comments