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
def prepare() -> None:
Ants.get_ants()
Ants.get()
Ants.fill()
@staticmethod
def hatch(num: int = 1) -> None:
@ -184,10 +185,11 @@ class Ants:
ant.updated = Utils.now()
Game.add_status(ant)
Game.update_info()
Ants.save()
@staticmethod
def get_ants() -> None:
def get() -> None:
objs = Storage.get_ants()
changed = False
@ -203,6 +205,15 @@ class Ants:
if changed:
Ants.save()
@staticmethod
def fill() -> None:
diff = Config.max_ants - len(Ants.ants)
if diff <= 0:
return
Ants.hatch(diff)
@staticmethod
def random_name() -> str:
return Utils.random_name(Ants.get_names())
@ -315,3 +326,7 @@ class Ants:
image_path = Config.terminated_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:
timer: QTimer
timer: QTimer | None = None
playing_song: bool = False
@staticmethod
@ -166,7 +166,7 @@ class Game:
if not ant:
return
num = random.randint(1, 10)
num = random.randint(1, 12)
status = ""
method = "normal"
@ -191,6 +191,8 @@ class Game:
elif num == 8:
status = Utils.random_country([])
method = "travel"
elif num == 9:
Ants.merge()
else:
status = Utils.rand_sentence.sentence()
@ -209,6 +211,9 @@ class Game:
@staticmethod
def start_loop() -> None:
if Game.timer:
Game.timer.stop()
speed = Settings.speed
if speed == "fast":
@ -230,7 +235,6 @@ class Game:
return
Settings.set_speed(speed)
Game.timer.stop()
Game.start_loop()
@staticmethod
@ -252,7 +256,6 @@ class Game:
text.append(f"Top:{nb}{ant.name} ({score})")
Window.info.setText(Config.info_separator.join(text))
Window.info.adjustSize()
@staticmethod
def toggle_song() -> None:
@ -267,3 +270,13 @@ class Game:
Window.play_audio(path, on_stop)
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:
Config.prepare()
Utils.prepare()
Ants.prepare()
Window.prepare()
Ants.prepare()
Settings.prepare()
Game.prepare()
Game.start_loop()

View File

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