This commit is contained in:
Cavallium 2018-10-16 19:28:43 +02:00
parent 38c8710929
commit f7a7f6d200
6 changed files with 39 additions and 13 deletions

View File

@ -78,6 +78,7 @@ public class TetrisGame {
if (move(this.currentTetromino, 0, 0, 0) == false) {
// Lose
this.gameStatus = GameStatus.LOST;
playAgain();
}
}
}
@ -95,9 +96,9 @@ public class TetrisGame {
if (scored) {
this.score += WIDTH;
for (int x = 0; x < WIDTH; x++) {
int y = HEIGHT - i - 2;
while (i + y > 0) {
this.grid[x + (i + y + 1) * WIDTH] = this.grid[x + (i + y) * WIDTH];
int y = i;
while (y > 0) {
this.grid[x + (y) * WIDTH] = this.grid[x + (y-1) * WIDTH];
y--;
}
}
@ -185,4 +186,8 @@ public class TetrisGame {
public BlockColor[] getRenderedGrid() {
return renderedGrid;
}
public int getScore() {
return this.score;
}
}

View File

@ -85,7 +85,7 @@ public class TetrisScreen extends Screen {
if (type != null) {
r.glFillRect(leftOffset + x * 5, topOffset + (y+3) * 5, 5, 5, renderedGrid[offset].ordinal() * 5, 0, 5, 5);
} else {
r.glFillRect(leftOffset + x * 5, topOffset + (y+3) * 5, 5, 5, 1 * 5, 0, 2, 2);
r.glFillRect(leftOffset + x * 5, topOffset + (y+3) * 5, 5, 5, 7 * 5, 0, 5, 5);
}
}
}
@ -93,22 +93,26 @@ public class TetrisScreen extends Screen {
Tetromino nextTetromino = g.getNextTetromino();
if (nextTetromino != null) {
r.glColor3f(0.25f, 0.25f, 0.25f);
r.glFillColor(leftOffset + (TetrisGame.WIDTH + 3) * 5, topOffset + 3 * 5, 5*4, 5*4);
r.glColor3f(1,1,1);
boolean[] renderedNextTetromino = nextTetromino.getRenderedBlock();
final BlockColor type = nextTetromino.getColor();
int nextTetrominoGridSize = nextTetromino.getTetrominoGridSize();
int nextGridOffset = 4*5/2 - nextTetrominoGridSize*5/2;
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);
r.glFillRect(leftOffset + nextGridOffset + (TetrisGame.WIDTH + 3 + x) * 5, topOffset + nextGridOffset + (3 + y) * 5, 5, 5, type.ordinal() * 5, 0, 5, 5);
}
}
}
}
}
r.glColor3f(1,1,1);
r.glDrawStringLeft(leftOffset + (TetrisGame.WIDTH + 3) * 5, topOffset + (3+5) * 5, "SCORE:"+g.getScore());
}
@Override
@ -132,6 +136,7 @@ public class TetrisScreen extends Screen {
}
case OK: {
okPressed = true;
g.playAgain();
return true;
}
case BACK: {
@ -179,6 +184,6 @@ public class TetrisScreen extends Screen {
@Override
public String getSessionTitle() {
return "Absolutely Not Tetris";
return "Tetris";
}
}

View File

@ -2,7 +2,7 @@ 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);
super(x, y, rotation, TetrominoType.T_PURPLE);
}
@Override

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.2 KiB

After

Width:  |  Height:  |  Size: 2.3 KiB

View File

@ -24,6 +24,7 @@ import it.cavallium.warppi.flow.BehaviorSubject;
import it.cavallium.warppi.flow.Observable;
import it.cavallium.warppi.gui.graphicengine.GraphicEngine;
import it.cavallium.warppi.gui.graphicengine.RenderingLoop;
import it.unimi.dsi.fastutil.objects.Object2IntArrayMap;
public class HtmlEngine implements GraphicEngine {
@ -70,6 +71,15 @@ public class HtmlEngine implements GraphicEngine {
}
private String previousValue = "";
private static final Object2IntArrayMap<String> keyNames = new Object2IntArrayMap<>();
static {
keyNames.put(" ", 32);
keyNames.put("ArrowUp", 38);
keyNames.put("ArrowDown", 40);
keyNames.put("ArrowLeft", 37);
keyNames.put("ArrowRight", 39);
}
@JSBody(params = { "ctx", "enabled" }, script = "" + "ctx.mozImageSmoothingEnabled = enabled;" + "ctx.oImageSmoothingEnabled = enabled;" + "ctx.webkitImageSmoothingEnabled = enabled;" + "ctx.msImageSmoothingEnabled = enabled;" + "ctx.imageSmoothingEnabled = enabled;")
public static native void setImageSmoothingEnabled(CanvasRenderingContext2D ctx, boolean enabled);
@ -110,12 +120,18 @@ public class HtmlEngine implements GraphicEngine {
HtmlEngine.document.getBody().appendChild(keyInput);
keyInput.setTabIndex(0);
keyInput.setValue("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
keyInput.addEventListener("keydown", (final KeyboardEvent evt) -> {
HtmlEngine.document.addEventListener("keydown", (final KeyboardEvent evt) -> {
evt.preventDefault();
new Thread(() -> {
previousValue = keyInput.getValue();
Keyboard.debugKey(evt.getKeyCode(), false);
Keyboard.debugKey(evt.getKeyCode(), true);
Keyboard.debugKey(keyNames .getOrDefault(evt.getKey(), evt.getKeyCode()), false);
System.out.println(evt.getKeyCode());
System.out.println("" + (int) evt.getKey().charAt(0));
}).start();
});
HtmlEngine.document.addEventListener("keyup", (final KeyboardEvent evt) -> {
evt.preventDefault();
new Thread(() -> {
Keyboard.debugKey(keyNames .getOrDefault(evt.getKey(), evt.getKeyCode()), true);
System.out.println(evt.getKeyCode());
System.out.println("" + (int) evt.getKey().charAt(0));
}).start();