This commit is contained in:
Auric Vente 2024-07-18 02:14:03 -06:00
parent a14e36e932
commit 685f5bc686
12 changed files with 76 additions and 25 deletions

2
.gitignore vendored
View File

@ -3,4 +3,4 @@ venv/*
*.pyc *.pyc
__pycache__/ __pycache__/
.mypy_cache/ .mypy_cache/
ants.db cromulant.db

View File

@ -1,4 +0,0 @@
class Config:
title = "Cromulant"
width = 800
height = 600

View File

@ -1,8 +1,8 @@
from __future__ import annotations from __future__ import annotations
from typing import ClassVar from typing import ClassVar
from utils import Utils from .utils import Utils
from database import Database from .database import Database
class Ant: class Ant:

17
cromulant/config.py Normal file
View File

@ -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"

View File

@ -3,6 +3,8 @@ from __future__ import annotations
import sqlite3 import sqlite3
from pathlib import Path from pathlib import Path
from .config import Config
class Database: class Database:
connection: sqlite3.Connection connection: sqlite3.Connection
@ -10,12 +12,12 @@ class Database:
@staticmethod @staticmethod
def prepare() -> None: def prepare() -> None:
Database.connection = sqlite3.connect("ants.db") Database.connection = sqlite3.connect(Config.database_path)
Database.cursor = Database.connection.cursor() Database.cursor = Database.connection.cursor()
@staticmethod @staticmethod
def create() -> None: def create() -> None:
with Path("schema.sql").open("r") as file: with Config.schema_path.open("r") as file:
schema = file.read() schema = file.read()
Database.cursor.executescript(schema) Database.cursor.executescript(schema)

11
cromulant/game.py Normal file
View File

@ -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)

View File

@ -1,22 +1,22 @@
from __future__ import annotations from __future__ import annotations
from database import Database from .config import Config
from window import Window from .database import Database
from ants import Ants from .window import Window
from .ants import Ants
def main() -> None: def main() -> None:
Config.prepare()
Database.prepare() Database.prepare()
Database.create() Database.create()
Ants.get_all() Ants.get_all()
for ant in Ants.ants:
ant.describe()
Window.make() Window.make()
Window.add_buttons() Window.add_buttons()
Window.add_view() Window.add_view()
Window.add_log()
Window.start() Window.start()

View File

@ -8,16 +8,17 @@ from PySide6.QtWidgets import QGraphicsScene
from PySide6.QtWidgets import QVBoxLayout from PySide6.QtWidgets import QVBoxLayout
from PySide6.QtWidgets import QPushButton from PySide6.QtWidgets import QPushButton
from PySide6.QtWidgets import QHBoxLayout from PySide6.QtWidgets import QHBoxLayout
from PySide6.QtWidgets import QTextEdit
from config import Config from .config import Config
from ants import Ants
class Window: class Window:
app: QApplication app: QApplication
window: QMainWindow window: QMainWindow
root: QWidget root: QHBoxLayout
view: QGraphicsView view: QGraphicsView
log: QTextEdit
@staticmethod @staticmethod
def make() -> None: def make() -> None:
@ -25,37 +26,56 @@ class Window:
Window.window = QMainWindow() Window.window = QMainWindow()
Window.window.setWindowTitle(Config.title) Window.window.setWindowTitle(Config.title)
Window.window.resize(Config.width, Config.height) Window.window.resize(Config.width, Config.height)
Window.root = QWidget() Window.root = QHBoxLayout()
Window.window.setCentralWidget(Window.root) central_widget = QWidget()
Window.root = QVBoxLayout()
central_widget.setLayout(Window.root)
Window.window.setCentralWidget(central_widget)
@staticmethod @staticmethod
def add_buttons() -> None: def add_buttons() -> None:
btn_hatch = QPushButton("Hatch Ant") btn_hatch = QPushButton("Hatch")
btn_terminate = QPushButton("Terminate") btn_terminate = QPushButton("Terminate")
btn_update = QPushButton("Update")
btn_hatch.clicked.connect(Window.hatch) btn_hatch.clicked.connect(Window.hatch)
btn_terminate.clicked.connect(Window.terminate) btn_terminate.clicked.connect(Window.terminate)
btn_update.clicked.connect(Window.update_view)
layout = QHBoxLayout() layout = QHBoxLayout()
layout.addWidget(btn_hatch) layout.addWidget(btn_hatch)
layout.addWidget(btn_terminate) layout.addWidget(btn_terminate)
layout.addWidget(btn_update)
Window.root.setLayout(layout) Window.root.addLayout(layout)
@staticmethod @staticmethod
def add_view() -> None: def add_view() -> None:
Window.view = QGraphicsView() Window.view = QGraphicsView()
scene = QGraphicsScene() scene = QGraphicsScene()
Window.view.setScene(scene) Window.view.setScene(scene)
layout = QVBoxLayout(Window.root) Window.root.addWidget(Window.view)
layout.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 @staticmethod
def hatch() -> None: def hatch() -> None:
from .ants import Ants
Ants.hatch() Ants.hatch()
@staticmethod @staticmethod
def terminate() -> None: def terminate() -> None:
from .ants import Ants
Ants.terminate() Ants.terminate()
@staticmethod @staticmethod

7
run.sh
View File

@ -1 +1,6 @@
venv/bin/python main.py #!/usr/bin/env bash
root="$(dirname "$(readlink -f "$0")")"
cd "$root"
venv/bin/python -m cromulant.main "$@"