diff options
| author | Gustav Sörnäs <gustav@sornas.net> | 2021-03-02 08:56:52 +0100 |
|---|---|---|
| committer | Gustav Sörnäs <gustav@sornas.net> | 2021-03-02 08:56:52 +0100 |
| commit | 71f5dca36ec89722f921971a9846a715b178d057 (patch) | |
| tree | 00d1813775c6bb3e2800137ec5b97c2ca1313fe0 /src/se | |
| parent | 78b8b53daebba36369c5041fa64904b86d5b5345 (diff) | |
| download | tdde30-71f5dca36ec89722f921971a9846a715b178d057.tar.gz | |
highscorelist
Diffstat (limited to 'src/se')
| -rw-r--r-- | src/se/liu/gusso230/tetris/Board.java | 37 | ||||
| -rw-r--r-- | src/se/liu/gusso230/tetris/BoardTester.java | 3 | ||||
| -rw-r--r-- | src/se/liu/gusso230/tetris/Highscore.java | 11 | ||||
| -rw-r--r-- | src/se/liu/gusso230/tetris/HighscoreList.java | 20 | ||||
| -rw-r--r-- | src/se/liu/gusso230/tetris/TetrisComponent.java | 34 |
5 files changed, 91 insertions, 14 deletions
diff --git a/src/se/liu/gusso230/tetris/Board.java b/src/se/liu/gusso230/tetris/Board.java index 88d5cc0..b164792 100644 --- a/src/se/liu/gusso230/tetris/Board.java +++ b/src/se/liu/gusso230/tetris/Board.java @@ -1,13 +1,15 @@ package se.liu.gusso230.tetris; +import javax.swing.*; import java.util.ArrayList; import java.util.List; import java.util.Random; public class Board { - private enum GameState { + public enum GameState { RUNNING, GAME_OVER, + SHOWING_HIGHSCORES, } private GameState state = GameState.RUNNING; @@ -21,11 +23,13 @@ public class Board { private int fallingY; private int points = 0; + private HighscoreList highscores; + private List<BoardListener> boardListeners; private final static Random RND = new Random(); - public Board(int width, int height) { + public Board(int width, int height, HighscoreList highscores) { this.height = height; this.width = width; squares = new SquareType[height][width]; @@ -36,6 +40,10 @@ public class Board { } boardListeners = new ArrayList<>(); tetrominoMaker = new TetrominoMaker(); + this.highscores = highscores; + highscores.addHighscore(new Highscore("a", 100)); + highscores.addHighscore(new Highscore("b", 200)); + highscores.addHighscore(new Highscore("c", 300)); } public void tick() { @@ -45,9 +53,14 @@ public class Board { } else { moveFalling(); } + checkFullLines(); } + } - checkFullLines(); + private void gameOver() { + state = GameState.GAME_OVER; + highscores.addHighscore(new Highscore(JOptionPane.showInputDialog("Your name: "), points)); + state = GameState.SHOWING_HIGHSCORES; } private void moveFalling() { @@ -65,7 +78,7 @@ public class Board { fallingX = (width - falling.getBoundingBoxSize()) / 2; fallingY = -1; if (hasFallingCollision()) { - state = GameState.GAME_OVER; + gameOver(); } notifyListeners(); } @@ -206,6 +219,14 @@ public class Board { return points; } + public GameState getState() { + return state; + } + + public List<Highscore> getHighscores() { + return highscores.getHighscores(); + } + public void setFalling(final Poly falling) { this.falling = falling; notifyListeners(); @@ -221,6 +242,14 @@ public class Board { notifyListeners(); } + public void setState(final GameState state) { + this.state = state; + } + + public void storeHighscore(String name) { + highscores.addHighscore(new Highscore(name, points)); + } + /** * Randomize all squares inside this board. */ diff --git a/src/se/liu/gusso230/tetris/BoardTester.java b/src/se/liu/gusso230/tetris/BoardTester.java index 2bd30ab..e24173e 100644 --- a/src/se/liu/gusso230/tetris/BoardTester.java +++ b/src/se/liu/gusso230/tetris/BoardTester.java @@ -5,7 +5,8 @@ import java.awt.event.ActionEvent; public class BoardTester { public static void main(String[] args) { - Board board = new Board(10, 20); + HighscoreList highscores = new HighscoreList(); + Board board = new Board(10, 20, highscores); TetrisViewer viewer = new TetrisViewer(board); viewer.show(); diff --git a/src/se/liu/gusso230/tetris/Highscore.java b/src/se/liu/gusso230/tetris/Highscore.java new file mode 100644 index 0000000..d72c9c5 --- /dev/null +++ b/src/se/liu/gusso230/tetris/Highscore.java @@ -0,0 +1,11 @@ +package se.liu.gusso230.tetris; + +public class Highscore { + String name; + int points; + + public Highscore(final String name, final int points) { + this.name = name; + this.points = points; + } +} diff --git a/src/se/liu/gusso230/tetris/HighscoreList.java b/src/se/liu/gusso230/tetris/HighscoreList.java new file mode 100644 index 0000000..31b88d7 --- /dev/null +++ b/src/se/liu/gusso230/tetris/HighscoreList.java @@ -0,0 +1,20 @@ +package se.liu.gusso230.tetris; + +import java.util.ArrayList; +import java.util.List; + +public class HighscoreList { + List<Highscore> highscores; + + public HighscoreList() { + highscores = new ArrayList<>(); + } + + public void addHighscore(Highscore highscore) { + highscores.add(highscore); + } + + public List<Highscore> getHighscores() { + return highscores; + } +} diff --git a/src/se/liu/gusso230/tetris/TetrisComponent.java b/src/se/liu/gusso230/tetris/TetrisComponent.java index 33a3642..52fc1ed 100644 --- a/src/se/liu/gusso230/tetris/TetrisComponent.java +++ b/src/se/liu/gusso230/tetris/TetrisComponent.java @@ -4,6 +4,7 @@ import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.util.EnumMap; +import java.util.List; public class TetrisComponent extends JComponent implements BoardListener { private static final int TILE_SIZE = 28; @@ -56,17 +57,32 @@ public class TetrisComponent extends JComponent implements BoardListener { super.paintComponent(g); final Graphics2D g2d = (Graphics2D) g; - for (int y = 0; y < board.getHeight(); y++) { - for (int x = 0; x < board.getWidth(); x++) { - g.setColor(getColor(x, y)); - g.fillRect(x * (TILE_SIZE + TILE_GAP), y * (TILE_SIZE + TILE_GAP), TILE_SIZE, TILE_SIZE); - } + switch (board.getState()) { + case RUNNING: + for (int y = 0; y < board.getHeight(); y++) { + for (int x = 0; x < board.getWidth(); x++) { + g.setColor(getColor(x, y)); + g.fillRect(x * (TILE_SIZE + TILE_GAP), y * (TILE_SIZE + TILE_GAP), TILE_SIZE, TILE_SIZE); + } + } + + g.setColor(Color.BLACK); + g.setFont(new Font("serif", Font.PLAIN, 22)); + g.drawString(Integer.toString(board.getPoints()), 10, 40); + break; + case SHOWING_HIGHSCORES: + List<Highscore> highscores = board.getHighscores(); + g.setColor(Color.BLACK); + g.setFont(new Font("serif", Font.PLAIN, 22)); + g.drawString("Highscores: ", 10, 40); + for (int i = 0; i < highscores.size(); i++) { + g.drawString(String.format("%s: %d", highscores.get(i).name, highscores.get(i).points), 10, 40 * (i + 2)); + } + break; + case GAME_OVER: + break; } - g.setColor(Color.BLACK); - g.setFont(new Font("serif", Font.PLAIN, 22)); - g.drawString(Integer.toString(board.getPoints()), 10, 40); - requestFocus(); } |
