New extra features
This commit is contained in:
parent
b7407886dd
commit
5e1d1dde52
@ -1,10 +1,11 @@
|
||||
package it.cavallium.warppi.extra.tetris;
|
||||
|
||||
public enum BlockType {
|
||||
public enum BlockColor {
|
||||
RED,
|
||||
GREEN,
|
||||
BLUE,
|
||||
YELLOW,
|
||||
ORANGE,
|
||||
VIOLET
|
||||
PURPLE,
|
||||
CYAN
|
||||
}
|
@ -1,14 +1,20 @@
|
||||
package it.cavallium.warppi.extra.tetris;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class TetrisGame {
|
||||
|
||||
public static final int WIDTH = 10, HEIGHT = 22;
|
||||
private BlockType[] grid;
|
||||
private BlockType[] hovergrid;
|
||||
private BlockType[] renderedGrid;
|
||||
public static final double TICK_TIME = 0.25;
|
||||
private BlockColor[] grid;
|
||||
private BlockColor[] hovergrid;
|
||||
private volatile BlockColor[] renderedGrid;
|
||||
private GameStatus gameStatus;
|
||||
private int score;
|
||||
private double currentTime;
|
||||
private double tickTimer;
|
||||
private Tetromino currentTetromino;
|
||||
private Tetromino nextTetromino;
|
||||
|
||||
public TetrisGame() {
|
||||
resetVariables();
|
||||
@ -17,19 +23,29 @@ public class TetrisGame {
|
||||
void playAgain() {
|
||||
resetVariables();
|
||||
gameStatus = GameStatus.PLAYING;
|
||||
placeNextTetromino();
|
||||
}
|
||||
|
||||
private void resetVariables() {
|
||||
grid = new BlockType[WIDTH * HEIGHT];
|
||||
hovergrid = new BlockType[WIDTH * HEIGHT];
|
||||
renderedGrid = new BlockType[WIDTH * HEIGHT];
|
||||
grid = new BlockColor[WIDTH * HEIGHT];
|
||||
hovergrid = new BlockColor[WIDTH * HEIGHT];
|
||||
renderedGrid = new BlockColor[WIDTH * HEIGHT];
|
||||
score = 0;
|
||||
currentTime = 0;
|
||||
tickTimer = 0;
|
||||
gameStatus = GameStatus.INITIAL;
|
||||
currentTetromino = null;
|
||||
nextTetromino = generateRandomTetromino();
|
||||
nextTetromino.fixInitialPosition();
|
||||
}
|
||||
|
||||
public void gameTick(float dt, boolean leftPressed, boolean rightPressed, boolean downPressed, boolean okPressed, boolean backPressed) {
|
||||
public void update(float dt, boolean leftPressed, boolean rightPressed, boolean downPressed, boolean okPressed, boolean backPressed) {
|
||||
currentTime += dt;
|
||||
tickTimer += dt;
|
||||
while (tickTimer >= TICK_TIME) {
|
||||
tickTimer -= TICK_TIME;
|
||||
gameTick(leftPressed, rightPressed, downPressed, okPressed, backPressed);
|
||||
}
|
||||
if (gameStatus == GameStatus.INITIAL) {
|
||||
playAgain();
|
||||
} else {
|
||||
@ -37,18 +53,54 @@ public class TetrisGame {
|
||||
}
|
||||
renderGrid();
|
||||
}
|
||||
|
||||
public void gameTick(boolean leftPressed, boolean rightPressed, boolean downPressed, boolean okPressed, boolean backPressed) {
|
||||
this.currentTetromino.setY((byte) (this.currentTetromino.getY() - 1));
|
||||
}
|
||||
|
||||
public void renderGrid() {
|
||||
this.renderedGrid = new BlockType[WIDTH*HEIGHT];
|
||||
this.renderedGrid = Arrays.copyOf(grid, grid.length);
|
||||
drawCurrentTetromino(this.renderedGrid);
|
||||
for (int y = 0; y < HEIGHT; y++) {
|
||||
for (int x = 0; x < WIDTH; x++) {
|
||||
final int offset = x+y*WIDTH;
|
||||
renderedGrid[offset] = hovergrid[offset] != null ? hovergrid[offset] : grid[offset];
|
||||
renderedGrid[offset] = hovergrid[offset] != null ? hovergrid[offset] : renderedGrid[offset];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public BlockType[] getRenderedGrid() {
|
||||
private void placeNextTetromino() {
|
||||
currentTetromino = nextTetromino;
|
||||
nextTetromino = generateRandomTetromino();
|
||||
nextTetromino.fixInitialPosition();
|
||||
}
|
||||
|
||||
private Tetromino generateRandomTetromino() {
|
||||
int s = (int) (Math.random() * 7);
|
||||
final byte middleX = (byte)((WIDTH - 1)/2), middleY = (byte)(HEIGHT - 1), rotation = (byte) (Math.random() * 4);
|
||||
switch (s) {
|
||||
case 0:
|
||||
return new TetrominoICyan(middleX, middleY, rotation);
|
||||
case 1:
|
||||
return new TetrominoJBlue(middleX, middleY, rotation);
|
||||
case 2:
|
||||
return new TetrominoLOrange(middleX, middleY, rotation);
|
||||
case 3:
|
||||
return new TetrominoOYellow(middleX, middleY, rotation);
|
||||
case 4:
|
||||
return new TetrominoSGreen(middleX, middleY, rotation);
|
||||
case 5:
|
||||
return new TetrominoTPurple(middleX, middleY, rotation);
|
||||
default:
|
||||
return new TetrominoZRed(middleX, middleY, rotation);
|
||||
}
|
||||
}
|
||||
|
||||
private void drawCurrentTetromino(BlockColor[] grid) {
|
||||
currentTetromino.draw(grid, WIDTH);
|
||||
}
|
||||
|
||||
public BlockColor[] getRenderedGrid() {
|
||||
return renderedGrid;
|
||||
}
|
||||
}
|
||||
|
@ -63,7 +63,7 @@ public class TetrisScreen extends Screen {
|
||||
@Override
|
||||
public void beforeRender(final float dt) {
|
||||
Engine.INSTANCE.getHardwareDevice().getDisplayManager().renderer.glClearColor(0xff000000);
|
||||
g.gameTick(dt, leftPressed, rightPressed, downPressed, okPressed, backPressed);
|
||||
g.update(dt, leftPressed, rightPressed, downPressed, okPressed, backPressed);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -72,17 +72,20 @@ public class TetrisScreen extends Screen {
|
||||
TetrisScreen.skin.use(e);
|
||||
}
|
||||
r.glColor3f(1, 1, 1);
|
||||
BlockType[] renderedGrid = g.getRenderedGrid();
|
||||
BlockColor[] renderedGrid = g.getRenderedGrid();
|
||||
int centerScreen = StaticVars.screenSize[0]/2;
|
||||
int centerGrid = TetrisGame.WIDTH*5/2-1;
|
||||
int centerGrid = TetrisGame.WIDTH*6/2-1;
|
||||
final int leftOffset = centerScreen - centerGrid;
|
||||
final int topOffset = StaticVars.screenSize[1] - TetrisGame.HEIGHT*5-1;
|
||||
final int topOffset = StaticVars.screenSize[1] - TetrisGame.HEIGHT*6-1;
|
||||
for (int y = 0; y < TetrisGame.HEIGHT; y++) {
|
||||
for (int x = 0; x < TetrisGame.WIDTH; x++) {
|
||||
final int offset = x+y*TetrisGame.WIDTH;
|
||||
final BlockType type = renderedGrid[offset];
|
||||
if (type != null) r.glFillRect(leftOffset + x * 4, y * 4, 4, 4, renderedGrid[offset].ordinal() * 4, 0, 4, 4);
|
||||
else r.glFillRect(leftOffset + x * 5, topOffset + y * 5, 4, 4, 2 * 4, 0, 4, 4);
|
||||
final BlockColor type = renderedGrid[offset];
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,126 @@
|
||||
package it.cavallium.warppi.extra.tetris;
|
||||
|
||||
public abstract class Tetromino {
|
||||
private byte x, y, rotation;
|
||||
private final TetrominoType type;
|
||||
public final boolean o = false, w = true;
|
||||
|
||||
public Tetromino(byte x, byte y, byte rotation, TetrominoType type) {
|
||||
super();
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.rotation = rotation;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public byte getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public void setX(byte x) {
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
public byte getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
public void setY(byte y) {
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public byte getRotation() {
|
||||
return rotation;
|
||||
}
|
||||
|
||||
public void setRotation(byte rotation) {
|
||||
this.rotation = rotation;
|
||||
}
|
||||
|
||||
public TetrominoType getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public BlockColor getColor() {
|
||||
switch(type) {
|
||||
case I_CYAN:
|
||||
return BlockColor.CYAN;
|
||||
case J_BLUE:
|
||||
return BlockColor.BLUE;
|
||||
case L_ORANGE:
|
||||
return BlockColor.ORANGE;
|
||||
case O_YELLOW:
|
||||
return BlockColor.YELLOW;
|
||||
case S_GREEN:
|
||||
return BlockColor.GREEN;
|
||||
case T_PURPLE:
|
||||
return BlockColor.PURPLE;
|
||||
case Z_RED:
|
||||
return BlockColor.RED;
|
||||
default:
|
||||
return BlockColor.RED;
|
||||
}
|
||||
}
|
||||
|
||||
public void draw(BlockColor[] grid, final int WIDTH) {
|
||||
boolean[] blockGrid = getRenderedBlock();
|
||||
final int tetrominoGridSize = getTetrominoGridSize();
|
||||
final int centerOffset = (int) Math.floor((double)tetrominoGridSize/2d);
|
||||
final BlockColor type = getColor();
|
||||
for (int bx = 0; bx < tetrominoGridSize; bx++) {
|
||||
for (int by = 0; by < tetrominoGridSize; by++) {
|
||||
if (blockGrid[bx+by*tetrominoGridSize] == w) {
|
||||
final int index = x+bx-centerOffset + (y+by-centerOffset) * WIDTH;
|
||||
if (index < grid.length && index >= 0) {
|
||||
grid[index] = type;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void fixInitialPosition() {
|
||||
this.y -= (byte) (this.getTetrominoGridSize()/2);
|
||||
}
|
||||
|
||||
public abstract int getTetrominoGridSize();
|
||||
protected abstract boolean[] getRenderedBlock();
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + rotation;
|
||||
result = prime * result + ((type == null) ? 0 : type.hashCode());
|
||||
result = prime * result + x;
|
||||
result = prime * result + y;
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
Tetromino other = (Tetromino) obj;
|
||||
if (rotation != other.rotation)
|
||||
return false;
|
||||
if (type != other.type)
|
||||
return false;
|
||||
if (x != other.x)
|
||||
return false;
|
||||
if (y != other.y)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Tetromino = {\n\t\"x\": \"" + x + "\",\n\ty\": \"" + y + "\",\n\trotation\": \"" + rotation + "\",\n\ttype\": \"" + type + "\"\n}";
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
package it.cavallium.warppi.extra.tetris;
|
||||
|
||||
public class TetrominoICyan extends Tetromino {
|
||||
public TetrominoICyan(byte x, byte y, byte rotation) {
|
||||
super(x, y, rotation, TetrominoType.I_CYAN);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getTetrominoGridSize() {
|
||||
return 4;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean[] getRenderedBlock() {
|
||||
switch(getRotation()) {
|
||||
case 0:
|
||||
return new boolean[] {
|
||||
o,o,o,o,
|
||||
w,w,w,w,
|
||||
o,o,o,o,
|
||||
o,o,o,o
|
||||
};
|
||||
case 1:
|
||||
return new boolean[] {
|
||||
o,o,w,o,
|
||||
o,o,w,o,
|
||||
o,o,w,o,
|
||||
o,o,w,o
|
||||
};
|
||||
case 2:
|
||||
return new boolean[] {
|
||||
o,o,o,o,
|
||||
o,o,o,o,
|
||||
w,w,w,w,
|
||||
o,o,o,o
|
||||
};
|
||||
case 3:
|
||||
return new boolean[] {
|
||||
o,w,o,o,
|
||||
o,w,o,o,
|
||||
o,w,o,o,
|
||||
o,w,o,o
|
||||
};
|
||||
default:
|
||||
throw new NullPointerException();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
package it.cavallium.warppi.extra.tetris;
|
||||
|
||||
public class TetrominoJBlue extends Tetromino {
|
||||
public TetrominoJBlue(byte x, byte y, byte rotation) {
|
||||
super(x, y, rotation, TetrominoType.J_BLUE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getTetrominoGridSize() {
|
||||
return 3;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean[] getRenderedBlock() {
|
||||
switch(getRotation()) {
|
||||
case 0:
|
||||
return new boolean[] {
|
||||
w,o,o,
|
||||
w,w,w,
|
||||
o,o,o
|
||||
};
|
||||
case 1:
|
||||
return new boolean[] {
|
||||
o,w,w,
|
||||
o,w,o,
|
||||
o,w,o
|
||||
};
|
||||
case 2:
|
||||
return new boolean[] {
|
||||
o,o,o,
|
||||
w,w,w,
|
||||
o,o,w
|
||||
};
|
||||
case 3:
|
||||
return new boolean[] {
|
||||
o,w,o,
|
||||
o,w,o,
|
||||
w,w,o
|
||||
};
|
||||
default:
|
||||
throw new NullPointerException();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
package it.cavallium.warppi.extra.tetris;
|
||||
|
||||
public class TetrominoLOrange extends Tetromino {
|
||||
public TetrominoLOrange(byte x, byte y, byte rotation) {
|
||||
super(x, y, rotation, TetrominoType.L_ORANGE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getTetrominoGridSize() {
|
||||
return 3;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean[] getRenderedBlock() {
|
||||
switch(getRotation()) {
|
||||
case 0:
|
||||
return new boolean[] {
|
||||
o,o,w,
|
||||
w,w,w,
|
||||
o,o,o
|
||||
};
|
||||
case 1:
|
||||
return new boolean[] {
|
||||
o,w,o,
|
||||
o,w,o,
|
||||
o,w,w
|
||||
};
|
||||
case 2:
|
||||
return new boolean[] {
|
||||
o,o,o,
|
||||
w,w,w,
|
||||
w,o,o
|
||||
};
|
||||
case 3:
|
||||
return new boolean[] {
|
||||
w,w,o,
|
||||
o,w,o,
|
||||
o,w,o
|
||||
};
|
||||
default:
|
||||
throw new NullPointerException();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package it.cavallium.warppi.extra.tetris;
|
||||
|
||||
public class TetrominoOYellow extends Tetromino {
|
||||
public TetrominoOYellow(byte x, byte y, byte rotation) {
|
||||
super(x, y, rotation, TetrominoType.O_YELLOW);
|
||||
}
|
||||
@Override
|
||||
public int getTetrominoGridSize() {
|
||||
return 2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean[] getRenderedBlock() {
|
||||
return new boolean[] {
|
||||
w,w,
|
||||
w,w,
|
||||
};
|
||||
}
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
package it.cavallium.warppi.extra.tetris;
|
||||
|
||||
public class TetrominoSGreen extends Tetromino {
|
||||
public TetrominoSGreen(byte x, byte y, byte rotation) {
|
||||
super(x, y, rotation, TetrominoType.S_GREEN);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getTetrominoGridSize() {
|
||||
return 3;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean[] getRenderedBlock() {
|
||||
switch(getRotation()) {
|
||||
case 0:
|
||||
return new boolean[] {
|
||||
o,w,w,
|
||||
w,w,o,
|
||||
o,o,o
|
||||
};
|
||||
case 1:
|
||||
return new boolean[] {
|
||||
o,w,o,
|
||||
o,w,w,
|
||||
o,o,w
|
||||
};
|
||||
case 2:
|
||||
return new boolean[] {
|
||||
o,o,o,
|
||||
o,w,w,
|
||||
w,w,o
|
||||
};
|
||||
case 3:
|
||||
return new boolean[] {
|
||||
w,o,o,
|
||||
w,w,o,
|
||||
o,w,o
|
||||
};
|
||||
default:
|
||||
throw new NullPointerException();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
package it.cavallium.warppi.extra.tetris;
|
||||
|
||||
public class TetrominoTPurple extends Tetromino {
|
||||
public TetrominoTPurple(byte x, byte y, byte rotation) {
|
||||
super(x, y, rotation, TetrominoType.I_CYAN);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getTetrominoGridSize() {
|
||||
return 3;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean[] getRenderedBlock() {
|
||||
switch(getRotation()) {
|
||||
case 0:
|
||||
return new boolean[] {
|
||||
o,w,o,
|
||||
w,w,w,
|
||||
o,o,o
|
||||
};
|
||||
case 1:
|
||||
return new boolean[] {
|
||||
o,w,o,
|
||||
o,w,w,
|
||||
o,w,o
|
||||
};
|
||||
case 2:
|
||||
return new boolean[] {
|
||||
o,o,o,
|
||||
w,w,w,
|
||||
o,w,o
|
||||
};
|
||||
case 3:
|
||||
return new boolean[] {
|
||||
o,w,o,
|
||||
w,w,o,
|
||||
o,w,o
|
||||
};
|
||||
default:
|
||||
throw new NullPointerException();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package it.cavallium.warppi.extra.tetris;
|
||||
|
||||
public enum TetrominoType {
|
||||
I_CYAN,
|
||||
O_YELLOW,
|
||||
T_PURPLE,
|
||||
S_GREEN,
|
||||
Z_RED,
|
||||
J_BLUE,
|
||||
L_ORANGE
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
package it.cavallium.warppi.extra.tetris;
|
||||
|
||||
public class TetrominoZRed extends Tetromino {
|
||||
public TetrominoZRed(byte x, byte y, byte rotation) {
|
||||
super(x, y, rotation, TetrominoType.Z_RED);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getTetrominoGridSize() {
|
||||
return 3;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean[] getRenderedBlock() {
|
||||
switch(getRotation()) {
|
||||
case 0:
|
||||
return new boolean[] {
|
||||
w,w,o,
|
||||
o,w,w,
|
||||
o,o,o
|
||||
};
|
||||
case 1:
|
||||
return new boolean[] {
|
||||
o,o,w,
|
||||
o,w,w,
|
||||
o,w,o
|
||||
};
|
||||
case 2:
|
||||
return new boolean[] {
|
||||
o,o,o,
|
||||
w,w,o,
|
||||
o,w,w
|
||||
};
|
||||
case 3:
|
||||
return new boolean[] {
|
||||
o,w,o,
|
||||
w,w,o,
|
||||
w,o,o
|
||||
};
|
||||
default:
|
||||
throw new NullPointerException();
|
||||
}
|
||||
}
|
||||
}
|
Binary file not shown.
Before Width: | Height: | Size: 1.7 KiB After Width: | Height: | Size: 2.2 KiB |
Binary file not shown.
Loading…
Reference in New Issue
Block a user