This commit is contained in:
Auric Vente 2024-07-18 23:19:08 -06:00
parent 6cb5fdc8ec
commit d989ac45a8
5 changed files with 85 additions and 33 deletions

View File

@ -56,12 +56,7 @@ class Ants:
@staticmethod @staticmethod
def prepare() -> None: def prepare() -> None:
objs = Storage.get_ants() Ants.get_ants()
for obj in objs:
ant = Ant()
ant.from_dict(obj)
Ants.ants.append(ant)
@staticmethod @staticmethod
def hatch() -> None: def hatch() -> None:
@ -83,7 +78,7 @@ class Ants:
Ants.save() Ants.save()
image_path = Config.hatched_image_path image_path = Config.hatched_image_path
Game.add_message(f"{ant.name} is born", image_path) Game.add_message("Hatched", f"{ant.name} is born", image_path)
@staticmethod @staticmethod
def terminate() -> None: def terminate() -> None:
@ -97,7 +92,7 @@ class Ants:
Ants.save() Ants.save()
image_path = Config.terminated_image_path image_path = Config.terminated_image_path
Game.add_message(f"{ant.name} was terminated", image_path) Game.add_message("Terminated", f"{ant.name} is gone", image_path)
@staticmethod @staticmethod
def get_random_ant() -> Ant: def get_random_ant() -> Ant:
@ -115,6 +110,7 @@ class Ants:
def get_lazy() -> Ant: def get_lazy() -> Ant:
return min(Ants.ants, key=lambda ant: ant.updated) return min(Ants.ants, key=lambda ant: ant.updated)
@staticmethod
def set_status(ant: Ant, status: str) -> None: def set_status(ant: Ant, status: str) -> None:
from .game import Game from .game import Game
@ -128,6 +124,16 @@ class Ants:
Game.add_status(ant) Game.add_status(ant)
Ants.save() Ants.save()
@staticmethod
def get_other(ant: Ant) -> Ant: def get_other(ant: Ant) -> Ant:
ants = [a for a in Ants.ants if a.name != ant.name] ants = [a for a in Ants.ants if a.name != ant.name]
return random.choice(ants) return random.choice(ants)
@staticmethod
def get_ants() -> None:
objs = Storage.get_ants()
for obj in objs:
ant = Ant()
ant.from_dict(obj)
Ants.ants.append(ant)

View File

@ -19,8 +19,10 @@ class Config:
names_json: Path names_json: Path
background_color: str = "#2c2c2c" background_color: str = "#2c2c2c"
text_color: str = "#ffffff" text_color: str = "#ffffff"
image_size: int = 50 image_size: int = 80
space_1: int = 25 space_1: int = 25
max_messages: int = 120
loop_delay: int = 2_000
@staticmethod @staticmethod
def prepare() -> None: def prepare() -> None:

View File

