This commit is contained in:
Auric Vente 2024-07-19 00:54:22 -06:00
parent 95bc45af8b
commit 2b965b3435
4 changed files with 72 additions and 9 deletions

View File

@ -86,6 +86,7 @@ class Ants:
break break
Ants.save() Ants.save()
Game.update_info()
@staticmethod @staticmethod
def hatch_burst() -> None: def hatch_burst() -> None:
@ -104,13 +105,17 @@ class Ants:
image_path = Config.terminated_image_path image_path = Config.terminated_image_path
Game.add_message("Terminated", f"{ant.name} is gone", image_path) Game.add_message("Terminated", f"{ant.name} is gone", image_path)
Game.update_info()
@staticmethod @staticmethod
def terminate_all() -> None: def terminate_all() -> None:
from .game import Game
def action() -> None: def action() -> None:
Ants.ants = [] Ants.ants = []
Ants.save() Ants.save()
Window.clear_view() Window.clear_view()
Game.update_info()
Window.confirm("Terminate all ants?", action) Window.confirm("Terminate all ants?", action)
@ -157,3 +162,17 @@ class Ants:
ant = Ant() ant = Ant()
ant.from_dict(obj) ant.from_dict(obj)
Ants.ants.append(ant) Ants.ants.append(ant)
@staticmethod
def most_hits() -> Ant | None:
if not len(Ants.ants):
return None
return max(Ants.ants, key=lambda a: a.hits)
@staticmethod
def most_triumph() -> Ant | None:
if not len(Ants.ants):
return None
return max(Ants.ants, key=lambda a: a.triumph)

View File

@ -20,7 +20,7 @@ class Config:
background_color: str = "#2c2c2c" background_color: str = "#2c2c2c"
text_color: str = "#ffffff" text_color: str = "#ffffff"
image_size: int = 80 image_size: int = 80
space_1: int = 25 space_1: int = 20
max_messages: int = 120 max_messages: int = 120
loop_delay_fast: int = 3_000 loop_delay_fast: int = 3_000
loop_delay_normal: int = 20_000 loop_delay_normal: int = 20_000

View File

@ -26,6 +26,7 @@ class Game:
@staticmethod @staticmethod
def prepare() -> None: def prepare() -> None:
Game.initial_fill() Game.initial_fill()
Game.update_info()
@staticmethod @staticmethod
def add_status(ant: Ant) -> None: def add_status(ant: Ant) -> None:
@ -145,6 +146,7 @@ class Game:
ants = sorted(Ants.ants, key=lambda ant: ant.updated) ants = sorted(Ants.ants, key=lambda ant: ant.updated)
for ant in ants: for ant in ants:
if ant.status:
Game.add_status(ant) Game.add_status(ant)
@staticmethod @staticmethod
@ -166,3 +168,28 @@ class Game:
def update_speed() -> None: def update_speed() -> None:
Game.timer.stop() Game.timer.stop()
Game.start_loop() Game.start_loop()
@staticmethod
def update_info() -> None:
text = []
# Non-breaking space
nb = "\u00a0"
if not len(Ants.ants):
text.append("Hatch some ants")
else:
text.append(f"Ants:{nb}{len(Ants.ants)}")
hits = Ants.most_hits()
if hits:
text.append(f"Most{nb}Hits:{nb}{hits.name}")
triumph = Ants.most_triumph()
if triumph:
text.append(f"Most{nb}Triumph:{nb}{triumph.name}")
Window.info.setText(" | ".join(text))
Window.info.adjustSize()

View File

@ -15,6 +15,7 @@ from PySide6.QtWidgets import QComboBox
from PySide6.QtWidgets import QLayout from PySide6.QtWidgets import QLayout
from PySide6.QtWidgets import QSizePolicy from PySide6.QtWidgets import QSizePolicy
from PySide6.QtWidgets import QMessageBox from PySide6.QtWidgets import QMessageBox
from PySide6.QtWidgets import QLabel
from PySide6.QtGui import QMouseEvent # type: ignore from PySide6.QtGui import QMouseEvent # type: ignore
from PySide6.QtGui import QIcon from PySide6.QtGui import QIcon
from PySide6.QtCore import Qt # type: ignore from PySide6.QtCore import Qt # type: ignore
@ -40,12 +41,15 @@ class Window:
view: QVBoxLayout view: QVBoxLayout
view_scene: QGraphicsScene view_scene: QGraphicsScene
speed: QComboBox speed: QComboBox
scroll_area: QScrollArea
info: QLabel
@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.add_footer()
@staticmethod @staticmethod
def make() -> None: def make() -> None:
@ -83,21 +87,21 @@ class Window:
Window.speed.setCurrentIndex(1) Window.speed.setCurrentIndex(1)
Window.speed.currentIndexChanged.connect(Game.update_speed) Window.speed.currentIndexChanged.connect(Game.update_speed)
btn_close = QPushButton("Close") btn_top = QPushButton("Top")
btn_close.clicked.connect(Window.close) btn_top.clicked.connect(Window.to_top)
layout = QHBoxLayout() layout = QHBoxLayout()
layout.addWidget(btn_hatch) layout.addWidget(btn_hatch)
layout.addWidget(btn_terminate) layout.addWidget(btn_terminate)
layout.addWidget(Window.speed) layout.addWidget(Window.speed)
layout.addWidget(btn_close) layout.addWidget(btn_top)
Window.root.addLayout(layout) Window.root.addLayout(layout)
@staticmethod @staticmethod
def add_view() -> None: def add_view() -> None:
scroll_area = QScrollArea() Window.scroll_area = QScrollArea()
scroll_area.setWidgetResizable(True) Window.scroll_area.setWidgetResizable(True)
container = QWidget() container = QWidget()
parent = QVBoxLayout(container) parent = QVBoxLayout(container)
@ -105,8 +109,8 @@ class Window:
parent.addLayout(Window.view) parent.addLayout(Window.view)
Window.view.setAlignment(Qt.AlignmentFlag.AlignTop) Window.view.setAlignment(Qt.AlignmentFlag.AlignTop)
scroll_area.setWidget(container) Window.scroll_area.setWidget(container)
Window.root.addWidget(scroll_area) Window.root.addWidget(Window.scroll_area)
@staticmethod @staticmethod
def start() -> None: def start() -> None:
@ -155,3 +159,16 @@ class Window:
item.widget().deleteLater() item.widget().deleteLater()
elif item.layout(): elif item.layout():
Window.delete_layout(item.layout()) Window.delete_layout(item.layout())
@staticmethod
def to_top() -> None:
Window.scroll_area.verticalScrollBar().setValue(0)
@staticmethod
def add_footer() -> None:
layout = QHBoxLayout()
Window.info = QLabel("---")
Window.info.setWordWrap(True)
Window.expand(Window.info)
layout.addWidget(Window.info)
Window.root.addLayout(layout)