clean: improve code structure

This commit is contained in:
Martin Prokoph
2025-08-01 16:51:14 +02:00
parent 7ef6e246ff
commit 4d175761a5
3 changed files with 17 additions and 19 deletions

View File

@@ -45,16 +45,7 @@ public class Tetris {
SoundUtil.playMusic("/music/theme.wav", true); SoundUtil.playMusic("/music/theme.wav", true);
space.spawnTetromino(); space.spawnTetromino();
startTime = LocalTime.now(); startTime = LocalTime.now();
gravityTask = new TimerTask() { gravityTask = new GravityTimerTask();
@Override
public void run() {
if (space.getCurrentTetromino() != null) {
updateTime();
space.getCurrentTetromino().fall();
ui.getGamePanel().repaint();
}
}
};
timer.scheduleAtFixedRate(gravityTask, 1, Settings.difficulty.getTimerPeriod()); timer.scheduleAtFixedRate(gravityTask, 1, Settings.difficulty.getTimerPeriod());
} }
@@ -72,4 +63,15 @@ public class Tetris {
public static void updateTime() { public static void updateTime() {
if (ui.getMenuPanel() instanceof ScoreMenu) ((ScoreMenu) ui.getMenuPanel()).updateTime(startTime); if (ui.getMenuPanel() instanceof ScoreMenu) ((ScoreMenu) ui.getMenuPanel()).updateTime(startTime);
} }
public static class GravityTimerTask extends TimerTask {
@Override
public void run() {
if (space.getCurrentTetromino() != null) {
updateTime();
space.getCurrentTetromino().fall();
ui.getGamePanel().repaint();
}
}
}
} }

View File

@@ -49,12 +49,11 @@ public class Space {
Color[][] tempGameMap = new Color[gameMap.length][gameMap[0].length]; Color[][] tempGameMap = new Color[gameMap.length][gameMap[0].length];
for (int y = 0; y < tempGameMap.length; y++) { for (int y = 0; y < tempGameMap.length; y++) {
System.arraycopy(gameMap[y], 0, tempGameMap[y], 0, tempGameMap[y].length); System.arraycopy(gameMap[y], 0, tempGameMap[y], 0, tempGameMap[y].length);
if (currentTetromino != null) { if (currentTetromino == null) continue;
Color[] newBlobs = currentTetromino.getLine(y); Color[] newBlobs = currentTetromino.getLine(y);
for (int i = 0; i < newBlobs.length; i++) { for (int i = 0; i < newBlobs.length; i++) {
if (newBlobs[i] == null) continue; if (newBlobs[i] == null) continue;
tempGameMap[y][i] = newBlobs[i]; tempGameMap[y][i] = newBlobs[i];
}
} }
} }

View File

@@ -1,7 +1,6 @@
package eu.midnightdust.yaytris.ui; package eu.midnightdust.yaytris.ui;
import eu.midnightdust.yaytris.Tetris; import eu.midnightdust.yaytris.Tetris;
import eu.midnightdust.yaytris.game.Space;
import eu.midnightdust.yaytris.util.CatppuccinColor; import eu.midnightdust.yaytris.util.CatppuccinColor;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
@@ -79,8 +78,6 @@ public class TetrisUI extends JFrame implements KeyListener {
public void startGame(ActionEvent actionEvent) { public void startGame(ActionEvent actionEvent) {
Tetris.startGame(); Tetris.startGame();
//this.remove(menuPanel);
//menuPanel = null;
this.openScoreMenu(actionEvent); this.openScoreMenu(actionEvent);
this.requestFocus(); this.requestFocus();
this.repaint(); this.repaint();