Simple HTML Tetris Game: Build Your Own Classic!
<!DOCTYPE html>
<html>
<head>
<title>Simple Tetris Game</title>
<style>
body {
background-color: #000;
}
div {
position: absolute;
width: 30px;
height: 30px;
background-color: #999;
}
</style>
<script>
// Define the canvas
var canvas = document.getElementById('game');
var ctx = canvas.getContext('2d');
<pre><code> // Define the pieces
var pieces = [
[1, 1, 0],
[2, 3, 2],
[0, 0, 1],
[1, 2, 0],
[2, 2, 1]
];
// Define the starting position of the pieces
var x = 3;
var y = 0;
// Define the falling speed of the pieces
var speed = 1000;
// Define the game loop
function draw() {
// Clear the canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw the pieces
for (let i = 0; i < pieces.length; i++) {
for (let j = 0; j < pieces[i].length; j++) {
if (pieces[i][j] !== 0) {
ctx.fillRect(x + j * 30, y + i * 30, 30, 30);
}
}
}
// Move the pieces down
y += 30;
// Check for collision
if (y + pieces.length * 30 > canvas.height) {
// Stop the game loop
clearInterval(interval);
}
}
// Start the game loop
var interval = setInterval(draw, speed);
</script>
</code></pre>
</head>
<body>
<canvas id='game' width='450' height='600'></canvas>
</body>
</html>
原文地址: https://www.cveoy.top/t/topic/lmoW 著作权归作者所有。请勿转载和采集!