make a tetris game and run in html
- User
make a tetris game and run in html
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
make a tetris game and run in html
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Here's a simple Tetris game in HTML, CSS, and JavaScript:
<!DOCTYPE html>
<html>
<head>
<title>Simple Tetris</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
background-color: #f0f0f0;
}
#game-container {
margin: 20px auto;
position: relative;
}
#tetris {
border: 2px solid #333;
background-color: #111;
}
#score {
margin-top: 10px;
font-size: 18px;
}
#controls {
margin-top: 15px;
}
</style>
</head>
<body>
<h1>Simple Tetris</h1>
<div id="game-container">
<canvas id="tetris" width="240" height="400"></canvas>
<div id="score">Score: 0</div>
<div id="controls">
<p>Controls: ← → to move, ↑ to rotate, ↓ for soft drop, Space for hard drop</p>
<button id="start-button">Start/Restart</button>
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', () => {
const canvas = document.getElementById('tetris');
const context = canvas.getContext('2d');
const scoreElement = document.getElementById('score');
const startButton = document.getElementById('start-button');
const BLOCK_SIZE = 20;
const BOARD_WIDTH = 12;
const BOARD_HEIGHT = 20;
const COLORS = [
null,
'#FF0D72', // I
'#0DC2FF', // J
'#0DFF72', // L
'#F538FF', // O
'#FF8E0D', // S
'#FFE138', // T
'#3877FF' // Z
];
// Tetromino shapes
const PIECES = [
[
[0, 0, 0, 0],
[1, 1, 1, 1],
[0, 0, 0, 0],
[0, 0, 0, 0]
],
[
[2, 0, 0],
[2, 2, 2],
[0, 0, 0]
],
[
[0, 0, 3],
[3, 3, 3],
[0, 0, 0]
],
[
[4, 4],
[4, 4]
],
[
[0, 5, 5],
[5, 5, 0],
[0, 0, 0]
],
[
[0, 6, 0],
[6, 6, 6],
[0, 0, 0]
],
[
[7, 7, 0],
[0, 7, 7],
[0, 0, 0]
]
];
let board = createBoard();
let score = 0;
let gameOver = true;
let requestId = null;
let dropCounter = 0;
let dropInterval = 1000;
let lastTime = 0;
const player = {
pos: {x: 0, y: 0},
matrix: null,
score: 0
};
function createBoard() {
const board = [];
for (let y = 0; y < BOARD_HEIGHT; y++) {
board.push(new Array(BOARD_WIDTH).fill(0));
}
return board;
}
function drawBlock(x, y, color) {
context.fillStyle = color;
context.fillRect(x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE);
context.strokeStyle = '#888';
context.strokeRect(x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE);
}
function drawMatrix(matrix, offset) {
matrix.forEach((row, y) => {
row.forEach((value, x) => {
if (value !== 0) {
drawBlock(
x + offset.x,
y + offset.y,
COLORS[value]
);
}
});
});
}
function draw() {
context.fillStyle = '#111';
context.fillRect(0, 0, canvas.width, canvas.height);
drawMatrix(board, {x: 0, y: 0});
drawMatrix(player.matrix, player.pos);
}
function merge() {
player.matrix.forEach((row, y) => {
row.forEach((value, x) => {
if (value !== 0) {
board[y + player.pos.y][x + player.pos.x] = value;
}
});
});
}
function rotate(matrix, dir) {
for (let y = 0; y < matrix.length; ++y) {
for (let x = 0; x < y; ++x) {
[
matrix[x][y],
matrix[y][x],
] = [
matrix[y][x],
matrix[x][y],
];
}
}
if (dir > 0) {
matrix.forEach(row => row.reverse());
} else {
matrix.reverse();
}
}
function playerRotate(dir) {
const pos = player.pos.x;
let offset = 1;
rotate(player.matrix, dir);
while (collide()) {
player.pos.x += offset;
offset = -(offset + (offset > 0 ? 1 : -1));
if (offset > player.matrix[0].length) {
rotate(player.matrix, -dir);
player.pos.x = pos;
return;
}
}
}
function playerDrop() {
player.pos.y++;
if (collide()) {
player.pos.y--;
merge();
playerReset();
removeRows();
updateScore();
}
dropCounter = 0;
}
function playerHardDrop() {
while (!collide()) {
player.pos.y++;
}
player.pos.y--;
merge();
playerReset();
removeRows();
updateScore();
dropCounter = 0;
}
function playerMove(dir) {
player.pos.x += dir;
if (collide()) {
player.pos.x -= dir;
}
}
function collide() {
const [m, o] = [player.matrix, player.pos];
for (let y = 0; y < m.length; ++y) {
for (let x = 0; x < m[y].length; ++x) {
if (m[y][x] !== 0 &&
(board[y + o.y] &&
board[y + o.y][x + o.x]) !== 0) {
return true;
}
}
}
return false;
}
function playerReset() {
const pieces = 'IJLOSTZ';
player.matrix = PIECES[pieces.indexOf(pieces[Math.floor(Math.random() * pieces.length)])];
player.pos.y = 0;
player.pos.x = Math.floor(BOARD_WIDTH / 2) - Math.floor(player.matrix[0].length / 2);
if (collide()) {
gameOver = true;
cancelAnimationFrame(requestId);
requestId = null;
}
}
function removeRows() {
let rowCount = 0;
outer: for (let y = board.length - 1; y >= 0; --y) {
for (let x = 0; x < board[y].length; ++x) {
if (board[y][x] === 0) {
continue outer;
}
}
const row = board.splice(y, 1)[0].fill(0);
board.unshift(row);
++y;
rowCount++;
}
if (rowCount > 0) {
player.score += [0, 40, 100, 300, 1200][rowCount];
}
}
function updateScore() {
scoreElement.textContent = `Score: ${player.score}`;
}
function update(time = 0) {
const deltaTime = time - lastTime;
lastTime = time;
dropCounter += deltaTime;
if (dropCounter > dropInterval) {
playerDrop();
}
draw();
if (!gameOver) {
requestId = requestAnimationFrame(update);
}
}
function startGame() {
if (requestId) {
cancelAnimationFrame(requestId);
}
board = createBoard();
player.score = 0;
updateScore();
gameOver = false;
playerReset();
update();
}
document.addEventListener('keydown', event => {
if (gameOver) return;
switch(event.keyCode) {
case 37: // Left arrow
playerMove(-1);
break;
case 39: // Right arrow
playerMove(1);
break;
case 40: // Down arrow
playerDrop();
break;
case 38: // Up arrow
playerRotate(1);
break;
case 32: // Space
playerHardDrop();
break;
}
});
startButton.addEventListener('click', startGame);
});
</script>
</body>
</html>
Would you like me to explain or break down the code?