This commit is contained in:
Auric Vente 2024-07-21 01:13:00 -06:00
parent 857dd9dd92
commit 1fd742ea8a
4 changed files with 39 additions and 30 deletions

View File

@ -61,7 +61,8 @@ class Ants:
@staticmethod @staticmethod
def prepare() -> None: def prepare() -> None:
Ants.get_ants() Ants.get()
Ants.fill()
@staticmethod @staticmethod
def hatch(num: int = 1) -> None: def hatch(num: int = 1) -> None:
@ -184,10 +185,11 @@ class Ants:
ant.updated = Utils.now() ant.updated = Utils.now()
Game.add_status(ant) Game.add_status(ant)
Game.update_info()
Ants.save() Ants.save()
@staticmethod @staticmethod
def get_ants() -> None: def get() -> None:
objs = Storage.get_ants() objs = Storage.get_ants()
changed = False changed = False
@ -203,6 +205,15 @@ class Ants:
if changed: if changed:
Ants.save() Ants.save()
@staticmethod
def fill() -> None:
diff = Config.max_ants - len(Ants.ants)
if diff <= 0:
return
Ants.hatch(diff)
@staticmethod @staticmethod
def random_name() -> str: def random_name() -> str:
return Utils.random_name(Ants.get_names()) return Utils.random_name(Ants.get_names())
@ -315,3 +326,7 @@ 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)
@staticmethod
def clear() -> None:
Ants.ants = []

View File

@ -20,7 +20,7 @@ from .settings import Settings
class Game: class Game:
timer: QTimer timer: QTimer | None = None
playing_song: bool = False playing_song: bool = False
@staticmethod @staticmethod
@ -166,7 +166,7 @@ class Game:
if not ant: if not ant:
return return
num = random.randint(1, 10) num = random.randint(1, 12)
status = "" status = ""
method = "normal" method = "normal"
@ -191,6 +191,8 @@ class Game:
elif num == 8: elif num == 8:
status = Utils.random_country([]) status = Utils.random_country([])
method = "travel" method = "travel"
elif num == 9:
Ants.merge()
else: else:
status = Utils.rand_sentence.sentence() status = Utils.rand_sentence.sentence()
@ -209,6 +211,9 @@ class Game:
@staticmethod @staticmethod
def start_loop() -> None: def start_loop() -> None:
if Game.timer:
Game.timer.stop()
speed = Settings.speed speed = Settings.speed
if speed == "fast": if speed == "fast":
@ -230,7 +235,6 @@ class Game:
return return
Settings.set_speed(speed) Settings.set_speed(speed)
Game.timer.stop()
Game.start_loop() Game.start_loop()
@staticmethod @staticmethod
@ -252,7 +256,6 @@ class Game:
text.append(f"Top:{nb}{ant.name} ({score})") text.append(f"Top:{nb}{ant.name} ({score})")
Window.info.setText(Config.info_separator.join(text)) Window.info.setText(Config.info_separator.join(text))
Window.info.adjustSize()
@staticmethod @staticmethod
def toggle_song() -> None: def toggle_song() -> None:
@ -267,3 +270,13 @@ class Game:
Window.play_audio(path, on_stop) Window.play_audio(path, on_stop)
Game.playing_song = True Game.playing_song = True
@staticmethod
def restart() -> None:
def action() -> None:
Ants.clear()
Window.clear_view()
Ants.fill()
Game.start_loop()
Window.confirm("Restart the ants?", action)

View File

@ -11,8 +11,8 @@ from .settings import Settings
def main() -> None: def main() -> None:
Config.prepare() Config.prepare()
Utils.prepare() Utils.prepare()
Ants.prepare()
Window.prepare() Window.prepare()
Ants.prepare()
Settings.prepare() Settings.prepare()
Game.prepare() Game.prepare()
Game.start_loop() Game.start_loop()

View File

@ -186,28 +186,13 @@ class Window:
@staticmethod @staticmethod
def add_buttons() -> None: def add_buttons() -> None:
from .ants import Ants
from .game import Game from .game import Game
from .filter import Filter from .filter import Filter
root = QWidget() root = QWidget()
container = QHBoxLayout() container = QHBoxLayout()
btn_restart = QPushButton("Restart")
btn_hatch = SpecialButton("Hatch") btn_restart.clicked.connect(Game.restart)
btn_hatch.setToolTip("Hatch a new ant\nMiddle Click to hatch Trio")
btn_hatch.clicked.connect(lambda e: Ants.hatch())
btn_hatch.middleClicked.connect(lambda: Ants.hatch_burst())
btn_terminate = SpecialButton("Term")
btn_merge = SpecialButton("Merge")
btn_terminate.setToolTip(
"Terminate a random ant\nMiddle Click to terminate all"
)
btn_terminate.clicked.connect(lambda e: Ants.terminate())
btn_terminate.middleClicked.connect(lambda: Ants.terminate_all())
btn_merge.clicked.connect(lambda e: Ants.merge())
Window.speed = QComboBox() Window.speed = QComboBox()
tooltip = "The speed of the updates\n" tooltip = "The speed of the updates\n"
@ -220,14 +205,12 @@ class Window:
Window.speed.currentIndexChanged.connect(Game.update_speed) Window.speed.currentIndexChanged.connect(Game.update_speed)
Window.filter = FilterLineEdit() Window.filter = FilterLineEdit()
Window.filter.setFixedWidth(120) Window.filter.setFixedWidth(150)
Window.filter.setPlaceholderText("Filter") Window.filter.setPlaceholderText("Filter")
Window.filter.mousePressEvent = lambda e: Window.to_top() Window.filter.mousePressEvent = lambda e: Window.to_top()
Window.filter.keyReleaseEvent = lambda e: Filter.filter(e) Window.filter.keyReleaseEvent = lambda e: Filter.filter(e)
container.addWidget(btn_hatch) container.addWidget(btn_restart)
container.addWidget(btn_terminate)
container.addWidget(btn_merge)
container.addWidget(Window.speed) container.addWidget(Window.speed)
container.addWidget(Window.filter) container.addWidget(Window.filter)
@ -322,9 +305,7 @@ class Window:
root.setContentsMargins(0, 0, 0, 0) root.setContentsMargins(0, 0, 0, 0)
container = QHBoxLayout() container = QHBoxLayout()
Window.info = SpecialButton("---") Window.info = SpecialButton("---")
Window.info.setToolTip("Click to scroll to the bottom or top") Window.info.setToolTip("Click to scroll to the bottom or top")
Window.info.clicked.connect(Window.toggle_scroll) Window.info.clicked.connect(Window.toggle_scroll)
Window.info.setMinimumSize(35, 35) Window.info.setMinimumSize(35, 35)
container.addWidget(Window.info) container.addWidget(Window.info)