Extra
This commit is contained in:
parent
1d4c2ec8a7
commit
1a51ae5698
@ -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,7 +68,70 @@ 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() {
|
||||
@ -96,6 +172,10 @@ public class TetrisGame {
|
||||
}
|
||||
}
|
||||
|
||||
public Tetromino getNextTetromino() {
|
||||
return this.nextTetromino;
|
||||
}
|
||||
|
||||
private void drawCurrentTetromino(BlockColor[] grid) {
|
||||
currentTetromino.draw(grid, WIDTH);
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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() {
|
||||
|
@ -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,
|
||||
|
@ -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,
|
||||
|
@ -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,
|
||||
|
@ -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,
|
||||
|
@ -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,
|
||||
|
@ -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,
|
||||
|
@ -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,
|
||||
|
Loading…
x
Reference in New Issue
Block a user