From 1a51ae5698e46f4d25bf8a843816b1599e6ac6ab Mon Sep 17 00:00:00 2001 From: Cavallium Date: Tue, 16 Oct 2018 15:56:27 +0200 Subject: [PATCH] Extra --- .../warppi/extra/tetris/TetrisGame.java | 84 ++++++++++++++++++- .../warppi/extra/tetris/TetrisScreen.java | 35 +++++++- .../warppi/extra/tetris/Tetromino.java | 6 +- .../warppi/extra/tetris/TetrominoICyan.java | 4 +- .../warppi/extra/tetris/TetrominoJBlue.java | 4 +- .../warppi/extra/tetris/TetrominoLOrange.java | 4 +- .../warppi/extra/tetris/TetrominoOYellow.java | 2 +- .../warppi/extra/tetris/TetrominoSGreen.java | 4 +- .../warppi/extra/tetris/TetrominoTPurple.java | 4 +- .../warppi/extra/tetris/TetrominoZRed.java | 4 +- 10 files changed, 133 insertions(+), 18 deletions(-) diff --git a/core/src/main/java/it/cavallium/warppi/extra/tetris/TetrisGame.java b/core/src/main/java/it/cavallium/warppi/extra/tetris/TetrisGame.java index 410f718c..8d4ea909 100644 --- a/core/src/main/java/it/cavallium/warppi/extra/tetris/TetrisGame.java +++ b/core/src/main/java/it/cavallium/warppi/extra/tetris/TetrisGame.java @@ -39,9 +39,22 @@ public class TetrisGame { nextTetromino.fixInitialPosition(); } - public void update(float dt, boolean leftPressed, boolean rightPressed, boolean downPressed, boolean okPressed, boolean backPressed) { + public void update(float dt, boolean leftPressed, boolean rightPressed, boolean downPressed, boolean upPressed, boolean okPressed, boolean backPressed) { currentTime += dt; tickTimer += dt; + if (!(leftPressed && rightPressed)) { + if (leftPressed) { + move(this.currentTetromino, -1, 0, 0); + } else if (rightPressed) { + move(this.currentTetromino, 1, 0, 0); + } + } + if (downPressed) { + move(this.currentTetromino, 0, -1, 0); + } + if (upPressed) { + move(this.currentTetromino, 0, 0, 1); + } while (tickTimer >= TICK_TIME) { tickTimer -= TICK_TIME; gameTick(leftPressed, rightPressed, downPressed, okPressed, backPressed); @@ -55,9 +68,72 @@ public class TetrisGame { } public void gameTick(boolean leftPressed, boolean rightPressed, boolean downPressed, boolean okPressed, boolean backPressed) { - this.currentTetromino.setY((byte) (this.currentTetromino.getY() - 1)); + if (move(this.currentTetromino, 0, -1, 0)) { + + } else { + // Spawn new tetromino and write the old to the permanent grid + drawCurrentTetromino(grid); + checkLines(); + placeNextTetromino(); + if (move(this.currentTetromino, 0, 0, 0) == false) { + // Lose + this.gameStatus = GameStatus.LOST; + } + } } + private void checkLines() { + for(int i = 0; i < HEIGHT; i++) { + boolean scored = true; + for (int x = 0; x < WIDTH; x++) { + if (this.grid[x + i * WIDTH] == null) { + scored = false; + break; + } + } + if (scored) { + this.score += WIDTH; + for (int x = 0; x < WIDTH; x++) { + int y = 1; + while (i + y < HEIGHT) { + this.grid[x + (i + y - 1) * WIDTH] = this.grid[x + (i + y) * WIDTH]; + y++; + } + } + } + } + } + + private boolean move(Tetromino t, int dX, int dY, int dRotation) { + byte rot = (byte) ((t.getRotation() + dRotation) % 4); + boolean[] block = t.getRenderedBlock(rot); + int blockSize = t.getTetrominoGridSize(); + int half1 = (int) Math.floor(((double)t.getTetrominoGridSize())/2d); + int half2 = blockSize - half1; + byte aX = (byte)(t.getX()+dX), aY = (byte)(t.getY()+dY); + int blockX = 0, blockY = 0; + for (int x = aX - half1; x < aX + half2; x++) { + for (int y = aY - half1; y < aY + half2; y++) { + if (block[blockX + blockY * blockSize] == true) { + if (x >= 0 & y >= 0 & x < WIDTH & (x + y * WIDTH) < this.grid.length) { + if (this.grid[x + y * WIDTH] != null) { + return false; + } + } else { + return false; + } + } + blockY++; + } + blockY = 0; + blockX++; + } + t.setRotation(rot); + t.setX(aX); + t.setY(aY); + return true; + } + public void renderGrid() { this.renderedGrid = Arrays.copyOf(grid, grid.length); drawCurrentTetromino(this.renderedGrid); @@ -95,6 +171,10 @@ public class TetrisGame { return new TetrominoZRed(middleX, middleY, rotation); } } + + public Tetromino getNextTetromino() { + return this.nextTetromino; + } private void drawCurrentTetromino(BlockColor[] grid) { currentTetromino.draw(grid, WIDTH); diff --git a/core/src/main/java/it/cavallium/warppi/extra/tetris/TetrisScreen.java b/core/src/main/java/it/cavallium/warppi/extra/tetris/TetrisScreen.java index 30c5c402..82312a1f 100644 --- a/core/src/main/java/it/cavallium/warppi/extra/tetris/TetrisScreen.java +++ b/core/src/main/java/it/cavallium/warppi/extra/tetris/TetrisScreen.java @@ -22,6 +22,8 @@ public class TetrisScreen extends Screen { private boolean rightPressed; + private boolean upPressed; + private boolean downPressed; private boolean okPressed; @@ -61,7 +63,8 @@ public class TetrisScreen extends Screen { @Override public void beforeRender(final float dt) { Engine.INSTANCE.getHardwareDevice().getDisplayManager().renderer.glClearColor(0xff000000); - g.update(dt, leftPressed, rightPressed, downPressed, okPressed, backPressed); + g.update(dt, leftPressed, rightPressed, downPressed, upPressed, okPressed, backPressed); + upPressed = false; } @Override @@ -82,7 +85,27 @@ public class TetrisScreen extends Screen { if (type != null) { r.glFillRect(leftOffset + x * 5, topOffset + (TetrisGame.HEIGHT+3-y) * 5, 5, 5, renderedGrid[offset].ordinal() * 5, 0, 5, 5); } else { -// r.glFillRect(leftOffset + x * 5, topOffset + y * 5, 5, 5, 1 * 5, 0, 5, 5); + //r.glFillRect(leftOffset + x * 5, topOffset + (TetrisGame.HEIGHT+3-y) * 5, 5, 5, 1 * 5, 0, 2, 2); + } + } + } + + + Tetromino nextTetromino = g.getNextTetromino(); + if (nextTetromino != null) { + boolean[] renderedNextTetromino = nextTetromino.getRenderedBlock(); + final BlockColor type = nextTetromino.getColor(); + int nextTetrominoGridSize = nextTetromino.getTetrominoGridSize(); + for (int y = 0; y < nextTetrominoGridSize; y++) { + for (int x = 0; x < nextTetrominoGridSize; x++) { + final int offset = x+y*nextTetrominoGridSize; + if (renderedNextTetromino[offset]) { + if (type != null) { + r.glFillRect(leftOffset + (TetrisGame.WIDTH + 3 + x) * 5, topOffset + (3 - y) * 5, 5, 5, type.ordinal() * 5, 0, 5, 5); + } else { + //r.glFillRect(leftOffset + x * 5, topOffset + (TetrisGame.HEIGHT+3-y) * 5, 5, 5, 1 * 5, 0, 2, 2); + } + } } } } @@ -99,6 +122,10 @@ public class TetrisScreen extends Screen { rightPressed = true; return true; } + case UP: { + upPressed = true; + return true; + } case DOWN: { downPressed = true; return true; @@ -126,6 +153,10 @@ public class TetrisScreen extends Screen { rightPressed = false; return true; } + case UP: { + upPressed = false; + return true; + } case DOWN: { downPressed = false; return true; diff --git a/core/src/main/java/it/cavallium/warppi/extra/tetris/Tetromino.java b/core/src/main/java/it/cavallium/warppi/extra/tetris/Tetromino.java index 0ca620c4..cfc1f3cd 100644 --- a/core/src/main/java/it/cavallium/warppi/extra/tetris/Tetromino.java +++ b/core/src/main/java/it/cavallium/warppi/extra/tetris/Tetromino.java @@ -84,7 +84,11 @@ public abstract class Tetromino { } public abstract int getTetrominoGridSize(); - protected abstract boolean[] getRenderedBlock(); + protected abstract boolean[] getRenderedBlock(byte dRotation); + protected boolean[] getRenderedBlock() { + return getRenderedBlock(this.rotation); + } + @Override public int hashCode() { diff --git a/core/src/main/java/it/cavallium/warppi/extra/tetris/TetrominoICyan.java b/core/src/main/java/it/cavallium/warppi/extra/tetris/TetrominoICyan.java index 4e908b6f..d15a2d7e 100644 --- a/core/src/main/java/it/cavallium/warppi/extra/tetris/TetrominoICyan.java +++ b/core/src/main/java/it/cavallium/warppi/extra/tetris/TetrominoICyan.java @@ -11,8 +11,8 @@ public class TetrominoICyan extends Tetromino { } @Override - public boolean[] getRenderedBlock() { - switch(getRotation()) { + public boolean[] getRenderedBlock(final byte rotation) { + switch(rotation) { case 0: return new boolean[] { o,o,o,o, diff --git a/core/src/main/java/it/cavallium/warppi/extra/tetris/TetrominoJBlue.java b/core/src/main/java/it/cavallium/warppi/extra/tetris/TetrominoJBlue.java index be8049e3..eae5698d 100644 --- a/core/src/main/java/it/cavallium/warppi/extra/tetris/TetrominoJBlue.java +++ b/core/src/main/java/it/cavallium/warppi/extra/tetris/TetrominoJBlue.java @@ -11,8 +11,8 @@ public class TetrominoJBlue extends Tetromino { } @Override - public boolean[] getRenderedBlock() { - switch(getRotation()) { + public boolean[] getRenderedBlock(final byte rotation) { + switch(rotation) { case 0: return new boolean[] { w,o,o, diff --git a/core/src/main/java/it/cavallium/warppi/extra/tetris/TetrominoLOrange.java b/core/src/main/java/it/cavallium/warppi/extra/tetris/TetrominoLOrange.java index 2d8911f0..09d5d384 100644 --- a/core/src/main/java/it/cavallium/warppi/extra/tetris/TetrominoLOrange.java +++ b/core/src/main/java/it/cavallium/warppi/extra/tetris/TetrominoLOrange.java @@ -11,8 +11,8 @@ public class TetrominoLOrange extends Tetromino { } @Override - public boolean[] getRenderedBlock() { - switch(getRotation()) { + public boolean[] getRenderedBlock(final byte rotation) { + switch(rotation) { case 0: return new boolean[] { o,o,w, diff --git a/core/src/main/java/it/cavallium/warppi/extra/tetris/TetrominoOYellow.java b/core/src/main/java/it/cavallium/warppi/extra/tetris/TetrominoOYellow.java index aa2d0602..7425a75d 100644 --- a/core/src/main/java/it/cavallium/warppi/extra/tetris/TetrominoOYellow.java +++ b/core/src/main/java/it/cavallium/warppi/extra/tetris/TetrominoOYellow.java @@ -10,7 +10,7 @@ public class TetrominoOYellow extends Tetromino { } @Override - public boolean[] getRenderedBlock() { + public boolean[] getRenderedBlock(byte rotation) { return new boolean[] { w,w, w,w, diff --git a/core/src/main/java/it/cavallium/warppi/extra/tetris/TetrominoSGreen.java b/core/src/main/java/it/cavallium/warppi/extra/tetris/TetrominoSGreen.java index 10e59ba4..1bb35053 100644 --- a/core/src/main/java/it/cavallium/warppi/extra/tetris/TetrominoSGreen.java +++ b/core/src/main/java/it/cavallium/warppi/extra/tetris/TetrominoSGreen.java @@ -11,8 +11,8 @@ public class TetrominoSGreen extends Tetromino { } @Override - public boolean[] getRenderedBlock() { - switch(getRotation()) { + public boolean[] getRenderedBlock(final byte rotation) { + switch(rotation) { case 0: return new boolean[] { o,w,w, diff --git a/core/src/main/java/it/cavallium/warppi/extra/tetris/TetrominoTPurple.java b/core/src/main/java/it/cavallium/warppi/extra/tetris/TetrominoTPurple.java index bd30e433..d656d6fc 100644 --- a/core/src/main/java/it/cavallium/warppi/extra/tetris/TetrominoTPurple.java +++ b/core/src/main/java/it/cavallium/warppi/extra/tetris/TetrominoTPurple.java @@ -11,8 +11,8 @@ public class TetrominoTPurple extends Tetromino { } @Override - public boolean[] getRenderedBlock() { - switch(getRotation()) { + public boolean[] getRenderedBlock(byte rotation) { + switch(rotation) { case 0: return new boolean[] { o,w,o, diff --git a/core/src/main/java/it/cavallium/warppi/extra/tetris/TetrominoZRed.java b/core/src/main/java/it/cavallium/warppi/extra/tetris/TetrominoZRed.java index 0b7a70d5..5f99f83d 100644 --- a/core/src/main/java/it/cavallium/warppi/extra/tetris/TetrominoZRed.java +++ b/core/src/main/java/it/cavallium/warppi/extra/tetris/TetrominoZRed.java @@ -11,8 +11,8 @@ public class TetrominoZRed extends Tetromino { } @Override - public boolean[] getRenderedBlock() { - switch(getRotation()) { + public boolean[] getRenderedBlock(final byte rotation) { + switch(rotation) { case 0: return new boolean[] { w,w,o,