This commit is contained in:
Auric Vente 2024-07-21 05:50:25 -06:00
parent 34896e24e2
commit 3d8ff1f167
4 changed files with 67 additions and 34 deletions

View File

@ -70,16 +70,11 @@ class Ants:
@staticmethod @staticmethod
def prepare() -> None: def prepare() -> None:
Ants.get() Ants.get()
Ants.fill()
@staticmethod @staticmethod
def hatch(num: int = 1) -> None: def hatch(num: int = 1) -> None:
from .game import Game from .game import Game
if len(Ants.ants) >= Config.max_ants:
Window.alert("Max ants reached\nTerminate some to hatch new ones")
return
now = Utils.now() now = Utils.now()
for _ in range(num): for _ in range(num):
@ -91,9 +86,6 @@ class Ants:
Ants.ants.append(ant) Ants.ants.append(ant)
Ants.announce_hatch(ant) Ants.announce_hatch(ant)
if len(Ants.ants) >= Config.max_ants:
break
Ants.save() Ants.save()
Game.update_info() Game.update_info()
@ -179,28 +171,16 @@ class Ants:
@staticmethod @staticmethod
def get() -> None: def get() -> None:
objs = Storage.get_ants() objs = Storage.get_ants()
changed = False
if len(objs) > Config.max_ants:
objs = objs[: Config.max_ants]
changed = True
for obj in objs: for obj in objs:
ant = Ant() ant = Ant()
ant.from_dict(obj) ant.from_dict(obj)
Ants.ants.append(ant) Ants.ants.append(ant)
if changed:
Ants.save()
@staticmethod @staticmethod
def fill() -> None: def populate(num: int) -> None:
diff = Config.max_ants - len(Ants.ants) Ants.clear()
Ants.hatch(num)
if diff <= 0:
return
Ants.hatch(diff)
@staticmethod @staticmethod
def random_name() -> str: def random_name() -> str:

View File

@ -10,7 +10,6 @@ class Config:
program: str = "cromulant" program: str = "cromulant"
width: int = 820 width: int = 820
height: int = 900 height: int = 900
max_ants: int = 100
here: Path here: Path
ants_json: Path ants_json: Path
icon_path: Path icon_path: Path

View File

@ -273,10 +273,19 @@ class Game:
@staticmethod @staticmethod
def restart() -> None: def restart() -> None:
def action() -> None: opts = ["25", "50", "100", "200", "300", "400", "500"]
Ants.clear() opts = [f"{opt} ants" for opt in opts]
size = Window.prompt_combobox("Size of the population", opts, 2)
if not size:
return
num = int(size.split(" ")[0])
Window.clear_view() Window.clear_view()
Ants.fill() Ants.populate(num)
Game.start_loop() Game.start_loop()
Window.confirm("Restart the ants?", action) @staticmethod
def update_size() -> None:
pass

View File

@ -5,10 +5,12 @@ from collections.abc import Callable
import signal import signal
from PySide6.QtWidgets import QApplication # type: ignore from PySide6.QtWidgets import QApplication # type: ignore
from PySide6.QtWidgets import QDialog
from PySide6.QtWidgets import QMainWindow from PySide6.QtWidgets import QMainWindow
from PySide6.QtWidgets import QWidget from PySide6.QtWidgets import QWidget
from PySide6.QtWidgets import QGraphicsScene from PySide6.QtWidgets import QGraphicsScene
from PySide6.QtWidgets import QVBoxLayout from PySide6.QtWidgets import QVBoxLayout
from PySide6.QtWidgets import QLabel
from PySide6.QtWidgets import QPushButton from PySide6.QtWidgets import QPushButton
from PySide6.QtWidgets import QHBoxLayout from PySide6.QtWidgets import QHBoxLayout
from PySide6.QtWidgets import QScrollArea from PySide6.QtWidgets import QScrollArea
@ -37,6 +39,39 @@ class FilterLineEdit(QLineEdit): # type: ignore
super().keyPressEvent(e) super().keyPressEvent(e)
class ComboBoxDialog(QDialog): # type: ignore
def __init__(self, message: str, options: list[str], defindex: int) -> None:
super().__init__()
self.setWindowTitle("Select Option")
self.setFixedSize(300, 150) # Set a fixed size for the dialog
self.layout = QVBoxLayout()
self.label = QLabel(message)
self.layout.addWidget(self.label)
self.combo_box = QComboBox()
self.combo_box.addItems(options)
self.combo_box.setCurrentIndex(defindex)
self.layout.addWidget(self.combo_box)
self.button_layout = QHBoxLayout()
self.ok_button = QPushButton("OK")
self.ok_button.clicked.connect(self.accept)
self.button_layout.addWidget(self.ok_button)
self.cancel_button = QPushButton("Cancel")
self.cancel_button.clicked.connect(self.reject)
self.button_layout.addWidget(self.cancel_button)
self.layout.addLayout(self.button_layout)
self.setLayout(self.layout)
def get_selection(self) -> str:
return str(self.combo_box.currentText())
class Window: class Window:
app: QApplication app: QApplication
window: QMainWindow window: QMainWindow
@ -107,23 +142,23 @@ class Window:
color: {Config.alt_hover_text_color}; color: {Config.alt_hover_text_color};
}} }}
QMessageBox {{ QDialog {{
background-color: {Config.alt_background_color}; background-color: {Config.alt_background_color};
color: {Config.alt_text_color}; color: {Config.alt_text_color};
border: 1px solid {Config.alt_border_color}; border: 1px solid {Config.alt_border_color};
}} }}
QMessageBox QLabel {{ QDialog QLabel {{
background-color: {Config.alt_background_color}; background-color: {Config.alt_background_color};
color: {Config.alt_text_color}; color: {Config.alt_text_color};
}} }}
QMessageBox QPushButton {{ QDialog QPushButton {{
background-color: {Config.alt_background_color}; background-color: {Config.alt_background_color};
color: {Config.alt_text_color}; color: {Config.alt_text_color};
}} }}
QMessageBox QPushButton:hover {{ QDialog QPushButton:hover {{
background-color: {Config.message_box_button_hover_background_color}; background-color: {Config.message_box_button_hover_background_color};
color: {Config.message_box_button_hover_text_color}; color: {Config.message_box_button_hover_text_color};
}} }}
@ -179,6 +214,7 @@ class Window:
root = QWidget() root = QWidget()
container = QHBoxLayout() container = QHBoxLayout()
btn_restart = QPushButton("Restart") btn_restart = QPushButton("Restart")
btn_restart.setToolTip("Restart with a new set of ants") btn_restart.setToolTip("Restart with a new set of ants")
btn_restart.clicked.connect(Game.restart) btn_restart.clicked.connect(Game.restart)
@ -329,3 +365,12 @@ class Window:
msg_box.setWindowTitle("Information") msg_box.setWindowTitle("Information")
msg_box.setStandardButtons(QMessageBox.Ok) msg_box.setStandardButtons(QMessageBox.Ok)
msg_box.exec() msg_box.exec()
@staticmethod
def prompt_combobox(message: str, options: list[str], defindex: int = 0) -> str:
dialog = ComboBoxDialog(message, options, defindex)
if dialog.exec() == QDialog.Accepted:
return dialog.get_selection()
return ""