This commit is contained in:
Auric Vente 2024-07-20 04:48:04 -06:00
parent 6450af91be
commit 896b39eb4a
5 changed files with 59 additions and 5 deletions

View File

@ -51,6 +51,7 @@ class Config:
input_text_color: str = "rgb(18, 18, 18)" input_text_color: str = "rgb(18, 18, 18)"
input_border_color: str = "rgb(140, 140, 140)" input_border_color: str = "rgb(140, 140, 140)"
input_caret_color: str = "rgb(18, 18, 18)" input_caret_color: str = "rgb(18, 18, 18)"
settings_json: Path
@staticmethod @staticmethod
def prepare() -> None: def prepare() -> None:
@ -61,6 +62,14 @@ class Config:
Config.ants_json.parent.mkdir(parents=True, exist_ok=True) Config.ants_json.parent.mkdir(parents=True, exist_ok=True)
Config.ants_json.write_text("[]") Config.ants_json.write_text("[]")
Config.settings_json = (
Path(appdirs.user_config_dir()) / "cromulant" / "settings.json"
)
if not Config.settings_json.exists():
Config.settings_json.parent.mkdir(parents=True, exist_ok=True)
Config.settings_json.write_text("{}")
Config.icon_path = Config.here / "img" / "icon_4.jpg" Config.icon_path = Config.here / "img" / "icon_4.jpg"
Config.status_image_path = Config.here / "img" / "icon_5.jpg" Config.status_image_path = Config.here / "img" / "icon_5.jpg"
Config.hatched_image_path = Config.here / "img" / "icon_7.jpg" Config.hatched_image_path = Config.here / "img" / "icon_7.jpg"

View File

@ -18,6 +18,7 @@ from .utils import Utils
from .ants import Ant from .ants import Ant
from .ants import Ants from .ants import Ants
from .window import Window from .window import Window
from .settings import Settings
class Game: class Game:
@ -204,14 +205,14 @@ class Game:
@staticmethod @staticmethod
def start_loop() -> None: def start_loop() -> None:
speed = Window.speed.currentText() speed = Settings.speed
if speed == "Fast": if speed == "fast":
delay = Config.loop_delay_fast delay = Config.loop_delay_fast
elif speed == "Normal": elif speed == "slow":
delay = Config.loop_delay_normal
else:
delay = Config.loop_delay_slow delay = Config.loop_delay_slow
else:
delay = Config.loop_delay_normal
Game.timer = QTimer() Game.timer = QTimer()
Game.timer.timeout.connect(Game.get_status) Game.timer.timeout.connect(Game.get_status)
@ -219,6 +220,12 @@ class Game:
@staticmethod @staticmethod
def update_speed() -> None: def update_speed() -> None:
speed = Window.speed.currentText().lower()
if speed == Settings.speed:
return
Settings.set_speed(speed)
Game.timer.stop() Game.timer.stop()
Game.start_loop() Game.start_loop()

View File

@ -5,6 +5,7 @@ from .utils import Utils
from .ants import Ants from .ants import Ants
from .window import Window from .window import Window
from .game import Game from .game import Game
from .settings import Settings
def main() -> None: def main() -> None:
@ -12,6 +13,7 @@ def main() -> None:
Utils.prepare() Utils.prepare()
Ants.prepare() Ants.prepare()
Window.prepare() Window.prepare()
Settings.prepare()
Game.prepare() Game.prepare()
Game.start_loop() Game.start_loop()
Window.start() Window.start()

26
cromulant/settings.py Normal file
View File

@ -0,0 +1,26 @@
from .window import Window
from .storage import Storage
class Settings:
speed: str
@staticmethod
def prepare() -> None:
settings = Storage.get_settings()
Settings.speed = settings.get("speed", "normal")
speed = Settings.speed.capitalize()
Window.speed.setCurrentText(speed)
@staticmethod
def save() -> None:
settings = {
"speed": Settings.speed,
}
Storage.save_settings(settings)
@staticmethod
def set_speed(speed: str) -> None:
Settings.speed = speed
Settings.save()

View File

@ -26,3 +26,13 @@ class Storage:
def get_names() -> Any: def get_names() -> Any:
with Config.names_json.open() as file: with Config.names_json.open() as file:
return json.load(file) return json.load(file)
@staticmethod
def get_settings() -> Any:
with Config.settings_json.open() as file:
return json.load(file)
@staticmethod
def save_settings(settings: dict[str, Any]) -> None:
with Config.settings_json.open("w") as file:
json.dump(settings, file)