code a tetris game i can play here
- User
code a tetris game i can play here
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
code a tetris game i can play here
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Here's a playable Tetris game implemented using HTML, CSS, and JavaScript. You can copy and paste this code into an HTML file and open it in your browser to play.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Tetris Game</title>
<style>
body {
background: #000;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
#tetris {
border: 2px solid #fff;
}
</style>
</head>
<body>
<canvas id="tetris" width="300" height="600"></canvas>
<script>
const canvas = document.getElementById('tetris');
const context = canvas.getContext('2d');
context.scale(30, 30); // Scale to make each block 30x30 pixels
// Define shapes and colors
const SHAPES = {
T: [
[0, 1, 0],
[1, 1, 1],
[0, 0, 0],
],
O: [
[2, 2],
[2, 2],
],
L: [
[0, 0, 3],
[3, 3, 3],
[0, 0, 0],
],
J: [
[4, 0, 0],
[4, 4, 4],
[0, 0, 0],
],
I: [
[0, 5, 0, 0],
[0, 5, 0, 0],
[0, 5, 0, 0],
[0, 5, 0, 0],
],
S: [
[0, 6, 6],
[6, 6, 0],
[0, 0, 0],
],
Z: [
[7, 7, 0],
[0, 7, 7],
[0, 0, 0],
],
};
const COLORS = [
'black',
'#FF0D72', // T
'#0DC2FF', // O
'#0DFF72', // L
'#F538FF', // J
'#FF8E0D', // I
'#FFE138', // S
'#3877FF', // Z
];
// Create the grid
function createGrid() {
const grid = [];
for (let y = 0; y < 20; y++) {
grid.push(new Array(10).fill(0));
}
return grid;
}
let grid = createGrid();
// Create a new piece
function createPiece(type) {
return {
shape: SHAPES[type],
x: Math.floor((10 - SHAPES[type][0].length) / 2),
y: 0,
type: type
};
}
// Rotate shape
function rotate(shape) {
const newShape = shape[0].map((_, i) => shape.map(row => row[i]).reverse());
return newShape;
}
// Check collision
function checkCollision(grid, piece, offsetX = 0, offsetY = 0) {
for (let y = 0; y < piece.shape.length; y++) {
for (let x = 0; x < piece.shape[y].length; x++) {
if (piece.shape[y][x]) {
const newX = piece.x + x + offsetX;
const newY = piece.y + y + offsetY;
if (newX < 0 || newX >= 10 || newY >= 20) {
return true;
}
if (newY >= 0 && grid[newY][newX]) {
return true;
}
}
}
}
return false;
}
// Merge piece into grid
function merge(grid, piece) {
piece.shape.forEach((row, y) => {
row.forEach((value, x) => {
if (value) {
grid[piece.y + y][piece.x + x] = value;
}
});
});
}
// Clear full lines
function clearLines() {
let lines = 0;
for (let y = grid.length - 1; y >= 0; y--) {
let filled = true;
for (let x = 0; x < grid[y].length; x++) {
if (!grid[y][x]) {
filled = false;
break;
}
}
if (filled) {
grid.splice(y, 1);
grid.unshift(new Array(10).fill(0));
lines++;
y++;
}
}
}
// Draw grid
function drawGrid() {
for (let y = 0; y < 20; y++) {
for (let x = 0; x < 10; x++) {
if (grid[y][x]) {
context.fillStyle = COLORS[grid[y][x]];
context.fillRect(x, y, 1, 1);
}
}
}
}
// Draw current piece
function drawPiece(piece) {
piece.shape.forEach((row, y) => {
row.forEach((value, x) => {
if (value) {
context.fillStyle = COLORS[value];
context.fillRect(piece.x + x, piece.y + y, 1, 1);
}
});
});
}
// Draw everything
function draw() {
context.fillStyle = '#000';
context.fillRect(0, 0, canvas.width, canvas.height);
drawGrid();
drawPiece(currentPiece);
}
// Game loop
let dropCounter = 0;
let dropInterval = 1000;
let lastTime = 0;
function update(time = 0) {
const deltaTime = time - lastTime;
lastTime = time;
dropCounter += deltaTime;
if (dropCounter > dropInterval) {
if (checkCollision(grid, currentPiece, 0, 1)) {
merge(grid, currentPiece);
clearLines();
const types = Object.keys(SHAPES);
currentPiece = createPiece(types[Math.floor(Math.random() * types.length)]);
if (checkCollision(grid, currentPiece)) {
alert('Game Over!');
grid = createGrid();
currentPiece = createPiece(types[0]); // Reset
}
} else {
currentPiece.y++;
dropCounter = 0;
}
}
draw();
requestAnimationFrame(update);
}
// Controls
document.addEventListener('keydown', event => {
if (event.key === 'ArrowLeft') {
if (!checkCollision(grid, currentPiece, -1, 0)) {
currentPiece.x--;
}
} else if (event.key === 'ArrowRight') {
if (!checkCollision(grid, currentPiece, 1, 0)) {
currentPiece.x++;
}
} else if (event.key === 'ArrowDown') {
if (!checkCollision(grid, currentPiece, 0, 1)) {
currentPiece.y++;
dropCounter = 0;
}
} else if (event.key === 'ArrowUp') {
const rotated = rotate(currentPiece.shape);
// Check if rotated shape collides
const oldShape = currentPiece.shape;
currentPiece.shape = rotated;
if (checkCollision(grid, currentPiece)) {
// Collision, revert rotation
currentPiece.shape = oldShape;
}
}
});
// Start game
let currentPiece = createPiece('T');
update();
</script>
</body>
</html>
Enjoy playing Tetris! 🎮