From 685f5bc6866de3dfc8c865be442ef9cfb67e1a19 Mon Sep 17 00:00:00 2001 From: Auric Vente Date: Thu, 18 Jul 2024 02:14:03 -0600 Subject: [PATCH] Mods --- .gitignore | 2 +- config.py | 4 --- ants.py => cromulant/ants.py | 4 +-- cromulant/config.py | 17 +++++++++++++ database.py => cromulant/database.py | 6 +++-- cromulant/game.py | 11 ++++++++ main.py => cromulant/main.py | 12 ++++----- ruff.toml => cromulant/ruff.toml | 0 schema.sql => cromulant/schema.sql | 0 utils.py => cromulant/utils.py | 0 window.py => cromulant/window.py | 38 +++++++++++++++++++++------- run.sh | 7 ++++- 12 files changed, 76 insertions(+), 25 deletions(-) delete mode 100644 config.py rename ants.py => cromulant/ants.py (96%) create mode 100644 cromulant/config.py rename database.py => cromulant/database.py (74%) create mode 100644 cromulant/game.py rename main.py => cromulant/main.py (62%) rename ruff.toml => cromulant/ruff.toml (100%) rename schema.sql => cromulant/schema.sql (100%) rename utils.py => cromulant/utils.py (100%) rename window.py => cromulant/window.py (59%) diff --git a/.gitignore b/.gitignore index 05f1c24..60bb0ee 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,4 @@ venv/* *.pyc __pycache__/ .mypy_cache/ -ants.db \ No newline at end of file +cromulant.db \ No newline at end of file diff --git a/config.py b/config.py deleted file mode 100644 index 07e5312..0000000 --- a/config.py +++ /dev/null @@ -1,4 +0,0 @@ -class Config: - title = "Cromulant" - width = 800 - height = 600 diff --git a/ants.py b/cromulant/ants.py similarity index 96% rename from ants.py rename to cromulant/ants.py index 8e7fcd1..88c78f2 100644 --- a/ants.py +++ b/cromulant/ants.py @@ -1,8 +1,8 @@ from __future__ import annotations from typing import ClassVar -from utils import Utils -from database import Database +from .utils import Utils +from .database import Database class Ant: diff --git a/cromulant/config.py b/cromulant/config.py new file mode 100644 index 0000000..fbb5f8f --- /dev/null +++ b/cromulant/config.py @@ -0,0 +1,17 @@ +from pathlib import Path + + +class Config: + title = "Cromulant" + width = 800 + height = 600 + here: str + database_path: Path + schema_path: Path + + + @staticmethod + def prepare() -> None: + Config.here = Path(__file__).parent + Config.database_path = Config.here / "cromulant.db" + Config.schema_path = Config.here / "schema.sql" \ No newline at end of file diff --git a/database.py b/cromulant/database.py similarity index 74% rename from database.py rename to cromulant/database.py index 7cf689f..f7ecb08 100644 --- a/database.py +++ b/cromulant/database.py @@ -3,6 +3,8 @@ from __future__ import annotations import sqlite3 from pathlib import Path +from .config import Config + class Database: connection: sqlite3.Connection @@ -10,12 +12,12 @@ class Database: @staticmethod def prepare() -> None: - Database.connection = sqlite3.connect("ants.db") + Database.connection = sqlite3.connect(Config.database_path) Database.cursor = Database.connection.cursor() @staticmethod def create() -> None: - with Path("schema.sql").open("r") as file: + with Config.schema_path.open("r") as file: schema = file.read() Database.cursor.executescript(schema) diff --git a/cromulant/game.py b/cromulant/game.py new file mode 100644 index 0000000..7b06bd2 --- /dev/null +++ b/cromulant/game.py @@ -0,0 +1,11 @@ +from .window import Window + +class Game: + @staticmethod + def update_view() -> None: + scene = Window.view.scene() + scene.addRect(0, 0, 10, 10) + scene.addRect(10, 10, 10, 10) + scene.addRect(20, 20, 10, 10) + scene.addRect(30, 30, 10, 10) + scene.addRect(40, 40, 10, 10) \ No newline at end of file diff --git a/main.py b/cromulant/main.py similarity index 62% rename from main.py rename to cromulant/main.py index 5fd10bd..3eae4ee 100644 --- a/main.py +++ b/cromulant/main.py @@ -1,22 +1,22 @@ from __future__ import annotations -from database import Database -from window import Window -from ants import Ants +from .config import Config +from .database import Database +from .window import Window +from .ants import Ants def main() -> None: + Config.prepare() Database.prepare() Database.create() Ants.get_all() - for ant in Ants.ants: - ant.describe() - Window.make() Window.add_buttons() Window.add_view() + Window.add_log() Window.start() diff --git a/ruff.toml b/cromulant/ruff.toml similarity index 100% rename from ruff.toml rename to cromulant/ruff.toml diff --git a/schema.sql b/cromulant/schema.sql similarity index 100% rename from schema.sql rename to cromulant/schema.sql diff --git a/utils.py b/cromulant/utils.py similarity index 100% rename from utils.py rename to cromulant/utils.py diff --git a/window.py b/cromulant/window.py similarity index 59% rename from window.py rename to cromulant/window.py index 363dd87..8712735 100644 --- a/window.py +++ b/cromulant/window.py @@ -8,16 +8,17 @@ from PySide6.QtWidgets import QGraphicsScene from PySide6.QtWidgets import QVBoxLayout from PySide6.QtWidgets import QPushButton from PySide6.QtWidgets import QHBoxLayout +from PySide6.QtWidgets import QTextEdit -from config import Config -from ants import Ants +from .config import Config class Window: app: QApplication window: QMainWindow - root: QWidget + root: QHBoxLayout view: QGraphicsView + log: QTextEdit @staticmethod def make() -> None: @@ -25,37 +26,56 @@ class Window: Window.window = QMainWindow() Window.window.setWindowTitle(Config.title) Window.window.resize(Config.width, Config.height) - Window.root = QWidget() - Window.window.setCentralWidget(Window.root) + Window.root = QHBoxLayout() + central_widget = QWidget() + Window.root = QVBoxLayout() + central_widget.setLayout(Window.root) + Window.window.setCentralWidget(central_widget) @staticmethod def add_buttons() -> None: - btn_hatch = QPushButton("Hatch Ant") + btn_hatch = QPushButton("Hatch") btn_terminate = QPushButton("Terminate") + btn_update = QPushButton("Update") btn_hatch.clicked.connect(Window.hatch) btn_terminate.clicked.connect(Window.terminate) + btn_update.clicked.connect(Window.update_view) layout = QHBoxLayout() layout.addWidget(btn_hatch) layout.addWidget(btn_terminate) + layout.addWidget(btn_update) - Window.root.setLayout(layout) + Window.root.addLayout(layout) @staticmethod def add_view() -> None: Window.view = QGraphicsView() scene = QGraphicsScene() Window.view.setScene(scene) - layout = QVBoxLayout(Window.root) - layout.addWidget(Window.view) + Window.root.addWidget(Window.view) + + @staticmethod + def add_log() -> None: + Window.log = QTextEdit() + Window.log.setReadOnly(True) + Window.log.setFixedHeight(100) + Window.root.addWidget(Window.log) + + @staticmethod + def update_view() -> None: + from .game import Game + Game.update_view() @staticmethod def hatch() -> None: + from .ants import Ants Ants.hatch() @staticmethod def terminate() -> None: + from .ants import Ants Ants.terminate() @staticmethod diff --git a/run.sh b/run.sh index 13b5227..e55dcd9 100755 --- a/run.sh +++ b/run.sh @@ -1 +1,6 @@ -venv/bin/python main.py \ No newline at end of file +#!/usr/bin/env bash + +root="$(dirname "$(readlink -f "$0")")" +cd "$root" + +venv/bin/python -m cromulant.main "$@" \ No newline at end of file