teris / index.html
Gerfeson's picture
Add 3 files
bb916fa verified
Raw History Blame Contribute Delete
26.9 kB
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tetris Moderno</title>
<script src="https://cdn.tailwindcss.com"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<style>
@import url('https://fonts.googleapis.com/css2?family=Press+Start+2P&display=swap');
.tetris-font {
font-family: 'Press Start 2P', cursive;
}
.game-board {
background: rgba(255, 255, 255, 0.05);
border-radius: 10px;
box-shadow: 0 0 20px rgba(0, 255, 255, 0.2);
border: 2px solid rgba(0, 255, 255, 0.3);
}
.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 10px rgba(0, 240, 240, 0.8); }
.piece-J { background-color: #0000f0; box-shadow: inset 0 0 10px rgba(0, 0, 240, 0.8); }
.piece-L { background-color: #f0a000; box-shadow: inset 0 0 10px rgba(240, 160, 0, 0.8); }
.piece-O { background-color: #f0f000; box-shadow: inset 0 0 10px rgba(240, 240, 0, 0.8); }
.piece-S { background-color: #00f000; box-shadow: inset 0 0 10px rgba(0, 240, 0, 0.8); }
.piece-T { background-color: #a000f0; box-shadow: inset 0 0 10px rgba(160, 0, 240, 0.8); }
.piece-Z { background-color: #f00000; box-shadow: inset 0 0 10px rgba(240, 0, 0, 0.8); }
.glow {
animation: glow 1.5s ease-in-out infinite alternate;
}
@keyframes glow {
from {
box-shadow: 0 0 5px #fff, 0 0 10px #fff, 0 0 15px #00f0f0, 0 0 20px #00f0f0;
}
to {
box-shadow: 0 0 10px #fff, 0 0 20px #fff, 0 0 30px #00f0f0, 0 0 40px #00f0f0;
}
}
.next-piece-container {
background: rgba(0, 0, 0, 0.3);
border-radius: 8px;
padding: 10px;
border: 1px solid rgba(0, 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="max-w-4xl w-full">
<h1 class="text-4xl md:text-5xl font-bold text-center mb-6 tetris-font glow">
<span class="text-cyan-400">T</span>
<span class="text-blue-500">E</span>
<span class="text-yellow-400">T</span>
<span class="text-green-500">R</span>
<span class="text-purple-500">I</span>
<span class="text-red-500">S</span>
</h1>
<div class="flex flex-col md:flex-row items-center justify-center gap-8">
<!-- Game Board -->
<div class="game-board relative">
<canvas id="tetris" width="300" height="600" class="rounded-lg"></canvas>
<!-- Game Over Overlay -->
<div id="gameOverOverlay" class="absolute inset-0 bg-black bg-opacity-70 flex flex-col items-center justify-center hidden rounded-lg">
<h2 class="text-3xl font-bold mb-4 text-red-500 tetris-font">GAME OVER</h2>
<p id="finalScore" class="text-xl mb-6 tetris-font">Score: 0</p>
<button id="restartButton" class="px-6 py-3 bg-cyan-500 hover:bg-cyan-600 rounded-lg font-bold tetris-font transition-all">
PLAY AGAIN
</button>
</div>
<!-- Pause Overlay -->
<div id="pauseOverlay" class="absolute inset-0 bg-black bg-opacity-70 flex flex-col items-center justify-center hidden rounded-lg">
<h2 class="text-3xl font-bold mb-4 text-yellow-400 tetris-font">PAUSED</h2>
<button id="resumeButton" class="px-6 py-3 bg-green-500 hover:bg-green-600 rounded-lg font-bold tetris-font transition-all">
RESUME
</button>
</div>
</div>
<!-- Game Info -->
<div class="flex flex-col gap-6 w-full md:w-64">
<!-- Score -->
<div class="bg-gray-800 p-4 rounded-lg border border-cyan-400">
<h3 class="text-lg font-semibold text-cyan-400 mb-2 tetris-font">SCORE</h3>
<p id="score" class="text-2xl font-bold tetris-font">0</p>
</div>
<!-- Level -->
<div class="bg-gray-800 p-4 rounded-lg border border-purple-500">
<h3 class="text-lg font-semibold text-purple-500 mb-2 tetris-font">LEVEL</h3>
<p id="level" class="text-2xl font-bold tetris-font">1</p>
</div>
<!-- Lines -->
<div class="bg-gray-800 p-4 rounded-lg border border-green-500">
<h3 class="text-lg font-semibold text-green-500 mb-2 tetris-font">LINES</h3>
<p id="lines" class="text-2xl font-bold tetris-font">0</p>
</div>
<!-- Next Piece -->
<div class="bg-gray-800 p-4 rounded-lg border border-yellow-400 next-piece-container">
<h3 class="text-lg font-semibold text-yellow-400 mb-2 tetris-font">NEXT</h3>
<canvas id="nextPiece" width="120" height="120" class="mx-auto"></canvas>
</div>
<!-- Controls -->
<div class="bg-gray-800 p-4 rounded-lg border border-red-500">
<h3 class="text-lg font-semibold text-red-500 mb-3 tetris-font">CONTROLS</h3>
<div class="grid grid-cols-3 gap-2 text-center">
<div class="p-2 bg-gray-700 rounded">
<i class="fas fa-arrow-up"></i>
<p class="text-xs mt-1">Rotate</p>
</div>
<div class="p-2 bg-gray-700 rounded">
<i class="fas fa-arrow-down"></i>
<p class="text-xs mt-1">Speed</p>
</div>
<div class="p-2 bg-gray-700 rounded">
<i class="fas fa-arrow-right"></i>
<p class="text-xs mt-1">Right</p>
</div>
<div class="p-2 bg-gray-700 rounded col-span-3">
<i class="fas fa-arrow-left"></i>
<p class="text-xs mt-1">Left</p>
</div>
<div class="p-2 bg-gray-700 rounded col-span-3">
<i class="fas fa-space-shuttle"></i>
<p class="text-xs mt-1">Hard Drop</p>
</div>
<div class="p-2 bg-gray-700 rounded col-span-3">
<i class="fas fa-pause"></i>
<p class="text-xs mt-1">Pause</p>
</div>
</div>
</div>
</div>
</div>
<!-- Mobile Controls -->
<div class="mt-8 md:hidden">
<div class="grid grid-cols-3 gap-2">
<button id="moveLeft" class="p-4 bg-blue-600 rounded-lg">
<i class="fas fa-arrow-left text-2xl"></i>
</button>
<button id="rotate" class="p-4 bg-purple-600 rounded-lg">
<i class="fas fa-redo text-2xl"></i>
</button>
<button id="moveRight" class="p-4 bg-blue-600 rounded-lg">
<i class="fas fa-arrow-right text-2xl"></i>
</button>
<button id="softDrop" class="p-4 bg-green-600 rounded-lg col-span-2">
<i class="fas fa-arrow-down text-2xl"></i>
</button>
<button id="hardDrop" class="p-4 bg-red-600 rounded-lg">
<i class="fas fa-arrow-circle-down text-2xl"></i>
</button>
</div>
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', () => {
// Game elements
const canvas = document.getElementById('tetris');
const ctx = canvas.getContext('2d');
const nextPieceCanvas = document.getElementById('nextPiece');
const nextPieceCtx = nextPieceCanvas.getContext('2d');
// UI elements
const scoreElement = document.getElementById('score');
const levelElement = document.getElementById('level');
const linesElement = document.getElementById('lines');
const gameOverOverlay = document.getElementById('gameOverOverlay');
const pauseOverlay = document.getElementById('pauseOverlay');
const finalScoreElement = document.getElementById('finalScore');
const restartButton = document.getElementById('restartButton');
const resumeButton = document.getElementById('resumeButton');
// Mobile controls
const moveLeftBtn = document.getElementById('moveLeft');
const moveRightBtn = document.getElementById('moveRight');
const rotateBtn = document.getElementById('rotate');
const softDropBtn = document.getElementById('softDrop');
const hardDropBtn = document.getElementById('hardDrop');
// Game settings
const scale = 30;
const rows = 20;
const cols = 10;
let score = 0;
let level = 1;
let lines = 0;
let gameOver = false;
let isPaused = false;
let dropCounter = 0;
let dropInterval = 1000;
let lastTime = 0;
// Board matrix
const board = Array.from(Array(rows), () => Array(cols).fill(0));
// Pieces
const pieces = [
{ shape: [[0, 0, 0, 0], [1, 1, 1, 1], [0, 0, 0, 0], [0, 0, 0, 0]], className: 'piece-I' }, // I
{ shape: [[2, 0, 0], [2, 2, 2], [0, 0, 0]], className: 'piece-J' }, // J
{ shape: [[0, 0, 3], [3, 3, 3], [0, 0, 0]], className: 'piece-L' }, // L
{ shape: [[4, 4], [4, 4]], className: 'piece-O' }, // O
{ shape: [[0, 5, 5], [5, 5, 0], [0, 0, 0]], className: 'piece-S' }, // S
{ shape: [[0, 6, 0], [6, 6, 6], [0, 0, 0]], className: 'piece-T' }, // T
{ shape: [[7, 7, 0], [0, 7, 7], [0, 0, 0]], className: 'piece-Z' } // Z
];
// Current and next piece
let player = {
pos: {x: 0, y: 0},
matrix: null,
pieceClass: ''
};
let nextPiece = {
matrix: null,
pieceClass: ''
};
// Initialize game
function init() {
// Reset game state
score = 0;
level = 1;
lines = 0;
gameOver = false;
isPaused = false;
dropInterval = 1000;
// Clear board
for (let y = 0; y < rows; y++) {
for (let x = 0; x < cols; x++) {
board[y][x] = 0;
}
}
// Update UI
updateScore();
updateLevel();
updateLines();
// Hide overlays
gameOverOverlay.classList.add('hidden');
pauseOverlay.classList.add('hidden');
// Create first pieces
resetPlayer();
createNextPiece();
// Start game loop
lastTime = 0;
requestAnimationFrame(update);
}
// Game loop
function update(time = 0) {
if (gameOver || isPaused) return;
const deltaTime = time - lastTime;
lastTime = time;
dropCounter += deltaTime;
if (dropCounter > dropInterval) {
playerDrop();
dropCounter = 0;
}
draw();
drawNextPiece();
requestAnimationFrame(update);
}
// Draw game
function draw() {
// Clear canvas
ctx.fillStyle = 'rgba(0, 0, 0, 0.1)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Draw board
drawMatrix(board, {x: 0, y: 0}, 'board');
// Draw player piece
drawMatrix(player.matrix, player.pos, player.pieceClass);
// Draw grid
ctx.strokeStyle = 'rgba(255, 255, 255, 0.05)';
ctx.lineWidth = 0.5;
for (let y = 0; y < rows; y++) {
for (let x = 0; x < cols; x++) {
ctx.strokeRect(x * scale, y * scale, scale, scale);
}
}
}
// Draw next piece preview
function drawNextPiece() {
// Clear canvas
nextPieceCtx.fillStyle = 'rgba(0, 0, 0, 0)';
nextPieceCtx.fillRect(0, 0, nextPieceCanvas.width, nextPieceCanvas.height);
// Center the piece
const offsetX = (nextPieceCanvas.width - nextPiece.matrix[0].length * scale / 2.5) / 2;
const offsetY = (nextPieceCanvas.height - nextPiece.matrix.length * scale / 2.5) / 2;
// Draw next piece
for (let y = 0; y < nextPiece.matrix.length; y++) {
for (let x = 0; x < nextPiece.matrix[y].length; x++) {
if (nextPiece.matrix[y][x] !== 0) {
nextPieceCtx.fillStyle = getComputedStyle(document.documentElement).getPropertyValue(`--color-${nextPiece.pieceClass.split('-')[1].toLowerCase()}`) || '#fff';
nextPieceCtx.fillRect(
offsetX + x * (scale / 2.5),
offsetY + y * (scale / 2.5),
scale / 2.5 - 1,
scale / 2.5 - 1
);
// Add glow effect
nextPieceCtx.shadowColor = getComputedStyle(document.documentElement).getPropertyValue(`--color-${nextPiece.pieceClass.split('-')[1].toLowerCase()}`) || '#fff';
nextPieceCtx.shadowBlur = 10;
}
}
}
}
// Draw matrix (board or piece)
function drawMatrix(matrix, offset, className) {
matrix.forEach((row, y) => {
row.forEach((value, x) => {
if (value !== 0) {
if (className === 'board') {
// For board cells, use the stored class
const pieceClasses = ['', 'piece-I', 'piece-J', 'piece-L', 'piece-O', 'piece-S', 'piece-T', 'piece-Z'];
ctx.fillStyle = getComputedStyle(document.documentElement).getPropertyValue(`--color-${pieceClasses[value].split('-')[1].toLowerCase()}`) || '#fff';
} else {
// For current piece, use its class
ctx.fillStyle = getComputedStyle(document.documentElement).getPropertyValue(`--color-${className.split('-')[1].toLowerCase()}`) || '#fff';
}
ctx.fillRect(
(offset.x + x) * scale,
(offset.y + y) * scale,
scale - 1,
scale - 1
);
// Add inner glow
ctx.shadowColor = ctx.fillStyle;
ctx.shadowBlur = 10;
}
});
});
}
// Merge player piece with 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 player piece
function playerRotate() {
if (gameOver || isPaused) return;
const pos = player.pos.x;
let offset = 1;
rotate(player.matrix);
while (collide()) {
player.pos.x += offset;
offset = -(offset + (offset > 0 ? 1 : -1));
if (offset > player.matrix[0].length) {
rotate(player.matrix);
player.pos.x = pos;
return;
}
}
}
// Rotate matrix
function rotate(matrix) {
const N = matrix.length;
// Transpose matrix
for (let y = 0; y < N; y++) {
for (let x = 0; x < y; x++) {
[matrix[x][y], matrix[y][x]] = [matrix[y][x], matrix[x][y]];
}
}
// Reverse each row
for (let y = 0; y < N; y++) {
matrix[y].reverse();
}
}
// Move player left
function playerMove(dir) {
if (gameOver || isPaused) return;
player.pos.x += dir;
if (collide()) {
player.pos.x -= dir;
}
}
// Move player down (soft drop)
function playerDrop() {
if (gameOver || isPaused) return;
player.pos.y++;
if (collide()) {
player.pos.y--;
merge();
resetPlayer();
arenaSweep();
}
dropCounter = 0;
}
// Hard drop
function playerHardDrop() {
if (gameOver || isPaused) return;
while (!collide()) {
player.pos.y++;
}
player.pos.y--;
merge();
resetPlayer();
arenaSweep();
}
// 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;
}
// Check for 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++;
linesCleared++;
}
if (linesCleared > 0) {
// Update score
const linePoints = [0, 40, 100, 300, 1200];
score += linePoints[linesCleared] * level;
lines += linesCleared;
// Update level (every 10 lines)
level = Math.floor(lines / 10) + 1;
// Increase speed
dropInterval = Math.max(100, 1000 - (level - 1) * 100);
updateScore();
updateLevel();
updateLines();
}
}
// Reset player with next piece
function resetPlayer() {
// Set current piece to next piece
if (nextPiece.matrix) {
player.matrix = nextPiece.matrix;
player.pieceClass = nextPiece.pieceClass;
} else {
// First piece
const randomPiece = pieces[Math.floor(Math.random() * pieces.length)];
player.matrix = randomPiece.shape;
player.pieceClass = randomPiece.className;
}
// Reset position
player.pos.y = 0;
player.pos.x = Math.floor(cols / 2) - Math.floor(player.matrix[0].length / 2);
// Game over if collision at spawn
if (collide()) {
gameOver = true;
finalScoreElement.textContent = `Score: ${score}`;
gameOverOverlay.classList.remove('hidden');
}
// Create new next piece
createNextPiece();
}
// Create next piece
function createNextPiece() {
const randomPiece = pieces[Math.floor(Math.random() * pieces.length)];
nextPiece.matrix = randomPiece.shape;
nextPiece.pieceClass = randomPiece.className;
}
// Update score display
function updateScore() {
scoreElement.textContent = score;
}
// Update level display
function updateLevel() {
levelElement.textContent = level;
}
// Update lines display
function updateLines() {
linesElement.textContent = lines;
}
// Pause game
function togglePause() {
if (gameOver) return;
isPaused = !isPaused;
if (isPaused) {
pauseOverlay.classList.remove('hidden');
} else {
pauseOverlay.classList.add('hidden');
lastTime = performance.now();
dropCounter = 0;
requestAnimationFrame(update);
}
}
// Event listeners
document.addEventListener('keydown', event => {
if (event.key === 'ArrowLeft') {
playerMove(-1);
} else if (event.key === 'ArrowRight') {
playerMove(1);
} else if (event.key === 'ArrowDown') {
playerDrop();
} else if (event.key === 'ArrowUp') {
playerRotate();
} else if (event.key === ' ') {
playerHardDrop();
} else if (event.key === 'p' || event.key === 'P') {
togglePause();
}
});
// Mobile controls
moveLeftBtn.addEventListener('click', () => playerMove(-1));
moveRightBtn.addEventListener('click', () => playerMove(1));
rotateBtn.addEventListener('click', () => playerRotate());
softDropBtn.addEventListener('click', () => playerDrop());
hardDropBtn.addEventListener('click', () => playerHardDrop());
// UI buttons
restartButton.addEventListener('click', init);
resumeButton.addEventListener('click', togglePause);
// Start game
init();
// Add CSS variables for piece colors
document.documentElement.style.setProperty('--color-i', '#00f0f0');
document.documentElement.style.setProperty('--color-j', '#0000f0');
document.documentElement.style.setProperty('--color-l', '#f0a000');
document.documentElement.style.setProperty('--color-o', '#f0f000');
document.documentElement.style.setProperty('--color-s', '#00f000');
document.documentElement.style.setProperty('--color-t', '#a000f0');
document.documentElement.style.setProperty('--color-z', '#f00000');
});
</script>
<p style="border-radius: 8px; text-align: center; font-size: 12px; color: #fff; margin-top: 16px;position: fixed; left: 8px; bottom: 8px; z-index: 10; background: rgba(0, 0, 0, 0.8); padding: 4px 8px;">Made with <img src="https://enzostvs-deepsite.hf.space/logo.svg" alt="DeepSite Logo" style="width: 16px; height: 16px; vertical-align: middle;display:inline-block;margin-right:3px;filter:brightness(0) invert(1);"><a href="https://enzostvs-deepsite.hf.space" style="color: #fff;text-decoration: underline;" target="_blank" >DeepSite</a> - 🧬 <a href="https://enzostvs-deepsite.hf.space?remix=Gerfeson/teris" style="color: #fff;text-decoration: underline;" target="_blank" >Remix</a></p></body>
</html>