<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>2048 - Игра</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Arial', sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
color: #333;
}
.game-container {
background: rgba(255, 255, 255, 0.1);
backdrop-filter: blur(10px);
border-radius: 20px;
padding: 30px;
box-shadow: 0 8px 32px rgba(31, 38, 135, 0.37);
border: 1px solid rgba(255, 255, 255, 0.18);
}
h1 {
color: white;
font-size: 48px;
margin-bottom: 20px;
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
}
.score-container {
display: flex;
justify-content: space-between;
margin-bottom: 20px;
color: white;
font-size: 20px;
}
.score-box {
background: rgba(255, 255, 255, 0.2);
padding: 10px 20px;
border-radius: 10px;
backdrop-filter: blur(5px);
}
.game-board {
background: rgba(255, 255, 255, 0.2);
border-radius: 10px;
padding: 10px;
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 10px;
width: 400px;
height: 400px;
}
.tile {
background: rgba(255, 255, 255, 0.3);
border-radius: 10px;
display: flex;
align-items: center;
justify-content: center;
font-size: 28px;
font-weight: bold;
transition: all 0.15s ease-in-out;
color: #776e65;
position: relative;
overflow: hidden;
}
.tile::before {
content: '';
position: absolute;
top: -50%;
left: -50%;
width: 200%;
height: 200%;
background: linear-gradient(45deg, transparent, rgba(255, 255, 255, 0.3), transparent);
transform: rotate(45deg);
transition: all 0.5s;
opacity: 0;
}
.tile:hover::before {
animation: shine 0.5s ease-in-out;
}
@keyframes shine {
0% {
transform: translateX(-100%) translateY(-100%) rotate(45deg);
opacity: 0;
}
50% {
opacity: 1;
}
100% {
transform: translateX(100%) translateY(100%) rotate(45deg);
opacity: 0;
}
}
.tile-2 { background: #eee4da; color: #776e65; }
.tile-4 { background: #ede0c8; color: #776e65; }
.tile-8 { background: #f2b179; color: #f9f6f2; }
.tile-16 { background: #f59563; color: #f9f6f2; }
.tile-32 { background: #f67c5f; color: #f9f6f2; }
.tile-64 { background: #f65e3b; color: #f9f6f2; }
.tile-128 { background: #edcf72; color: #f9f6f2; font-size: 24px; }
.tile-256 { background: #edcc61; color: #f9f6f2; font-size: 24px; }
.tile-512 { background: #edc850; color: #f9f6f2; font-size: 24px; }
.tile-1024 { background: #edc53f; color: #f9f6f2; font-size: 20px; }
.tile-2048 { background: #edc22e; color: #f9f6f2; font-size: 20px; animation: pulse 2s infinite; }
@keyframes pulse {
0% { transform: scale(1); }
50% { transform: scale(1.05); }
100% { transform: scale(1); }
}
.new-tile {
animation: appear 0.2s ease-in-out;
}
@keyframes appear {
0% {
transform: scale(0);
opacity: 0;
}
100% {
transform: scale(1);
opacity: 1;
}
}
.merged {
animation: merge 0.15s ease-in-out;
}
@keyframes merge {
0% { transform: scale(1); }
50% { transform: scale(1.2); }
100% { transform: scale(1); }
}
.controls {
margin-top: 20px;
text-align: center;
color: white;
}
button {
background: rgba(255, 255, 255, 0.2);
border: none;
color: white;
padding: 10px 20px;
font-size: 16px;
border-radius: 10px;
cursor: pointer;
transition: all 0.3s;
backdrop-filter: blur(5px);
margin: 0 10px;
}
button:hover {
background: rgba(255, 255, 255, 0.3);
transform: translateY(-2px);
}
.game-over {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.8);
display: none;
align-items: center;
justify-content: center;
z-index: 1000;
}
.game-over-content {
background: white;
padding: 40px;
border-radius: 20px;
text-align: center;
box-shadow: 0 0 30px rgba(0, 0, 0, 0.5);
}
.game-over h2 {
font-size: 36px;
margin-bottom: 20px;
color: #776e65;
}
.instructions {
color: white;
margin-top: 20px;
text-align: center;
opacity: 0.8;
}
</style>
</head>
<body>
<div class="game-container">
<h1>2048</h1>
<div class="score-container">
<div class="score-box">
<span>Счет: </span>
<span id="score">0</span>
</div>
<div class="score-box">
<span>Рекорд: </span>
<span id="best">0</span>
</div>
</div>
<div class="game-board" id="game-board"></div>
<div class="controls">
<button onclick="newGame()">Новая игра</button>
</div>
</div>
<div class="game-over" id="game-over">
<div class="game-over-content">
<h2 id="game-over-text">Игра окончена!</h2>
<button onclick="newGame()">Играть снова</button>
</div>
</div>
<div class="instructions">
<p>Используйте стрелки для перемещения плиток</p>
</div>
<script>
let board = [];
let score = 0;
let best = localStorage.getItem('bestScore') || 0;
const size = 4;
document.getElementById('best').textContent = best;
function initBoard() {
board = Array(size).fill().map(() => Array(size).fill(0));
score = 0;
updateScore();
addNewTile();
addNewTile();
updateDisplay();
}
function addNewTile() {
const emptyCells = [];
for (let i = 0; i < size; i++) {
for (let j = 0; j < size; j++) {
if (board[i][j] === 0) {
emptyCells.push({x: i, y: j});
}
}
}
if (emptyCells.length > 0) {
const randomCell = emptyCells[Math.floor(Math.random() * emptyCells.length)];
board[randomCell.x][randomCell.y] = Math.random() < 0.9 ? 2 : 4;
}
}
function updateDisplay() {
const gameBoard = document.getElementById('game-board');
gameBoard.innerHTML = '';
for (let i = 0; i < size; i++) {
for (let j = 0; j < size; j++) {
const tile = document.createElement('div');
tile.className = 'tile';
if (board[i][j] !== 0) {
tile.textContent = board[i][j];
tile.classList.add(`tile-${board[i][j]}`);
}
gameBoard.appendChild(tile);
}
}
}
function move(direction) {
let moved = false;
const newBoard = board.map(row => [...row]);
function slide(row) {
const filtered = row.filter(val => val !== 0);
const merged = [];
for (let i = 0; i < filtered.length; i++) {
if (i < filtered.length - 1 && filtered[i] === filtered[i + 1]) {
merged.push(filtered[i] * 2);
score += filtered[i] * 2;
i++;
} else {
merged.push(filtered[i]);
}
}
while (merged.length < size) {
merged.push(0);
}
return merged;
}
if (direction === 'left') {
for (let i = 0; i < size; i++) {
const newRow = slide(board[i]);
if (newRow.some((val, idx) => val !== board[i][idx])) {
moved = true;
}
newBoard[i] = newRow;
}
} else if (direction === 'right') {
for (let i = 0; i < size; i++) {
const newRow = slide(board[i].slice().reverse()).reverse();
if (newRow.some((val, idx) => val !== board[i][idx])) {
moved = true;
}
newBoard[i] = newRow;
}
} else if (direction === 'up') {
for (let j = 0; j < size; j++) {
const column = [];
for (let i = 0; i < size; i++) {
column.push(board[i][j]);
}
const newColumn = slide(column);
if (newColumn.some((val, idx) => val !== board[idx][j])) {
moved = true;
}
for (let i = 0; i < size; i++) {
newBoard[i][j] = newColumn[i];
}
}
} else if (direction === 'down') {
for (let j = 0; j < size; j++) {
const column = [];
for (let i = 0; i < size; i++) {
column.push(board[i][j]);
}
const newColumn = slide(column.reverse()).reverse();
if (newColumn.some((val, idx) => val !== board[idx][j])) {
moved = true;
}
for (let i = 0; i < size; i++) {
newBoard[i][j] = newColumn[i];
}
}
}
if (moved) {
board = newBoard;
addNewTile();
updateScore();
updateDisplay();
if (isGameOver()) {
showGameOver();
}
}
}
function updateScore() {
document.getElementById('score').textContent = score;
if (score > best) {
best = score;
localStorage.setItem('bestScore', best);
document.getElementById('best').textContent = best;
}
}
function isGameOver() {
// Проверка на 2048
for (let i = 0; i < size; i++) {
for (let j = 0; j < size; j++) {
if (board[i][j] === 2048) {
return 'win';
}
}
}
// Проверка на возможность хода
for (let i = 0; i < size; i++) {
for (let j = 0; j < size; j++) {
if (board[i][j] === 0) {
return false;
}
if (i < size - 1 && board[i][j] === board[i + 1][j]) {
return false;
}
if (j < size - 1 && board[i][j] === board[i][j + 1]) {
return false;
}
}
}
return 'lose';
}
function showGameOver() {
const gameOver = document.getElementById('game-over');
const text = document.getElementById('game-over-text');
const result = isGameOver();
if (result === 'win') {
text.textContent = 'Поздравляем! Вы победили!';
} else {
text.textContent = 'Игра окончена!';
}
gameOver.style.display = 'flex';
}
function newGame() {
document.getElementById('game-over').style.display = 'none';
initBoard();
}
document.addEventListener('keydown', (e) => {
switch(e.key) {
case 'ArrowLeft':
e.preventDefault();
move('left');
break;
case 'ArrowRight':
e.preventDefault();
move('right');
break;
case 'ArrowUp':
e.preventDefault();
move('up');
break;
case 'ArrowDown':
e.preventDefault();
move('down');
break;
}
});
// Инициализация игры
initBoard();
</script>
</body>
</html>