feat: add difficulty setting
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
package eu.midnightdust.yaytris;
|
package eu.midnightdust.yaytris;
|
||||||
|
|
||||||
|
import eu.midnightdust.yaytris.util.Difficulty;
|
||||||
import eu.midnightdust.yaytris.util.NightJson;
|
import eu.midnightdust.yaytris.util.NightJson;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@@ -11,6 +12,7 @@ public class Settings {
|
|||||||
public static int musicVolume = 100;
|
public static int musicVolume = 100;
|
||||||
public static int soundVolume = 100;
|
public static int soundVolume = 100;
|
||||||
public static float guiScale = 3.f;
|
public static float guiScale = 3.f;
|
||||||
|
public static Difficulty difficulty = Difficulty.NORMAL;
|
||||||
//public static Map<String, Integer> highScores = new HashMap<>();
|
//public static Map<String, Integer> highScores = new HashMap<>();
|
||||||
|
|
||||||
public static void load() {
|
public static void load() {
|
||||||
|
|||||||
@@ -6,12 +6,12 @@ import eu.midnightdust.yaytris.ui.TetrisUI;
|
|||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import java.util.Timer;
|
import java.util.Timer;
|
||||||
import java.util.TimerTask;
|
|
||||||
|
|
||||||
public class Tetris {
|
public class Tetris {
|
||||||
public static final Random random = new Random();
|
public static final Random random = new Random();
|
||||||
public static Space space;
|
public static Space space;
|
||||||
static TetrisUI ui;
|
public static Timer timer;
|
||||||
|
public static TetrisUI ui;
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
try {
|
try {
|
||||||
@@ -19,17 +19,8 @@ public class Tetris {
|
|||||||
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
|
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
|
||||||
} catch (Exception | Error e) { System.out.printf("%s: %s\n", "Error setting system look and feel", e); }
|
} catch (Exception | Error e) { System.out.printf("%s: %s\n", "Error setting system look and feel", e); }
|
||||||
Settings.load();
|
Settings.load();
|
||||||
|
timer = new Timer("Tetris falling pieces");
|
||||||
space = new Space();
|
space = new Space();
|
||||||
ui = new TetrisUI();
|
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 final Color[][] gameMap; // Bereits abgesetzte Tetrominos werden nur noch als einzelne Farben ('Blobs') auf der Karte abgespeichert
|
||||||
private TetrominoShape nextShape;
|
private TetrominoShape nextShape;
|
||||||
private Tetromino currentTetromino;
|
private Tetromino currentTetromino;
|
||||||
|
private int score;
|
||||||
|
|
||||||
public Space() {
|
public Space() {
|
||||||
gameMap = new Color[12][7];
|
gameMap = new Color[12][7];
|
||||||
nextShape = getNextShape();
|
nextShape = getNextShape();
|
||||||
|
score = 0;
|
||||||
|
|
||||||
Tetromino mino = new Tetromino(TetrominoShape.T);
|
Tetromino mino = new Tetromino(TetrominoShape.T);
|
||||||
mino.move(-2);
|
mino.move(-2);
|
||||||
@@ -62,7 +64,7 @@ public class Space {
|
|||||||
return gameMap;
|
return gameMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int onLinesChanged(Tetromino tetromino, int... lines) {
|
public void onLinesChanged(Tetromino tetromino, int... lines) {
|
||||||
int combo = 0;
|
int combo = 0;
|
||||||
Set<Integer> completedLines = new TreeSet<>();
|
Set<Integer> completedLines = new TreeSet<>();
|
||||||
for (int line : lines) {
|
for (int line : lines) {
|
||||||
@@ -85,7 +87,8 @@ public class Space {
|
|||||||
gameMap[i] = (i-1 < 0) ? new Color[gameMap[i].length] : gameMap[i-1];
|
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() {
|
public Tetromino getCurrentTetromino() {
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package eu.midnightdust.yaytris.ui;
|
package eu.midnightdust.yaytris.ui;
|
||||||
|
|
||||||
|
import eu.midnightdust.yaytris.Settings;
|
||||||
import eu.midnightdust.yaytris.Tetris;
|
import eu.midnightdust.yaytris.Tetris;
|
||||||
import eu.midnightdust.yaytris.game.Space;
|
import eu.midnightdust.yaytris.game.Space;
|
||||||
|
|
||||||
@@ -11,8 +12,10 @@ import java.awt.event.ActionEvent;
|
|||||||
import java.awt.event.KeyEvent;
|
import java.awt.event.KeyEvent;
|
||||||
import java.awt.event.KeyListener;
|
import java.awt.event.KeyListener;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.TimerTask;
|
||||||
|
|
||||||
import static eu.midnightdust.yaytris.Settings.guiScale;
|
import static eu.midnightdust.yaytris.Settings.guiScale;
|
||||||
|
import static eu.midnightdust.yaytris.Tetris.timer;
|
||||||
|
|
||||||
public class TetrisUI extends JFrame implements KeyListener {
|
public class TetrisUI extends JFrame implements KeyListener {
|
||||||
JLabel titleLabel;
|
JLabel titleLabel;
|
||||||
@@ -74,6 +77,16 @@ public class TetrisUI extends JFrame implements KeyListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void startGame(ActionEvent actionEvent) {
|
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);
|
//this.remove(menuPanel);
|
||||||
//menuPanel = null;
|
//menuPanel = null;
|
||||||
this.requestFocus();
|
this.requestFocus();
|
||||||
@@ -134,11 +147,11 @@ public class TetrisUI extends JFrame implements KeyListener {
|
|||||||
@Override
|
@Override
|
||||||
public void keyPressed(KeyEvent e) {
|
public void keyPressed(KeyEvent e) {
|
||||||
//System.out.println("Pressed");
|
//System.out.println("Pressed");
|
||||||
if (e.getKeyCode() == KeyEvent.VK_W) {
|
if (e.getKeyCode() == KeyEvent.VK_E) {
|
||||||
Tetris.space.spawnTetromino();
|
Tetris.space.spawnTetromino();
|
||||||
//Tetris.space.onLinesChanged(Tetris.space.getCurrentTetromino(), 0, 1, 2, 3);
|
//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.getCurrentTetromino().rotate();
|
||||||
//Tetris.space.onLinesChanged(Tetris.space.getCurrentTetromino(), 0, 1, 2, 3);
|
//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.getCurrentTetromino().move(-1);
|
||||||
//Tetris.space.onLinesChanged(Tetris.space.getCurrentTetromino(), 0, 1, 2, 3);
|
//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.getCurrentTetromino().fall(1);
|
||||||
//Tetris.space.onLinesChanged(Tetris.space.getCurrentTetromino(), 0, 1, 2, 3);
|
//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();
|
gamePanel.repaint();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
14
src/main/java/eu/midnightdust/yaytris/util/Difficulty.java
Normal file
14
src/main/java/eu/midnightdust/yaytris/util/Difficulty.java
Normal 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.
Reference in New Issue
Block a user