feat: add timer for automatically falling pieces

This commit is contained in:
Martin Prokoph
2025-06-27 23:33:43 +02:00
parent b1be4e0731
commit 2ca0fcfd86
2 changed files with 19 additions and 3 deletions

View File

@@ -5,6 +5,8 @@ import eu.midnightdust.yaytris.ui.TetrisUI;
import javax.swing.*;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
public class Tetris {
public static final Random random = new Random();
@@ -19,5 +21,15 @@ public class Tetris {
Settings.load();
space = new Space();
ui = new TetrisUI();
Timer timer = new Timer("Tetris falling pieces");
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
if (Tetris.space.getCurrentTetromino() != null) {
Tetris.space.getCurrentTetromino().fall(1);
Tetris.ui.getGamePanel().repaint();
}
}
}, 50, 1000);
}
}

View File

@@ -56,7 +56,7 @@ public class TetrisUI extends JFrame implements KeyListener {
private void rescale() {
this.setSize((int) (400 * guiScale), (int) (300 * guiScale));
titleLabel.setBounds(scale(225), scale(7), scale(110), scale(30));
gamePanel.setBounds(scale(10), scale(10), scale(150), scale(260));
gamePanel.setBounds(scale(10), scale(10), scale(150), scale(256));
for (Component comp : this.getComponents()){
if (comp instanceof JComponent) setFontScale((JComponent) comp);
}
@@ -69,6 +69,10 @@ public class TetrisUI extends JFrame implements KeyListener {
//if (label.getFont() != null) label.setFont(label.getFont().deriveFont((float) label.getFont().getSize() * guiScale));
}
public GameCanvas getGamePanel() {
return gamePanel;
}
public void startGame(ActionEvent actionEvent) {
//this.remove(menuPanel);
//menuPanel = null;
@@ -79,7 +83,7 @@ public class TetrisUI extends JFrame implements KeyListener {
public void openMainMenu(ActionEvent actionEvent) {
if (this.menuPanel != null) this.remove(menuPanel);
rescale();
menuPanel = new MainMenu(scale(170), scale(40), scale(220), scale(230), this);
menuPanel = new MainMenu(scale(170), scale(40), scale(220), scale(226), this);
menuPanel.setBackground(Color.DARK_GRAY);
menuPanel.setBorder(new LineBorder(Color.GRAY, scale(2)));
this.add(menuPanel);
@@ -88,7 +92,7 @@ public class TetrisUI extends JFrame implements KeyListener {
public void openSettings(ActionEvent actionEvent) {
if (this.menuPanel != null) this.remove(menuPanel);
menuPanel = new SettingsMenu(scale(170), scale(40), scale(220), scale(230), this);
menuPanel = new SettingsMenu(scale(170), scale(40), scale(220), scale(226), this);
menuPanel.setBackground(Color.DARK_GRAY);
menuPanel.setBorder(new LineBorder(Color.GRAY, scale(2)));
this.add(menuPanel);