From b17e88f96da8b9c486393ed6ac19a4d575ae55c2 Mon Sep 17 00:00:00 2001 From: Martin Prokoph Date: Sat, 13 Sep 2025 17:18:06 +0200 Subject: [PATCH] clean: improve code structure --- .../java/eu/midnightdust/yaytris/Main.java | 22 ++++++++ .../java/eu/midnightdust/yaytris/Tetris.java | 53 ++++++++----------- .../eu/midnightdust/yaytris/ui/TetrisUI.java | 4 +- 3 files changed, 46 insertions(+), 33 deletions(-) create mode 100644 src/main/java/eu/midnightdust/yaytris/Main.java diff --git a/src/main/java/eu/midnightdust/yaytris/Main.java b/src/main/java/eu/midnightdust/yaytris/Main.java new file mode 100644 index 0000000..4887c56 --- /dev/null +++ b/src/main/java/eu/midnightdust/yaytris/Main.java @@ -0,0 +1,22 @@ +package eu.midnightdust.yaytris; + +import javax.swing.UIManager; + +public class Main { + + /** + * The application's entry point. + * Initializes the UI library to match the system style. + * Also loads saved settings, translations, and highscores from JSON. + * + * @param args command line arguments – will be ignored + */ + public static void main(String[] args) { + try { + System.setProperty("java.awt.headless", "false"); + UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); + } catch (Exception | Error e) { System.out.printf("%s: %s\n", "Error setting system look and feel", e); } + Tetris.init(); + } + +} diff --git a/src/main/java/eu/midnightdust/yaytris/Tetris.java b/src/main/java/eu/midnightdust/yaytris/Tetris.java index 69fcd44..299c85b 100644 --- a/src/main/java/eu/midnightdust/yaytris/Tetris.java +++ b/src/main/java/eu/midnightdust/yaytris/Tetris.java @@ -1,12 +1,12 @@ package eu.midnightdust.yaytris; import eu.midnightdust.yaytris.game.Space; +import eu.midnightdust.yaytris.game.Tetromino; import eu.midnightdust.yaytris.ui.ScoreMenu; import eu.midnightdust.yaytris.ui.TetrisUI; import eu.midnightdust.yaytris.util.GravityTimerTask; import eu.midnightdust.yaytris.util.sound.SoundUtil; -import javax.swing.*; import java.time.LocalTime; import java.util.Random; import java.util.Timer; @@ -20,30 +20,19 @@ public class Tetris { private static TimerTask gravityTask; private static LocalTime startTime; - /** - * The application's entry point. - * Initializes the UI library to match the system style. - * Also loads saved settings, translations, and highscores from JSON. - * - * @param args command line arguments – will be ignored - */ - public static void main(String[] args) { - try { - System.setProperty("java.awt.headless", "false"); - UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); - } catch (Exception | Error e) { System.out.printf("%s: %s\n", "Error setting system look and feel", e); } + public static void init() { Settings.load(); Translation.load(Settings.language.locale); HighScores.load(); - timer = new Timer("Tetris falling pieces"); - space = new Space(); - ui = new TetrisUI(); + Tetris.timer = new Timer("Tetris falling pieces"); + Tetris.space = new Space(); + Tetris.ui = new TetrisUI(); } /** * Get the active game space * - * @see Space + * @see Space */ public static Space getSpace() { return space; @@ -52,7 +41,7 @@ public class Tetris { /** * Get the ui instance * - * @see TetrisUI + * @see TetrisUI */ public static TetrisUI getUi() { return ui; @@ -61,7 +50,7 @@ public class Tetris { /** * Resets the game space, preparing it for a new game. * - * @see Space + * @see Space */ public static void resetSpace() { SoundUtil.stopMusic("/music/theme.wav"); @@ -72,9 +61,9 @@ public class Tetris { /** * Starts a new game of Tetris :D - * This involves starting our gravity task, playing music and spawning the first {@link eu.midnightdust.yaytris.game.Tetromino} + * This involves starting our gravity task, playing music and spawning the first {@link Tetromino} * - * @see Space#spawnTetromino() + * @see Space#spawnTetromino() */ public static void startGame() { SoundUtil.playMusic("/music/theme.wav", true); @@ -88,8 +77,8 @@ public class Tetris { * Stops the current game. * Disables falling, fades out music and handles saving of high scores. * - * @see ScoreMenu - * @see HighScores + * @see ScoreMenu + * @see HighScores */ public static void stopGame() { SoundUtil.stopMusic("/music/theme.wav"); @@ -97,32 +86,35 @@ public class Tetris { timer.purge(); if (ui.getMenuPanel() instanceof ScoreMenu) ((ScoreMenu) ui.getMenuPanel()).gameOver(); ui.transferFocus(); - if (HighScores.diffToMap(Settings.difficulty).values().stream().noneMatch(hs -> hs > space.getScore())) ui.showHighscoreDialog(space.getScore()); + if (HighScores.diffToMap(Settings.difficulty).values().stream().noneMatch(hs -> hs > space.getScore())) + ui.showHighscoreDialog(space.getScore()); } /** * Updates the displayed score * - * @param score the new score - * @see ScoreMenu + * @param score the new score + * @see ScoreMenu */ public static void updateScore(int score) { if (ui.getMenuPanel() instanceof ScoreMenu) ((ScoreMenu) ui.getMenuPanel()).updateScore(score); updateLevel(score); } + /** * Updates the elapsed time * - * @see ScoreMenu + * @see ScoreMenu */ public static void updateTime() { if (ui.getMenuPanel() instanceof ScoreMenu) ((ScoreMenu) ui.getMenuPanel()).updateTime(startTime); } + /** * Updates the displayed level * - * @param score the new score, from which the level will be calculated - * @see ScoreMenu + * @param score the new score, from which the level will be calculated + * @see ScoreMenu */ public static void updateLevel(int score) { int newLevel = Math.max(0, (int) (score / 1400f)); @@ -136,5 +128,4 @@ public class Tetris { if (ui.getMenuPanel() instanceof ScoreMenu) ((ScoreMenu) ui.getMenuPanel()).updateLevel(newLevel); } } - -} +} \ No newline at end of file diff --git a/src/main/java/eu/midnightdust/yaytris/ui/TetrisUI.java b/src/main/java/eu/midnightdust/yaytris/ui/TetrisUI.java index 0932281..d3ade03 100644 --- a/src/main/java/eu/midnightdust/yaytris/ui/TetrisUI.java +++ b/src/main/java/eu/midnightdust/yaytris/ui/TetrisUI.java @@ -64,7 +64,7 @@ public class TetrisUI extends JFrame implements KeyListener { private void rescale() { this.setSize((int) (400 * guiScale), (int) (320 * guiScale)); titleLabel.setBounds(scale(225), scale(7), scale(110), scale(30)); - titleLabel.setIcon(new ImageIcon(new ImageIcon(titleImage).getImage().getScaledInstance(scale(110), scale(30), Image.SCALE_DEFAULT))); + titleLabel.setIcon(new ImageIcon(titleImage.getScaledInstance(scale(110), scale(30), Image.SCALE_DEFAULT))); gamePanel.setBounds(scale(10), scale(10), scale(150), scale(282)); this.setLocationRelativeTo(null); } @@ -165,7 +165,7 @@ public class TetrisUI extends JFrame implements KeyListener { */ public void showHighscoreDialog(int score) { String playerName = JOptionPane.showInputDialog(null, t("dialog.highscore.action"), t("dialog.highscore.title"), JOptionPane.PLAIN_MESSAGE); - HighScores.addScore(playerName, score); + if (playerName != null) HighScores.addScore(playerName, score); } /**