@ -5,8 +5,10 @@ from pathlib import Path
from PySide6.QtCore import Qt # type: ignore from PySide6.QtCore import Qt # type: ignore
from PySide6.QtWidgets import QHBoxLayout from PySide6.QtWidgets import QHBoxLayout
from PySide6.QtWidgets import QVBoxLayout
from PySide6.QtWidgets import QLabel from PySide6.QtWidgets import QLabel
from PySide6.QtGui import QPixmap # type: ignore from PySide6.QtGui import QPixmap # type: ignore
from PySide6.QtCore import QTimer
from wonderwords import RandomSentence # type: ignore from wonderwords import RandomSentence # type: ignore
@ -18,33 +20,56 @@ from .window import Window
class Game: class Game:
timer: QTimer
@staticmethod
def prepare() -> None:
Game.initial_fill()
@staticmethod @staticmethod
def add_status(ant: Ant) -> None: def add_status(ant: Ant) -> None:
container = QHBoxLayout() container = QHBoxLayout()
image_label = Game.get_image(Config.status_image_path, ant.color) image_label = Game.get_image(Config.status_image_path, ant.color)
name = QLabel(ant.name) right_container = Game.make_right_container(ant.name, ant.status)
status = QLabel(ant.status)
container.addWidget(image_label) container.addWidget(image_label)
container.addSpacing(Config.space_1) container.addSpacing(Config.space_1)
container.addWidget(name) container.addLayout(right_container)
container.addSpacing(Config.space_1) Game.add_view_container(container)
container.addWidget(status)
Window.view.insertLayout(0, container)
@staticmethod @staticmethod
def add_message(message: str, image_path: Path) -> None: def add_message(title: str, message: str, image_path: Path) -> None:
container = QHBoxLayout() container = QHBoxLayout()
image_label = Game.get_image(image_path, (255, 255, 255)) image_label = Game.get_image(image_path, (255, 255, 255))
message_label = QLabel(message) right_container = Game.make_right_container(title, message)
container.addWidget(image_label) container.addWidget(image_label)
container.addSpacing(Config.space_1) container.addSpacing(Config.space_1)
container.addWidget(message_label) container.addLayout(right_container)
Game.add_view_container(container)
@staticmethod
def add_view_container(container: QHBoxLayout) -> None:
Window.view.insertLayout(0, container) Window.view.insertLayout(0, container)
while Window.view.count() > Config.max_messages:
item = Window.view.takeAt(Window.view.count() - 1)
if item.widget():
item.widget().deleteLater()
elif item.layout():
Window.delete_layout(item.layout())
@staticmethod
def make_right_container(title: str, message: str) -> QVBoxLayout:
container = QVBoxLayout()
container.setAlignment(Qt.AlignTop)
title_label = QLabel(title)
title_label.setStyleSheet("font-weight: bold;")
message_label = QLabel(message)
container.addWidget(title_label)
container.addWidget(message_label)
return container
@staticmethod @staticmethod
def get_image(image_path: Path, border_color: tuple[int, int, int]) -> QLabel: def get_image(image_path: Path, border_color: tuple[int, int, int]) -> QLabel:
image_label = QLabel() image_label = QLabel()
@ -91,3 +116,20 @@ class Game:
status = s.sentence() status = s.sentence()
Ants.set_status(ant, status) Ants.set_status(ant, status)
@staticmethod
def initial_fill() -> None:
if not len(Ants.ants):
return
ants = sorted(Ants.ants, key=lambda ant: ant.updated)
for ant in ants:
Game.add_status(ant)
@staticmethod
def start_loop() -> None:
interval_ms = Config.loop_delay
Game.timer = QTimer()
Game.timer.timeout.connect(Game.get_status)
Game.timer.start(interval_ms)

View File

@ -4,6 +4,7 @@ from .config import Config
from .utils import Utils from .utils import Utils
from .ants import Ants from .ants import Ants
from .window import Window from .window import Window
from .game import Game
def main() -> None: def main() -> None:
@ -11,6 +12,9 @@ def main() -> None:
Utils.prepare() Utils.prepare()
Ants.prepare() Ants.prepare()
Window.prepare() Window.prepare()
Game.prepare()
Game.start_loop()
Window.start()
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -11,7 +11,6 @@ from PySide6.QtWidgets import QHBoxLayout
from PySide6.QtWidgets import QScrollArea from PySide6.QtWidgets import QScrollArea
from PySide6.QtGui import QIcon from PySide6.QtGui import QIcon
from PySide6.QtCore import Qt # type: ignore from PySide6.QtCore import Qt # type: ignore
from PySide6.QtCore import QTimer
from .config import Config from .config import Config
@ -22,15 +21,12 @@ class Window:
root: QHBoxLayout root: QHBoxLayout
view: QGraphicsView view: QGraphicsView
view_scene: QGraphicsScene view_scene: QGraphicsScene
timer: QTimer
@staticmethod @staticmethod
def prepare() -> None: def prepare() -> None:
Window.make() Window.make()
Window.add_buttons() Window.add_buttons()
Window.add_view() Window.add_view()
Window.start_timer()
Window.start()
@staticmethod @staticmethod
def make() -> None: def make() -> None:
@ -94,15 +90,6 @@ class Window:
Ants.terminate() Ants.terminate()
@staticmethod
def start_timer() -> None:
from .game import Game
interval_ms = 3_000
Window.timer = QTimer()
Window.timer.timeout.connect(Game.get_status)
Window.timer.start(interval_ms)
@staticmethod @staticmethod
def start() -> None: def start() -> None:
Window.window.show() Window.window.show()
@ -111,3 +98,14 @@ class Window:
@staticmethod @staticmethod
def close() -> None: def close() -> None:
Window.app.quit() Window.app.quit()
@staticmethod
def delete_layout(layout):
while layout.count():
item = layout.takeAt(0)
if item.widget():
item.widget().deleteLater()
elif item.layout():
Window.delete_layout(item.layout())
layout.deleteLater()