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 complete Tetris game implementation that you can run directly in your browser. Copy this code into an HTML file and open it in any modern browser.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tetris Game</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
background-color: #f0f0f0;
margin: 0;
padding: 20px;
}
#game-container {
display: flex;
justify-content: center;
gap: 20px;
margin-top: 20px;
}
#tetris {
border: 2px solid #333;
background-color: #111;
}
#side-panel {
display: flex;
flex-direction: column;
gap: 20px;
}
#next-piece {
border: 2px solid #333;
background-color: #111;
}
#score-panel {
background-color: #222;
color: white;
padding: 10px;
border-radius: 5px;
}
h1 {
color: #333;
}
button {
padding: 10px 20px;
font-size: 16px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
margin: 5px;
}
button:hover {
background-color: #45a049;
}
.controls {
margin-top: 20px;
}
</style>
</head>
<body>
<h1>Tetris Game</h1>
<div id="game-container">
<canvas id="tetris" width="300" height="600"></canvas>
<div id="side-panel">
<canvas id="next-piece" width="120" height="120"></canvas>
<div id="score-panel">
<h3>Score: <span id="score">0</span></h3>
<h3>Level: <span id="level">1</span></h3>
<h3>Lines: <span id="lines">0</span></h3>
</div>
</div>
</div>
<div class="controls">
<button id="start-btn">Start Game</button>
<button id="pause-btn">Pause</button>
<p>Controls: Arrow keys to move, Up to rotate, Space to drop</p>
</div>
<script>
// Game constants
const COLS = 10;
const ROWS = 20;
const BLOCK_SIZE = 30;
const COLORS = [
null,
'#FF0D72', // I
'#0DC2FF', // J
'#0DFF72', // L
'#F538FF', // O
'#FF8E0D', // S
'#FFE138', // T
'#3877FF' // Z
];
// Tetromino shapes
const SHAPES = [
null,
[[0, 0, 0, 0], [1, 1, 1, 1], [0, 0, 0, 0], [0, 0, 0, 0]], // I
[[2, 0, 0], [2, 2, 2], [0, 0, 0]], // J
[[0, 0, 3], [3, 3, 3], [0, 0, 0]], // L
[[0, 4, 4], [0, 4, 4], [0, 0, 0]], // O
[[0, 5, 5], [5, 5, 0], [0, 0, 0]], // S
[[0, 6, 0], [6, 6, 6], [0, 0, 0]], // T
[[7, 7, 0], [0, 7, 7], [0, 0, 0]] // Z
];
// Game variables
let canvas = document.getElementById('tetris');
let ctx = canvas.getContext('2d');
let nextCanvas = document.getElementById('next-piece');
let nextCtx = nextCanvas.getContext('2d');
let scoreElement = document.getElementById('score');
let levelElement = document.getElementById('level');
let linesElement = document.getElementById('lines');
let startBtn = document.getElementById('start-btn');
let pauseBtn = document.getElementById('pause-btn');
// Scale the canvases
ctx.scale(BLOCK_SIZE, BLOCK_SIZE);
nextCtx.scale(BLOCK_SIZE, BLOCK_SIZE);
// Game state
let board = createMatrix(COLS, ROWS);
let player = {
pos: {x: 0, y: 0},
matrix: null,
score: 0,
level: 1,
lines: 0,
dropCounter: 0,
dropInterval: 1000,
gameOver: false,
paused: false
};
// Initialize the game
function init() {
board = createMatrix(COLS, ROWS);
player.score = 0;
player.level = 1;
player.lines = 0;
player.dropInterval = 1000;
player.gameOver = false;
player.paused = false;
updateScore();
resetPlayer();
update();
}
// Create a matrix (2D array)
function createMatrix(w, h) {
const matrix = [];
while (h--) {
matrix.push(new Array(w).fill(0));
}
return matrix;
}
// Create a piece
function createPiece(type) {
return SHAPES[type];
}
// Draw the matrix
function drawMatrix(matrix, offset) {
matrix.forEach((row, y) => {
row.forEach((value, x) => {
if (value !== 0) {
ctx.fillStyle = COLORS[value];
ctx.fillRect(x + offset.x, y + offset.y, 1, 1);
ctx.strokeStyle = '#000';
ctx.strokeRect(x + offset.x, y + offset.y, 1, 1);
}
});
});
}
// Draw the next piece
function drawNextPiece() {
nextCtx.clearRect(0, 0, nextCanvas.width, nextCanvas.height);
if (player.next) {
const offset = {x: 1, y: 1};
drawPiece(player.next, offset, nextCtx);
}
}
// Draw a piece
function drawPiece(piece, offset, context) {
piece.forEach((row, y) => {
row.forEach((value, x) => {
if (value !== 0) {
context.fillStyle = COLORS[value];
context.fillRect(x + offset.x, y + offset.y, 1, 1);
context.strokeStyle = '#000';
context.strokeRect(x + offset.x, y + offset.y, 1, 1);
}
});
});
}
// Draw the game
function draw() {
// Clear the canvas
ctx.fillStyle = '#111';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Draw the board
drawMatrix(board, {x: 0, y: 0});
// Draw the current piece
if (player.matrix) {
drawPiece(player.matrix, player.pos, ctx);
}
}
// Merge the piece with the board
function merge() {
player.matrix.forEach((row, y) => {
row.forEach((value, x) => {
if (value !== 0) {
board[y + player.pos.y][x + player.pos.x] = value;
}
});
});
}
// Rotate the piece
function rotate(matrix) {
const N = matrix.length;
const result = createMatrix(N, N);
for (let y = 0; y < N; y++) {
for (let x = 0; x < N; x++) {
result[x][N - 1 - y] = matrix[y][x];
}
}
return result;
}
// Check for collisions
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] === undefined ||
board[y + o.y][x + o.x] === undefined ||
board[y + o.y][x + o.x] !== 0)) {
return true;
}
}
}
return false;
}
// Move the player
function playerMove(dir) {
if (player.paused || player.gameOver) return;
player.pos.x += dir;
if (collide()) {
player.pos.x -= dir;
}
}
// Rotate the player's piece
function playerRotate() {
if (player.paused || player.gameOver) return;
const pos = player.pos.x;
let offset = 1;
rotatePiece();
while (collide()) {
player.pos.x += offset;
offset = -(offset + (offset > 0 ? 1 : -1));
if (offset > player.matrix[0].length) {
rotatePiece();
player.pos.x = pos;
return;
}
}
}
// Rotate the piece
function rotatePiece() {
const rotated = rotate(player.matrix);
player.matrix = rotated;
}
// Drop the piece
function playerDrop() {
if (player.paused || player.gameOver) return;
player.pos.y++;
if (collide()) {
player.pos.y--;
merge();
resetPlayer();
arenaSweep();
updateScore();
}
player.dropCounter = 0;
}
// Hard drop
function playerHardDrop() {
if (player.paused || player.gameOver) return;
while (!collide()) {
player.pos.y++;
}
player.pos.y--;
merge();
resetPlayer();
arenaSweep();
updateScore();
}
// Reset the player
function resetPlayer() {
const pieces = 'IJLOSTZ';
player.matrix = createPiece(pieces.charCodeAt(Math.floor(Math.random() * pieces.length)) - 64);
player.pos.y = 0;
player.pos.x = Math.floor(COLS / 2) - Math.floor(player.matrix[0].length / 2);
// Generate next piece
player.next = createPiece(pieces.charCodeAt(Math.floor(Math.random() * pieces.length)) - 64);
drawNextPiece();
if (collide()) {
board.forEach(row => row.fill(0));
player.gameOver = true;
alert('Game Over! Your score: ' + player.score);
}
}
// Sweep completed lines
function arenaSweep() {
let linesCleared = 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;
}
}
// Remove the line
const row = board.splice(y, 1)[0].fill(0);
board.unshift(row);
y++; // Check the same row again (now with new content)
linesCleared++;
}
if (linesCleared > 0) {
// Update score based on lines cleared
switch (linesCleared) {
case 1:
player.score += 100 * player.level;
break;
case 2:
player.score += 300 * player.level;
break;
case 3:
player.score += 500 * player.level;
break;
case 4:
player.score += 800 * player.level;
break;
}
player.lines += linesCleared;
// Level up every 10 lines
player.level = Math.floor(player.lines / 10) + 1;
// Increase speed (reduce drop interval)
player.dropInterval = Math.max(100, 1000 - (player.level - 1) * 100);
}
}
// Update the score display
function updateScore() {
scoreElement.textContent = player.score;
levelElement.textContent = player.level;
linesElement.textContent = player.lines;
}
// Game loop
function update(time = 0) {
if (player.gameOver) return;
if (!player.paused) {
player.dropCounter += time;
if (player.dropCounter > player.dropInterval) {
playerDrop();
}
}
draw();
requestAnimationFrame(update);
}
// Event listeners
document.addEventListener('keydown', event => {
if (player.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();
break;
case 32: // Space
playerHardDrop();
break;
case 80: // P
togglePause();
break;
}
});
startBtn.addEventListener('click', () => {
init();
});
pauseBtn.addEventListener('click', togglePause);
function togglePause() {
player.paused = !player.paused;
pauseBtn.textContent = player.paused ? 'Resume' : 'Pause';
}
// Initialize the game
init();
</script>
</body>
</html>
.html extension (e.g., tetris.html)Enjoy the game!