feat: save highscores

This commit is contained in:
Martin Prokoph
2025-08-01 19:07:20 +02:00
parent 8037f9a323
commit 93c0ee5f95
4 changed files with 33 additions and 0 deletions

View File

@@ -0,0 +1,24 @@
package eu.midnightdust.yaytris;
import eu.midnightdust.yaytris.util.NightJson;
import java.util.HashMap;
import java.util.Map;
public class HighScores {
private static final NightJson json = new NightJson(HighScores.class, "tetris_scores.json5");
public static Map<String, Integer> scores = new HashMap<>();
public static void addScore(int score) {
scores.put(String.valueOf(System.currentTimeMillis()), score);
write();
}
public static void load() {
json.readJson();
}
public static void write() {
json.writeJson();
}
}

View File

@@ -25,6 +25,7 @@ public class Tetris {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception | Error e) { System.out.printf("%s: %s\n", "Error setting system look and feel", e); }
Settings.load();
HighScores.load();
timer = new Timer("Tetris falling pieces");
space = new Space();
ui = new TetrisUI();
@@ -51,6 +52,7 @@ public class Tetris {
public static void stopGame() {
SoundUtil.stopMusic("/music/theme.wav");
HighScores.addScore(space.getScore());
if (gravityTask != null) gravityTask.cancel();
timer.purge();
if (ui.getMenuPanel() instanceof ScoreMenu) ((ScoreMenu) ui.getMenuPanel()).gameOver();

View File

@@ -65,6 +65,10 @@ public class Space {
return gameMap;
}
public int getScore() {
return score;
}
public void onLinesChanged(Tetromino tetromino, int... lines) {
int combo = 0;
Set<Integer> completedLines = new TreeSet<>();

3
tetris_scores.json5 Normal file
View File

@@ -0,0 +1,3 @@
{
"scores": {"1754065988809": 105,"1754067853918": 113,"1754067067271": 49}
}