Spaces:
Running

tetris / index.html
Jpaper's picture
the levels dont get harder when i alter them? - Initial Deployment
95e7d0e verified
Raw History Blame Contribute Delete
18.2 kB
<!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>
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://cdn.jsdelivr.net/npm/feather-icons/dist/feather.min.js"></script>
<style>
#game-board {
border: 2px solid #4b5563;
background-color: #1f2937;
}
.cell {
width: 30px;
height: 30px;
border: 1px solid rgba(255, 255, 255, 0.05);
box-sizing: border-box;
}
.piece-I {
background-color: #00f0f0;
box-shadow: inset 0 0 0 1px rgba(255,255,255,0.3),
inset 0 0 0 2px rgba(0,0,0,0.2);
}
.piece-J {
background-color: #0000f0;
box-shadow: inset 0 0 0 1px rgba(255,255,255,0.3),
inset 0 0 0 2px rgba(0,0,0,0.2);
}
.piece-L {
background-color: #f0a000;
box-shadow: inset 0 0 0 1px rgba(255,255,255,0.3),
inset 0 0 0 2px rgba(0,0,0,0.2);
}
.piece-O {
background-color: #f0f000;
box-shadow: inset 0 0 0 1px rgba(255,255,255,0.3),
inset 0 0 0 2px rgba(0,0,0,0.2);
}
.piece-S {
background-color: #00f000;
box-shadow: inset 0 0 0 1px rgba(255,255,255,0.3),
inset 0 0 0 2px rgba(0,0,0,0.2);
}
.piece-T {
background-color: #a000f0;
box-shadow: inset 0 0 0 1px rgba(255,255,255,0.3),
inset 0 0 0 2px rgba(0,0,0,0.2);
}
.piece-Z {
background-color: #f00000;
box-shadow: inset 0 0 0 1px rgba(255,255,255,0.3),
inset 0 0 0 2px rgba(0,0,0,0.2);
}
.ghost {
opacity: 0.3;
box-shadow: inset 0 0 0 1px rgba(255,255,255,0.2);
}
</style>
</head>
<body class="bg-gray-900 text-white min-h-screen flex flex-col items-center justify-center p-4">
<div class="text-center mb-6">
<h1 class="text-4xl font-bold mb-2 text-purple-400">TETRIS</h1>
<p class="text-gray-300">Use arrow keys to move, space to drop, and up arrow to rotate</p>
</div>
<div class="flex flex-col md:flex-row gap-8 items-center justify-center">
<div class="relative">
<canvas id="game-board" width="300" height="600"></canvas>
<div id="game-over" class="absolute inset-0 bg-black bg-opacity-70 hidden flex-col items-center justify-center">
<h2 class="text-3xl font-bold text-red-500 mb-4">GAME OVER</h2>
<button id="restart-btn" class="px-6 py-2 bg-purple-600 rounded-md hover:bg-purple-700 transition">Play Again</button>
</div>
</div>
<div class="bg-gray-800 p-6 rounded-lg shadow-lg w-full max-w-xs">
<div class="mb-6">
<h2 class="text-xl font-semibold mb-2 text-purple-300">Score</h2>
<div class="bg-gray-700 p-4 rounded text-2xl font-bold" id="score">0</div>
</div>
<div class="mb-6">
<h2 class="text-xl font-semibold mb-2 text-purple-300">Level</h2>
<div class="bg-gray-700 p-4 rounded text-2xl font-bold" id="level">1</div>
</div>
<div class="mb-6">
<h2 class="text-xl font-semibold mb-2 text-purple-300">Lines</h2>
<div class="bg-gray-700 p-4 rounded text-2xl font-bold" id="lines">0</div>
</div>
<div class="mb-6">
<h2 class="text-xl font-semibold mb-2 text-purple-300">Difficulty</h2>
<select id="difficulty" class="w-full bg-gray-700 p-2 rounded text-white">
<option value="1">Easy</option>
<option value="2" selected>Medium</option>
<option value="3">Hard</option>
<option value="4">Expert</option>
</select>
</div>
<div class="mb-4">
<h2 class="text-xl font-semibold mb-2 text-purple-300">Next Piece</h2>
<canvas id="next-piece" width="120" height="120" class="bg-gray-700 rounded"></canvas>
</div>
<button id="start-btn" class="w-full py-3 bg-green-600 rounded-md hover:bg-green-700 transition font-bold">
Start Game
</button>
<button id="pause-btn" class="w-full py-3 bg-yellow-600 rounded-md hover:bg-yellow-700 transition font-bold mt-2 hidden">
Pause
</button>
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', () => {
feather.replace();
// Game constants
const COLS = 10;
const ROWS = 20;
const BLOCK_SIZE = 30;
const COLORS = [
null,
'#00f0f0', // I
'#0000f0', // J
'#f0a000', // L
'#f0f000', // O
'#00f000', // S
'#a000f0', // T
'#f00000' // Z
];
// Game variables
let canvas = document.getElementById('game-board');
let ctx = canvas.getContext('2d');
let nextPieceCanvas = document.getElementById('next-piece');
let nextPieceCtx = nextPieceCanvas.getContext('2d');
let score = 0;
let lines = 0;
let level = 1;
let gameOver = false;
let paused = false;
let dropInterval;
let dropSpeed = 1000; // Initial speed in ms
let board = createMatrix(COLS, ROWS);
let piece = null;
let nextPiece = null;
let dropStart = Date.now();
// Scale canvas
ctx.scale(BLOCK_SIZE, BLOCK_SIZE);
nextPieceCtx.scale(BLOCK_SIZE, BLOCK_SIZE);
// Game pieces
const PIECES = [
{ shape: [[0, 0, 0, 0], [1, 1, 1, 1], [0, 0, 0, 0], [0, 0, 0, 0]], color: 1 }, // I
{ shape: [[2, 0, 0], [2, 2, 2], [0, 0, 0]], color: 2 }, // J
{ shape: [[0, 0, 3], [3, 3, 3], [0, 0, 0]], color: 3 }, // L
{ shape: [[0, 4, 4], [0, 4, 4], [0, 0, 0]], color: 4 }, // O
{ shape: [[0, 5, 5], [5, 5, 0], [0, 0, 0]], color: 5 }, // S
{ shape: [[0, 6, 0], [6, 6, 6], [0, 0, 0]], color: 6 }, // T
{ shape: [[7, 7, 0], [0, 7, 7], [0, 0, 0]], color: 7 } // Z
];
// Initialize game
function init() {
board = createMatrix(COLS, ROWS);
score = 0;
lines = 0;
level = 1;
gameOver = false;
updateScore();
nextPiece = randomPiece();
resetPiece();
draw();
}
// Create matrix
function createMatrix(w, h) {
const matrix = [];
while (h--) {
matrix.push(new Array(w).fill(0));
}
return matrix;
}
// Random piece
function randomPiece() {
const rand = Math.floor(Math.random() * PIECES.length);
return PIECES[rand];
}
// Reset current piece
function resetPiece() {
piece = JSON.parse(JSON.stringify(nextPiece));
nextPiece = randomPiece();
drawNextPiece();
piece.pos = {x: Math.floor(COLS / 2) - Math.floor(piece.shape[0].length / 2), y: 0};
if (collide()) {
gameOver = true;
document.getElementById('game-over').classList.remove('hidden');
}
}
// Draw the game
function draw() {
// Clear board
ctx.fillStyle = '#1f2937';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Draw board
drawMatrix(board, {x: 0, y: 0});
// Draw current piece
drawMatrix(piece.shape, piece.pos);
// Draw ghost piece
if (!gameOver && !paused) {
const ghostPos = {x: piece.pos.x, y: piece.pos.y};
while (!collide(ghostPos.x, ghostPos.y + 1)) {
ghostPos.y++;
}
ctx.globalAlpha = 0.3;
drawMatrix(piece.shape, ghostPos);
ctx.globalAlpha = 1.0;
}
}
// Draw next piece
function drawNextPiece() {
nextPieceCtx.fillStyle = '#1f2937';
nextPieceCtx.fillRect(0, 0, nextPieceCanvas.width, nextPieceCanvas.height);
const centerX = (nextPieceCanvas.width / BLOCK_SIZE - nextPiece.shape[0].length) / 2;
const centerY = (nextPieceCanvas.height / BLOCK_SIZE - nextPiece.shape.length) / 2;
drawMatrix(nextPiece.shape, {x: centerX, y: centerY}, nextPieceCtx);
}
// Draw matrix
function drawMatrix(matrix, offset, context = ctx) {
matrix.forEach((row, y) => {
row.forEach((value, x) => {
if (value !== 0) {
// Main block
context.fillStyle = COLORS[value];
context.fillRect(x + offset.x, y + offset.y, 1, 1);
// Inner highlight
context.strokeStyle = 'rgba(255, 255, 255, 0.4)';
context.lineWidth = 0.1;
context.strokeRect(x + offset.x + 0.05, y + offset.y + 0.05, 0.9, 0.9);
// Outer shadow
context.strokeStyle = 'rgba(0, 0, 0, 0.4)';
context.lineWidth = 0.15;
context.strokeRect(x + offset.x, y + offset.y, 1, 1);
// Corner highlights
context.fillStyle = 'rgba(255, 255, 255, 0.2)';
context.fillRect(x + offset.x + 0.7, y + offset.y + 0.1, 0.2, 0.2);
context.fillRect(x + offset.x + 0.1, y + offset.y + 0.7, 0.2, 0.2);
}
});
});
}
// Collision detection
function collide(offsetX = piece.pos.x, offsetY = piece.pos.y, pieceShape = piece.shape) {
for (let y = 0; y < pieceShape.length; y++) {
for (let x = 0; x < pieceShape[y].length; x++) {
if (pieceShape[y][x] !== 0 &&
(board[y + offsetY] === undefined ||
board[y + offsetY][x + offsetX] === undefined ||
board[y + offsetY][x + offsetX] !== 0)) {
return true;
}
}
}
return false;
}
// Merge piece to board
function merge() {
piece.shape.forEach((row, y) => {
row.forEach((value, x) => {
if (value !== 0) {
board[y + piece.pos.y][x + piece.pos.x] = value;
}
});
});
}
// Rotate piece
function rotate() {
const rotated = [];
for (let y = 0; y < piece.shape[0].length; y++) {
rotated[y] = [];
for (let x = 0; x < piece.shape.length; x++) {
rotated[y][x] = piece.shape[piece.shape.length - 1 - x][y];
}
}
const originalShape = piece.shape;
piece.shape = rotated;
if (collide()) {
piece.shape = originalShape;
}
}
// Clear lines
function clearLines() {
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
linesCleared++;
}
if (linesCleared > 0) {
// Update score based on lines cleared
const points = [0, 100, 300, 500, 800]; // Points for 0, 1, 2, 3, 4 lines
score += points[linesCleared] * level;
lines += linesCleared;
// Level up every 10 lines
level = Math.floor(lines / 10) + 1;
// Increase speed based on level and difficulty
const difficulty = parseInt(document.getElementById('difficulty').value);
dropSpeed = Math.max(100, 1000 - (level * 100 * difficulty));
// Reset drop timer when level changes
dropStart = Date.now();
updateScore();
}
}
// Update score display
function updateScore() {
document.getElementById('score').textContent = score;
document.getElementById('level').textContent = level;
document.getElementById('lines').textContent = lines;
}
// Drop the piece
function drop() {
const now = Date.now();
const delta = now - dropStart;
if (delta > dropSpeed) {
piece.pos.y++;
if (collide()) {
piece.pos.y--;
merge();
clearLines();
resetPiece();
}
dropStart = now;
}
if (!gameOver && !paused) {
draw();
requestAnimationFrame(drop);
}
}
// Game controls
document.addEventListener('keydown', event => {
if (gameOver || paused) return;
switch (event.keyCode) {
case 37: // Left
piece.pos.x--;
if (collide()) piece.pos.x++;
break;
case 39: // Right
piece.pos.x++;
if (collide()) piece.pos.x--;
break;
case 40: // Down
piece.pos.y++;
if (collide()) {
piece.pos.y--;
merge();
clearLines();
resetPiece();
}
break;
case 38: // Up
rotate();
break;
case 32: // Space
while (!collide(piece.pos.x, piece.pos.y + 1)) {
piece.pos.y++;
}
merge();
clearLines();
resetPiece();
break;
case 80: // P
togglePause();
break;
}
draw();
});
// Start game
document.getElementById('start-btn').addEventListener('click', () => {
document.getElementById('game-over').classList.add('hidden');
document.getElementById('start-btn').classList.add('hidden');
document.getElementById('pause-btn').classList.remove('hidden');
init();
drop();
});
// Restart game
document.getElementById('restart-btn').addEventListener('click', () => {
document.getElementById('game-over').classList.add('hidden');
init();
drop();
});
// Pause game
document.getElementById('pause-btn').addEventListener('click', togglePause);
function togglePause() {
paused = !paused;
document.getElementById('pause-btn').textContent = paused ? 'Resume' : 'Pause';
if (!paused && !gameOver) {
dropStart = Date.now();
drop();
}
}
// Difficulty change
document.getElementById('difficulty').addEventListener('change', () => {
const difficulty = parseInt(document.getElementById('difficulty').value);
dropSpeed = Math.max(100, 1000 - (level * 100 * difficulty));
// Reset drop timer when changing difficulty
dropStart = Date.now();
});
// Initial setup
init();
draw();
drawNextPiece();
});
</script>
</body>
</html>