summaryrefslogtreecommitdiffstats
path: root/src/se
diff options
context:
space:
mode:
authorGustav Sörnäs <gustav@sornas.net>2021-02-08 08:42:52 +0100
committerGustav Sörnäs <gustav@sornas.net>2021-02-08 08:51:33 +0100
commita5c082375721faf4bbd869e6d04255e96b18764c (patch)
treeb5f6c51ece5e194d6c7edf19c25f5abb414be66f /src/se
parent64d9b21780d4a3b760ff699611c16781f6b0dbab (diff)
downloadtdde30-a5c082375721faf4bbd869e6d04255e96b18764c.tar.gz
4.4
Diffstat (limited to 'src/se')
-rw-r--r--src/se/liu/gusso230/tetris/Board.java28
-rw-r--r--src/se/liu/gusso230/tetris/BoardTester.java10
-rw-r--r--src/se/liu/gusso230/tetris/Poly.java4
-rw-r--r--src/se/liu/gusso230/tetris/TetrominoMaker.java13
4 files changed, 37 insertions, 18 deletions
diff --git a/src/se/liu/gusso230/tetris/Board.java b/src/se/liu/gusso230/tetris/Board.java
index ee791ac..141cacf 100644
--- a/src/se/liu/gusso230/tetris/Board.java
+++ b/src/se/liu/gusso230/tetris/Board.java
@@ -9,6 +9,7 @@ public class Board {
private int width;
private int height;
+ private TetrominoMaker tetrominoMaker;
private Poly falling = null;
private int fallingX;
private int fallingY;
@@ -17,10 +18,6 @@ public class Board {
private final static Random RND = new Random();
- public static void main(String[] args) {
- Board board = new Board(5, 10);
- }
-
public Board(int width, int height) {
this.height = height;
this.width = width;
@@ -31,6 +28,28 @@ public class Board {
}
}
boardListeners = new ArrayList<>();
+ tetrominoMaker = new TetrominoMaker();
+ }
+
+ public void tick() {
+ System.out.println("tick");
+ if (falling == null) {
+ newFalling();
+ } else {
+ moveFalling();
+ }
+ }
+
+ private void moveFalling() {
+ fallingY += 1;
+ notifyListeners();
+ }
+
+ private void newFalling() {
+ falling = tetrominoMaker.getRandomPoly(RND);
+ fallingX = (width - falling.getBoundingBoxSize()) / 2;
+ fallingY = -1;
+ notifyListeners();
}
public int getWidth() {
@@ -92,6 +111,7 @@ public class Board {
public void addListener(BoardListener bl) {
boardListeners.add(bl);
+ notifyListeners();
}
private void notifyListeners() {
diff --git a/src/se/liu/gusso230/tetris/BoardTester.java b/src/se/liu/gusso230/tetris/BoardTester.java
index 7d7770c..14f3b5a 100644
--- a/src/se/liu/gusso230/tetris/BoardTester.java
+++ b/src/se/liu/gusso230/tetris/BoardTester.java
@@ -7,20 +7,16 @@ public class BoardTester {
public static void main(String[] args) {
Board board = new Board(10, 20);
- board.setFalling(new TetrominoMaker().getPoly(0));
- board.setFallingX(0);
- board.setFallingY(0);
-
TetrisViewer viewer = new TetrisViewer(board);
viewer.show();
- final Action doOneStep = new AbstractAction() {
+ final Action tick = new AbstractAction() {
@Override public void actionPerformed(final ActionEvent actionEvent) {
- board.randomize();
+ board.tick();
}
};
- final Timer clockTimer = new Timer(1000, doOneStep);
+ final Timer clockTimer = new Timer(500, tick);
clockTimer.setCoalesce(true);
clockTimer.start();
}
diff --git a/src/se/liu/gusso230/tetris/Poly.java b/src/se/liu/gusso230/tetris/Poly.java
index a337d15..1787194 100644
--- a/src/se/liu/gusso230/tetris/Poly.java
+++ b/src/se/liu/gusso230/tetris/Poly.java
@@ -47,6 +47,10 @@ public class Poly {
}
}
+ public int getBoundingBoxSize() {
+ return boundingBoxSize;
+ }
+
public Point[] getPoints() {
return points;
}
diff --git a/src/se/liu/gusso230/tetris/TetrominoMaker.java b/src/se/liu/gusso230/tetris/TetrominoMaker.java
index ee5ce1a..8d79d17 100644
--- a/src/se/liu/gusso230/tetris/TetrominoMaker.java
+++ b/src/se/liu/gusso230/tetris/TetrominoMaker.java
@@ -2,6 +2,7 @@ package se.liu.gusso230.tetris;
import java.util.ArrayList;
import java.util.List;
+import java.util.Random;
public class TetrominoMaker {
private List<Poly> polys;
@@ -10,17 +11,11 @@ public class TetrominoMaker {
polys = new ArrayList<>();
polys.add(new Poly(SquareType.I, new Point[] { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(3, 1), }));
-
polys.add(new Poly(SquareType.J, new Point[] { new Point(0, 0), new Point(0, 1), new Point(1, 1), new Point(2, 1), }));
-
polys.add(new Poly(SquareType.L, new Point[] { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(2, 0), }));
-
polys.add(new Poly(SquareType.O, new Point[] { new Point(0, 0), new Point(0, 1), new Point(1, 0), new Point(1, 1), }));
-
- polys.add(new Poly(SquareType.S, new Point[] { new Point(0, 0), new Point(0, 1), new Point(1, 0), new Point(1, 1), }));
-
+ polys.add(new Poly(SquareType.S, new Point[] { new Point(0, 1), new Point(1, 1), new Point(1, 0), new Point(2, 0), }));
polys.add(new Poly(SquareType.T, new Point[] { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(1, 0), }));
-
polys.add(new Poly(SquareType.Z, new Point[] { new Point(0, 0), new Point(1, 0), new Point(1, 1), new Point(2, 1), }));
}
@@ -34,4 +29,8 @@ public class TetrominoMaker {
}
return polys.get(n);
}
+
+ public Poly getRandomPoly(Random rnd) {
+ return polys.get(rnd.nextInt(polys.size()));
+ }
}