feat: add difficulty setting

This commit is contained in:
Martin Prokoph
2025-06-28 00:01:00 +02:00
parent 2ca0fcfd86
commit 41af446f94
6 changed files with 44 additions and 17 deletions

View File

@@ -1,5 +1,6 @@
package eu.midnightdust.yaytris;
import eu.midnightdust.yaytris.util.Difficulty;
import eu.midnightdust.yaytris.util.NightJson;
import java.util.HashMap;
@@ -11,6 +12,7 @@ public class Settings {
public static int musicVolume = 100;
public static int soundVolume = 100;
public static float guiScale = 3.f;
public static Difficulty difficulty = Difficulty.NORMAL;
//public static Map<String, Integer> highScores = new HashMap<>();
public static void load() {

View File

@@ -6,12 +6,12 @@ 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();
public static Space space;
static TetrisUI ui;
public static Timer timer;
public static TetrisUI ui;
public static void main(String[] args) {
try {
@@ -19,17 +19,8 @@ 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();
timer = new Timer("Tetris falling pieces");
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

@@ -9,10 +9,12 @@ public class Space {
private final Color[][] gameMap; // Bereits abgesetzte Tetrominos werden nur noch als einzelne Farben ('Blobs') auf der Karte abgespeichert
private TetrominoShape nextShape;
private Tetromino currentTetromino;
private int score;
public Space() {
gameMap = new Color[12][7];
nextShape = getNextShape();
score = 0;
Tetromino mino = new Tetromino(TetrominoShape.T);
mino.move(-2);
@@ -62,7 +64,7 @@ public class Space {
return gameMap;
}
public int onLinesChanged(Tetromino tetromino, int... lines) {
public void onLinesChanged(Tetromino tetromino, int... lines) {
int combo = 0;
Set<Integer> completedLines = new TreeSet<>();
for (int line : lines) {
@@ -85,7 +87,8 @@ public class Space {
gameMap[i] = (i-1 < 0) ? new Color[gameMap[i].length] : gameMap[i-1];
}
}
return combo;
this.score += combo;
//System.out.println(score);
}
public Tetromino getCurrentTetromino() {

View File

@@ -1,5 +1,6 @@
package eu.midnightdust.yaytris.ui;
import eu.midnightdust.yaytris.Settings;
import eu.midnightdust.yaytris.Tetris;
import eu.midnightdust.yaytris.game.Space;
@@ -11,8 +12,10 @@ import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;
import java.util.TimerTask;
import static eu.midnightdust.yaytris.Settings.guiScale;
import static eu.midnightdust.yaytris.Tetris.timer;
public class TetrisUI extends JFrame implements KeyListener {
JLabel titleLabel;
@@ -74,6 +77,16 @@ public class TetrisUI extends JFrame implements KeyListener {
}
public void startGame(ActionEvent actionEvent) {
Tetris.space.spawnTetromino();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
if (Tetris.space.getCurrentTetromino() != null) {
Tetris.space.getCurrentTetromino().fall(1);
Tetris.ui.getGamePanel().repaint();
}
}
}, 1, Settings.difficulty.getTimerPeriod());
//this.remove(menuPanel);
//menuPanel = null;
this.requestFocus();
@@ -134,11 +147,11 @@ public class TetrisUI extends JFrame implements KeyListener {
@Override
public void keyPressed(KeyEvent e) {
//System.out.println("Pressed");
if (e.getKeyCode() == KeyEvent.VK_W) {
if (e.getKeyCode() == KeyEvent.VK_E) {
Tetris.space.spawnTetromino();
//Tetris.space.onLinesChanged(Tetris.space.getCurrentTetromino(), 0, 1, 2, 3);
}
else if (e.getKeyCode() == KeyEvent.VK_S) {
else if (e.getKeyCode() == KeyEvent.VK_W) {
Tetris.space.getCurrentTetromino().rotate();
//Tetris.space.onLinesChanged(Tetris.space.getCurrentTetromino(), 0, 1, 2, 3);
}
@@ -150,10 +163,14 @@ public class TetrisUI extends JFrame implements KeyListener {
Tetris.space.getCurrentTetromino().move(-1);
//Tetris.space.onLinesChanged(Tetris.space.getCurrentTetromino(), 0, 1, 2, 3);
}
else if (e.getKeyCode() == KeyEvent.VK_SPACE) {
else if (e.getKeyCode() == KeyEvent.VK_S) {
Tetris.space.getCurrentTetromino().fall(1);
//Tetris.space.onLinesChanged(Tetris.space.getCurrentTetromino(), 0, 1, 2, 3);
}
else if (e.getKeyCode() == KeyEvent.VK_SPACE) {
Tetris.space.getCurrentTetromino().fall(12);
//Tetris.space.onLinesChanged(Tetris.space.getCurrentTetromino(), 0, 1, 2, 3);
}
gamePanel.repaint();
}

View File

@@ -0,0 +1,14 @@
package eu.midnightdust.yaytris.util;
public enum Difficulty {
NOOB(2000), EASY(1200), NORMAL(1000), HARD(750), EXTREME(500), WTF(100);
private final int timerPeriod;
Difficulty(int timerPeriod) {
this.timerPeriod = timerPeriod;
}
public int getTimerPeriod() {
return timerPeriod;
}
}

Binary file not shown.