Simple HTML Tetris Game - Play Now!
<html>
<head>
<title>Simple HTML Tetris Game</title>
</head>
<body>
<div style='background-color: #F0F8FF; width: 400px; height: 400px;'>
<div style='margin: 0 auto; width: 250px;'>
<div id='tetris' style='overflow: hidden; position: relative;'>
<div id='tetris_board' style='background-color: #999999; width: 200px; height: 400px; position: absolute; left: 0; bottom: 0;'>
</div>
<div id='tetris_score' style='position: absolute; top: 0; right: 0; font-family: Verdana; font-size: 10px; font-weight: bold; color: #666666; padding: 5px;'>
Score: 0
</div>
</div>
</div>
</div>
<script>
// Tetris Board Variables
let board = [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
];
// Tetromino Variables
let tetrominoes = [
[1, 1, 1, 1],
[2, 2, 2, 0, 0, 2],
[0, 3, 3, 0, 3, 3],
[4, 0, 0, 4, 4, 4],
[0, 0, 5, 5, 5, 5],
[6, 6, 6, 6, 0, 0],
[0, 7, 7, 7, 7]
];
// Position and Score Variables
let x = 0;
let y = 0;
let score = 0;
<pre><code> // Draw the board
function drawBoard() {
let boardDiv = document.getElementById('tetris_board');
let output = '';
for (let i = 0; i < board.length; i++) {
output += '<div class='row'>';
for (let j = 0; j < board[i].length; j++) {
if (board[i][j] === 0) {
output += '<div class='empty'></div>';
} else {
output += '<div class='filled'></div>';
}
}
output += '</div>';
}
boardDiv.innerHTML = output;
}
// Move the Tetromino
function moveTetromino(keyCode) {
switch (keyCode) {
case 37:
if (x > 0) {
x--;
}
break;
case 38:
if (y > 0) {
y--;
}
break;
case 39:
if (x < 9) {
x++;
}
break;
case 40:
if (y < 19) {
y++;
}
break;
}
drawBoard();
}
// Rotate the Tetromino
function rotateTetromino(keyCode) {
if (keyCode === 32) {
let tetromino = tetrominoes[Math.floor(Math.random() * tetrominoes.length)];
for (let i = 0; i < tetromino.length; i++) {
board[y + i][x] = tetromino[i];
}
}
drawBoard();
}
// Update the Score
function updateScore() {
let scoreDiv = document.getElementById('tetris_score');
scoreDiv.innerHTML = 'Score: ' + score;
}
// Delete Filled Rows
function deleteFilledRows() {
for (let i = 0; i < board.length; i++) {
let isRowFilled = true;
for (let j = 0; j < board[i].length; j++) {
if (board[i][j] === 0) {
isRowFilled = false;
break;
}
}
if (isRowFilled) {
for (let k = i; k > 1; k--) {
board[k] = board[k - 1];
}
board[0] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
score++;
}
}
updateScore();
drawBoard();
}
// Game Loop
function gameLoop() {
if (y < 19) {
y++;
} else {
deleteFilledRows();
}
drawBoard();
}
// Key Down Event
document.addEventListener('keydown', function(event) {
moveTetromino(event.keyCode);
rotateTetromino(event.keyCode);
});
// Start the Game
setInterval(gameLoop, 1000);
</script>
</code></pre>
</body>
</html>
原文地址: https://www.cveoy.top/t/topic/lmlT 著作权归作者所有。请勿转载和采集!