feat: add browser support and Docker deployment
This commit is contained in:
+6
-2
@@ -8,6 +8,8 @@ Controls:
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
|
||||
@@ -89,10 +91,11 @@ class Game:
|
||||
self.monsters = MonsterManager(monster_sides, self.map)
|
||||
self.monsters.spawn_initial(self.get_monster_spawn_blockers())
|
||||
|
||||
def run(self, max_frames: Optional[int] = None) -> None:
|
||||
async def run(self, max_frames: Optional[int] = None) -> None:
|
||||
frame_count = 0
|
||||
while self.running and (max_frames is None or frame_count < max_frames):
|
||||
dt = min(self.clock.tick(FPS) / 1000.0, 0.05)
|
||||
# The browser schedules frames; only throttle the desktop loop.
|
||||
dt = min(self.clock.tick(0 if sys.platform == "emscripten" else FPS) / 1000.0, 0.05)
|
||||
|
||||
if self.state == "startscreen":
|
||||
next_state = self.startscreen.run()
|
||||
@@ -119,6 +122,7 @@ class Game:
|
||||
|
||||
pygame.display.flip()
|
||||
frame_count += 1
|
||||
await asyncio.sleep(0)
|
||||
|
||||
pygame.quit()
|
||||
|
||||
|
||||
+26
-9
@@ -1,4 +1,9 @@
|
||||
import json
|
||||
import sys
|
||||
|
||||
if sys.platform == "emscripten":
|
||||
from platform import window
|
||||
|
||||
import pygame
|
||||
from src.settings import HIGHSCORE_COLOR_FONT, HIGHSCORE_COLOR_TABLE
|
||||
|
||||
@@ -10,20 +15,26 @@ class Highscore:
|
||||
self.font_title = pygame.font.SysFont("Arial", 28, bold=True)
|
||||
self.font_score = pygame.font.SysFont("Arial", 18, bold=True)
|
||||
|
||||
# check if file already exists, otherwise create it
|
||||
try:
|
||||
with open(self.path, "r") as f:
|
||||
pass
|
||||
except FileNotFoundError:
|
||||
self.save_scores([])
|
||||
|
||||
def load_scores(self):
|
||||
"""
|
||||
Will load the highscore json file.
|
||||
:return: All scores form json file.
|
||||
"""
|
||||
with open(self.path, "r") as f:
|
||||
scores = json.load(f)
|
||||
if sys.platform == "emscripten":
|
||||
try:
|
||||
stored = window.localStorage.getItem(f"silent-fh:{self.path}")
|
||||
if stored:
|
||||
scores = json.loads(stored)
|
||||
return scores if isinstance(scores, list) else []
|
||||
except Exception as exc:
|
||||
# Storage may be blocked by browser privacy settings.
|
||||
print(f"Could not load browser highscores: {exc}")
|
||||
|
||||
try:
|
||||
with open(self.path, "r") as f:
|
||||
scores = json.load(f)
|
||||
except (FileNotFoundError, json.JSONDecodeError):
|
||||
return []
|
||||
|
||||
return scores if isinstance(scores, list) else []
|
||||
|
||||
@@ -37,6 +48,12 @@ class Highscore:
|
||||
with open(self.path, "w") as file:
|
||||
json.dump(scores, file, indent=4)
|
||||
|
||||
if sys.platform == "emscripten":
|
||||
try:
|
||||
window.localStorage.setItem(f"silent-fh:{self.path}", json.dumps(scores))
|
||||
except Exception as exc:
|
||||
print(f"Could not persist browser highscores: {exc}")
|
||||
|
||||
def add_score(self, player_name, score_ects, time_survived, won):
|
||||
"""
|
||||
Add a new score to the list of existing scores from highscore file. The
|
||||
|
||||
Reference in New Issue
Block a user