From e533edac0891482a8059a6284076a2b46ac81c04 Mon Sep 17 00:00:00 2001 From: sigonasr2 Date: Sat, 15 Aug 2020 08:30:30 +0900 Subject: [PATCH] Implement all Tetris basic functions. --- Tetris/src/sig/game/Block.java | 3 + Tetris/src/sig/game/Game.java | 121 +++++++++++++++++---- Tetris/src/sig/game/GameState.java | 7 ++ Tetris/src/sig/game/MoveDirection.java | 12 ++ Tetris/src/sig/game/Player.java | 93 +++++++++++++++- Tetris/src/sig/game/RotationDirection.java | 12 ++ 6 files changed, 221 insertions(+), 27 deletions(-) create mode 100644 Tetris/src/sig/game/GameState.java create mode 100644 Tetris/src/sig/game/MoveDirection.java create mode 100644 Tetris/src/sig/game/RotationDirection.java diff --git a/Tetris/src/sig/game/Block.java b/Tetris/src/sig/game/Block.java index a47584d..0100205 100644 --- a/Tetris/src/sig/game/Block.java +++ b/Tetris/src/sig/game/Block.java @@ -11,4 +11,7 @@ public class Block { this.active=active; this.color=col; } + public String toString() { + return active?"O":"-"; + } } diff --git a/Tetris/src/sig/game/Game.java b/Tetris/src/sig/game/Game.java index 5be5bae..bb5df8f 100644 --- a/Tetris/src/sig/game/Game.java +++ b/Tetris/src/sig/game/Game.java @@ -1,6 +1,7 @@ package sig.game; import java.awt.Point; +import java.util.Arrays; import java.util.Timer; import java.util.TimerTask; @@ -15,6 +16,11 @@ public class Game { public static int tickDelay = 60; public static int rotation = 0; //0-3 public static Grid gameGrid = new Grid(); + + public static int rotateDelay = 20; + public static int rotateTimer = rotateDelay; + + public static GameState state = GameState.PLAYING; public static void main(String[] args) { @@ -26,8 +32,8 @@ public class Game { + "XXXX"), new Shape( "XXXX" - + "OOOO" - + "OXXX" + + "XOOO" + + "XOXX" + "XXXX"), new Shape( "XOOX" @@ -36,8 +42,8 @@ public class Game { + "XXXX"), new Shape( "XXXX" - + "XXXO" - + "OOOO" + + "XXOX" + + "OOOX" + "XXXX") ,Color.ORANGE); final Frame JPiece = new Frame( @@ -47,8 +53,8 @@ public class Game { + "XOOX" + "XXXX"), new Shape( - "OXXX" - + "OOOO" + "XOXX" + + "XOOO" + "XXXX" + "XXXX"), new Shape( @@ -57,8 +63,8 @@ public class Game { + "XOOX" + "XXXX"), new Shape( - "OOOO" - + "XXXO" + "OOOX" + + "XXOX" + "XXXX" + "XXXX") ,Color.BLUE); @@ -193,12 +199,22 @@ public class Game { static void step() { tickDelay--; + rotateTimer--; if (tickDelay<=0) { moveBlock(); tickDelay=levelDelay[level]; } + if (rotateTimer<=0) { + //p.rotateCounterClockwise(); + p.moveBlockLeft(); + rotateTimer=rotateDelay; + } + DrawBoard(); + } + + private static void DrawBoard() { ClearScreen(); - Point[] checkPoints = p.GetPlayerBlocksInGrid(); + Point[] checkPoints = p.GetPlayerBlocksInGrid(p.getCurrentShape()); for (int y=19;y>=0;y--) { for (int x=0;x<10;x++) { boolean plotted=false; @@ -210,7 +226,7 @@ public class Game { } } if (!plotted) { - System.out.print((gameGrid.grid[x][y].active)?'O':'-'); + System.out.print(gameGrid.grid[x][y]); } } System.out.println(); @@ -236,25 +252,88 @@ public class Game { } }*/ - Point[] checkPoints = p.GetPlayerBlocksInGrid(); + Point[] checkPoints = p.GetPlayerBlocksInGrid(p.getCurrentShape()); + + boolean isOccupied = isOccupied(checkPoints,new Point(0,-1)); + if (isOccupied) { + SnapPieceThatCollided(checkPoints); + ClearLinesThatHaveFullRows(); + } else { + MovePlayerBlockDownByOne(); + } + } + + private static void ClearLinesThatHaveFullRows() { + int deletionRows=0; + for (int y=0;y0) { + gameGrid.grid[x][y-deletionRows].active=gameGrid.grid[x][y].active; + gameGrid.grid[x][y].active=false; + } + } + + private static void MovePlayerBlockDownByOne() { + p.pos.translate(0, -1); + } + + private static void SnapPieceThatCollided(Point[] checkPoints) { + for (Point point : checkPoints) { + //If any point in the grid is outside, this is our lose condition. + if (point.y>=gameGrid.grid[0].length) { + state = GameState.LOSE; + } + gameGrid.grid[point.x][point.y].active=true; + } + p.ShuffleNextPiece(); + } + + static boolean isOccupied(Point[] checkPoints) { + return isOccupied(checkPoints,new Point(0,0)); + } + static boolean isOccupied(Point[] checkPoints,Point offset) { boolean isOccupied = false; for (int i=0;i=gameGrid.grid[0].length|| + point.x+offset.x>=gameGrid.grid.length|| + point.x+offset.x<0 + || gameGrid.grid[point.x+offset.x][point.y+offset.y].active) { isOccupied=true; break; } } - - if (isOccupied) { - for (Point point : checkPoints) { - gameGrid.grid[point.x][point.y].active=true; - } - p.ShuffleNextPiece(); - } else { - p.pos.translate(0, -1); - } + return isOccupied; } } diff --git a/Tetris/src/sig/game/GameState.java b/Tetris/src/sig/game/GameState.java new file mode 100644 index 0000000..8141986 --- /dev/null +++ b/Tetris/src/sig/game/GameState.java @@ -0,0 +1,7 @@ +package sig.game; + +public enum GameState { + PLAYING, + WIN, + LOSE; +} diff --git a/Tetris/src/sig/game/MoveDirection.java b/Tetris/src/sig/game/MoveDirection.java new file mode 100644 index 0000000..2037e7c --- /dev/null +++ b/Tetris/src/sig/game/MoveDirection.java @@ -0,0 +1,12 @@ +package sig.game; + +public enum MoveDirection { + LEFT(-1), + RIGHT(1); + + int value; + + MoveDirection(int value) { + this.value=value; + } +} diff --git a/Tetris/src/sig/game/Player.java b/Tetris/src/sig/game/Player.java index 6da9993..24d3e9c 100644 --- a/Tetris/src/sig/game/Player.java +++ b/Tetris/src/sig/game/Player.java @@ -16,10 +16,6 @@ public class Player { nextPiece = SelectRandomTetrimino(); pos = new Point(5,20); } - - private Frame SelectRandomTetrimino() { - return Game.piecePool[(int)(Math.random()*Game.piecePool.length)]; - } public void ShuffleNextPiece() { piece = nextPiece.clone(); @@ -27,9 +23,9 @@ public class Player { pos = new Point(5,20); } - public Point[] GetPlayerBlocksInGrid() { + public Point[] GetPlayerBlocksInGrid(Shape s) { Point[] checkPoints = new Point[4]; - Shape tetShape = piece.shape[Game.rotation]; + Shape tetShape = s; int pointsInserted=0; for (int x=0;x3) { + currentRotation=0; + } + return currentRotation; + } } diff --git a/Tetris/src/sig/game/RotationDirection.java b/Tetris/src/sig/game/RotationDirection.java new file mode 100644 index 0000000..bdee29d --- /dev/null +++ b/Tetris/src/sig/game/RotationDirection.java @@ -0,0 +1,12 @@ +package sig.game; + +public enum RotationDirection { + CLOCKWISE(1), + COUNTERCLOCKWISE(-1); + + int value; + + RotationDirection(int value) { + this.value=value; + } +}