--- a/src/constants.py +++ b/src/constants.py @@ -5,7 +5,7 @@ HEIGHT = 120 MID_W = WIDTH / 2 MID_H = HEIGHT / 2 -SCORE_FILE = "lastspacefighter_scores.dat" +SCORE_FILE = "lastspacefighter_scores.json" # colors --- a/src/shooter.py +++ b/src/shooter.py @@ -14,9 +14,9 @@ class App: def __init__(self): - px.init(WIDTH, HEIGHT, title="The last space fighter", capture_sec=0) + px.init(WIDTH, HEIGHT, title="The last space fighter", capture_sec=0, + quit_key=px.KEY_NONE) px.load(path_join("assets", "shooter.pyxres")) - px.fullscreen(True) self.pt = time() # Buffer previous time self.game = Game() @@ -27,8 +27,6 @@ def update(self): - if px.btnp(px.KEY_Q): - px.quit() if px.btnp(px.KEY_P): if not self.paused: px.stop() --- a/src/scores.py +++ b/src/scores.py @@ -1,7 +1,7 @@ -from os.path import join as path_join +from os.path import isdir, join as path_join from os import environ, makedirs -import shelve +import json import pyxel as px from constants import WHITE, BROWN, PURPLE, RED, PINK, SCORE_FILE @@ -22,24 +22,28 @@ @classmethod def init(cls): - config_path = path_join( - environ.get("APPDATA") # Windows - or environ.get("XDG_CONFIG_HOME") # Linux/macos XDG spec. - or path_join(environ.get("HOME"), ".config"), # fallback - "lastspacefighter", - ) + # FredyGames: the browser build keeps the scores in /saves/the-last-space-fighter (IndexedDB, kept by the page) + config_path = "/saves/the-last-space-fighter" + if not isdir(config_path): + config_path = path_join( + environ.get("APPDATA") # Windows + or environ.get("XDG_CONFIG_HOME") # Linux/macos XDG spec. + or path_join(environ.get("HOME"), ".config"), # fallback + "lastspacefighter", + ) makedirs(config_path, exist_ok=True) cls.scores_file = path_join(config_path, SCORE_FILE) cls.hiscores = cls.load() @classmethod def load(cls): - with shelve.open(cls.scores_file) as s: - try: - return s["hiscores"] - except KeyError: - # Default high-scores - return [1000, 2000, 3000, 4000, 5000] + # FredyGames: a JSON file instead of shelve (Pyodide has no dbm module) + try: + with open(cls.scores_file, encoding="utf-8") as f: + return [int(s) for s in json.load(f)["hiscores"]] + except (OSError, ValueError, KeyError, TypeError): + # Default high-scores + return [1000, 2000, 3000, 4000, 5000] @classmethod def pr(cls): @@ -49,8 +53,8 @@ @classmethod def save(cls): - with shelve.open(cls.scores_file) as s: - s["hiscores"] = cls.hiscores + with open(cls.scores_file, "w", encoding="utf-8") as f: + json.dump({"hiscores": cls.hiscores}, f) @classmethod def update(cls, score):