Tetris/JavaScript: Difference between revisions
m
→Code: Fixed syntax highlighting.
m (→Code) |
m (→Code: Fixed syntax highlighting.) |
||
(3 intermediate revisions by one other user not shown) | |||
Line 2:
WASD or arrow keys.
===Code===
{{trans|Java}}
<
<html lang='en'>
<head>
<meta charset='UTF-8'>
Line 19 ⟶ 20:
</style>
</head>
<body>
<canvas></canvas>
Line 75 ⟶ 77:
var keyDown = false;
var fastDown = false;
var grid = [];
Line 98 ⟶ 99:
case 'ArrowLeft':
if (canMove(fallingShape, left))
move(
break;
Line 104 ⟶ 105:
case 'ArrowRight':
if (canMove(fallingShape, right))
move(
break;
Line 112 ⟶ 113:
fastDown = true;
while (canMove(fallingShape, down)) {
move(
draw();
}
Line 132 ⟶ 133:
function canRotate(s) {
if (s === Shapes.Square)
return false;
Line 149 ⟶ 150:
var newCol = fallingShapeCol + p[0];
var newRow = fallingShapeRow + p[1];
return grid[newRow][newCol] === EMPTY;
});
}
function rotate(s) {
if (s === Shapes.Square)
return;
Line 164 ⟶ 165:
}
function move(
fallingShapeRow += dir.y;
fallingShapeCol += dir.x;
Line 173 ⟶ 174:
var newCol = fallingShapeCol + dir.x + p[0];
var newRow = fallingShapeRow + dir.y + p[1];
return grid[newRow][newCol] === EMPTY;
});
}
Line 192 ⟶ 193:
for (var r = 0; r < nRows - 1; r++) {
for (var c = 1; c < nCols - 1; c++) {
if (grid[r][c] === EMPTY)
break;
if (c === nCols - 2) {
count++;
removeLine(r);
Line 401 ⟶ 402:
g.lineWidth = smallStroke;
g.strokeStyle = squareBorder;
g.
}
Line 458 ⟶ 459:
}
var requestId = requestAnimationFrame(
animate(lastFrameTime);
});
var time = new Date().getTime();
var delay = scoreboard.getSpeed();
Line 469 ⟶ 472:
if (canMove(fallingShape, down)) {
move(
} else {
shapeHasLanded();
Line 486 ⟶ 489:
selectShape();
scoreboard.reset();
animate(-1);
}
Line 499 ⟶ 502:
fill(grid[r], EMPTY);
for (var c = 0; c < nCols; c++) {
if (c === 0 || c === nCols - 1 || r === nRows - 1)
grid[r][c] = BORDER;
}
Line 516 ⟶ 519:
</body>
</html></
|