projects / index.html
Boody25's picture
Add 2 files
f597ff9 verified
Raw History Blame Contribute Delete
17.7 kB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Snake Game</title>
<script src="https://cdn.tailwindcss.com"></script>
<style>
@keyframes pulse {
0% { transform: scale(1); }
50% { transform: scale(1.05); }
100% { transform: scale(1); }
}
.game-over {
animation: pulse 1.5s infinite;
}
#game-board {
border: 2px solid #4a5568;
box-shadow: 0 10px 25px rgba(0, 0, 0, 0.2);
}
.snake-head {
background-color: #4caf50;
border-radius: 4px;
}
.snake-body {
background-color: #81c784;
border-radius: 3px;
}
.food {
background-color: #f44336;
border-radius: 50%;
animation: pulse 0.8s infinite;
}
</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-green-400">Snake Game</h1>
<div class="flex justify-center gap-8 mb-4">
<div class="text-xl">
Score: <span id="score" class="font-bold text-yellow-300">0</span>
</div>
<div class="text-xl">
High Score: <span id="high-score" class="font-bold text-yellow-300">0</span>
</div>
</div>
</div>
<canvas id="game-board" class="bg-gray-800 rounded-lg"></canvas>
<div class="mt-6 flex flex-col items-center">
<div class="grid grid-cols-3 gap-2 mb-4">
<button id="up-btn" class="bg-gray-700 hover:bg-gray-600 text-white p-4 rounded-lg">
↑
</button>
<div></div>
<div></div>
<button id="left-btn" class="bg-gray-700 hover:bg-gray-600 text-white p-4 rounded-lg">
←
</button>
<button id="down-btn" class="bg-gray-700 hover:bg-gray-600 text-white p-4 rounded-lg">
↓
</button>
<button id="right-btn" class="bg-gray-700 hover:bg-gray-600 text-white p-4 rounded-lg">
→
</button>
</div>
<div class="flex gap-4">
<button id="start-btn" class="bg-green-600 hover:bg-green-500 text-white px-6 py-2 rounded-lg font-bold">
Start Game
</button>
<button id="pause-btn" class="bg-yellow-600 hover:bg-yellow-500 text-white px-6 py-2 rounded-lg font-bold">
Pause
</button>
<button id="reset-btn" class="bg-red-600 hover:bg-red-500 text-white px-6 py-2 rounded-lg font-bold">
Reset
</button>
</div>
</div>
<div id="game-over-modal" class="fixed inset-0 bg-black bg-opacity-70 flex items-center justify-center hidden">
<div class="bg-gray-800 p-8 rounded-lg max-w-md w-full text-center">
<h2 class="text-3xl font-bold text-red-500 mb-4">Game Over!</h2>
<p class="text-xl mb-6">Your score: <span id="final-score" class="font-bold text-yellow-300">0</span></p>
<button id="restart-btn" class="bg-green-600 hover:bg-green-500 text-white px-6 py-2 rounded-lg font-bold">
Play Again
</button>
</div>
</div>
<div class="mt-6 text-gray-400 text-sm">
<p>Use arrow keys or buttons to control the snake</p>
</div>
<script>
document.addEventListener('DOMContentLoaded', () => {
const canvas = document.getElementById('game-board');
const ctx = canvas.getContext('2d');
const scoreElement = document.getElementById('score');
const highScoreElement = document.getElementById('high-score');
const finalScoreElement = document.getElementById('final-score');
const gameOverModal = document.getElementById('game-over-modal');
// Game settings
const gridSize = 20;
const tileCount = 20;
const initialSpeed = 150;
// Game state
let snake = [];
let food = {};
let direction = 'right';
let nextDirection = 'right';
let score = 0;
let highScore = localStorage.getItem('snakeHighScore') || 0;
let gameRunning = false;
let gamePaused = false;
let gameLoop;
// Set canvas size
canvas.width = tileCount * gridSize;
canvas.height = tileCount * gridSize;
// Initialize high score display
highScoreElement.textContent = highScore;
// Initialize game
function initGame() {
// Create initial snake
snake = [
{x: 10, y: 10},
{x: 9, y: 10},
{x: 8, y: 10}
];
// Create first food
createFood();
// Reset score
score = 0;
scoreElement.textContent = score;
// Reset direction
direction = 'right';
nextDirection = 'right';
// Start game loop
if (gameLoop) clearInterval(gameLoop);
gameRunning = true;
gamePaused = false;
gameLoop = setInterval(gameStep, initialSpeed);
// Hide game over modal
gameOverModal.classList.add('hidden');
}
// Create food at random position
function createFood() {
food = {
x: Math.floor(Math.random() * tileCount),
y: Math.floor(Math.random() * tileCount)
};
// Make sure food doesn't appear on snake
for (let i = 0; i < snake.length; i++) {
if (food.x === snake[i].x && food.y === snake[i].y) {
createFood();
return;
}
}
}
// Main game loop
function gameStep() {
if (gamePaused) return;
// Update direction
direction = nextDirection;
// Calculate new head position
const head = {x: snake[0].x, y: snake[0].y};
switch (direction) {
case 'up':
head.y--;
break;
case 'down':
head.y++;
break;
case 'left':
head.x--;
break;
case 'right':
head.x++;
break;
}
// Check for collisions
if (
head.x < 0 || head.x >= tileCount ||
head.y < 0 || head.y >= tileCount ||
checkCollision(head, snake)
) {
gameOver();
return;
}
// Add new head
snake.unshift(head);
// Check if snake ate food
if (head.x === food.x && head.y === food.y) {
// Increase score
score++;
scoreElement.textContent = score;
// Create new food
createFood();
// Increase speed slightly
if (score % 5 === 0) {
clearInterval(gameLoop);
const newSpeed = Math.max(initialSpeed - (score * 2), 50);
gameLoop = setInterval(gameStep, newSpeed);
}
} else {
// Remove tail if no food was eaten
snake.pop();
}
// Draw everything
draw();
}
// Check for collision with snake body
function checkCollision(head, snake) {
for (let i = 0; i < snake.length; i++) {
if (head.x === snake[i].x && head.y === snake[i].y) {
return true;
}
}
return false;
}
// Draw game elements
function draw() {
// Clear canvas
ctx.fillStyle = '#1a202c';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Draw snake
for (let i = 0; i < snake.length; i++) {
if (i === 0) {
// Snake head
ctx.fillStyle = '#4caf50';
ctx.fillRect(
snake[i].x * gridSize,
snake[i].y * gridSize,
gridSize,
gridSize
);
// Eyes
ctx.fillStyle = 'white';
if (direction === 'right' || direction === 'left') {
ctx.fillRect(
snake[i].x * gridSize + (direction === 'right' ? gridSize - 6 : 2),
snake[i].y * gridSize + 4,
4,
4
);
ctx.fillRect(
snake[i].x * gridSize + (direction === 'right' ? gridSize - 6 : 2),
snake[i].y * gridSize + gridSize - 8,
4,
4
);
} else {
ctx.fillRect(
snake[i].x * gridSize + 4,
snake[i].y * gridSize + (direction === 'down' ? gridSize - 6 : 2),
4,
4
);
ctx.fillRect(
snake[i].x * gridSize + gridSize - 8,
snake[i].y * gridSize + (direction === 'down' ? gridSize - 6 : 2),
4,
4
);
}
} else {
// Snake body
ctx.fillStyle = '#81c784';
ctx.fillRect(
snake[i].x * gridSize,
snake[i].y * gridSize,
gridSize,
gridSize
);
}
}
// Draw food
ctx.fillStyle = '#f44336';
ctx.beginPath();
ctx.arc(
food.x * gridSize + gridSize / 2,
food.y * gridSize + gridSize / 2,
gridSize / 2 - 2,
0,
Math.PI * 2
);
ctx.fill();
// Draw grid (optional)
ctx.strokeStyle = '#2d3748';
ctx.lineWidth = 0.5;
for (let i = 0; i < tileCount; i++) {
ctx.beginPath();
ctx.moveTo(i * gridSize, 0);
ctx.lineTo(i * gridSize, canvas.height);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(0, i * gridSize);
ctx.lineTo(canvas.width, i * gridSize);
ctx.stroke();
}
}
// Game over function
function gameOver() {
clearInterval(gameLoop);
gameRunning = false;
// Update high score if needed
if (score > highScore) {
highScore = score;
localStorage.setItem('snakeHighScore', highScore);
highScoreElement.textContent = highScore;
}
// Show game over modal
finalScoreElement.textContent = score;
gameOverModal.classList.remove('hidden');
}
// Event listeners for keyboard controls
document.addEventListener('keydown', (e) => {
// Prevent default for arrow keys to stop page scrolling
if ([37, 38, 39, 40].includes(e.keyCode)) {
e.preventDefault();
}
// Only change direction if game is running and not paused
if (!gameRunning || gamePaused) return;
switch (e.keyCode) {
case 37: // Left
if (direction !== 'right') nextDirection = 'left';
break;
case 38: // Up
if (direction !== 'down') nextDirection = 'up';
break;
case 39: // Right
if (direction !== 'left') nextDirection = 'right';
break;
case 40: // Down
if (direction !== 'up') nextDirection = 'down';
break;
case 32: // Space - pause/unpause
togglePause();
break;
}
});
// Button controls
document.getElementById('up-btn').addEventListener('click', () => {
if (gameRunning && !gamePaused && direction !== 'down') {
nextDirection = 'up';
}
});
document.getElementById('down-btn').addEventListener('click', () => {
if (gameRunning && !gamePaused && direction !== 'up') {
nextDirection = 'down';
}
});
document.getElementById('left-btn').addEventListener('click', () => {
if (gameRunning && !gamePaused && direction !== 'right') {
nextDirection = 'left';
}
});
document.getElementById('right-btn').addEventListener('click', () => {
if (gameRunning && !gamePaused && direction !== 'left') {
nextDirection = 'right';
}
});
// Game control buttons
document.getElementById('start-btn').addEventListener('click', () => {
if (!gameRunning) {
initGame();
}
});
document.getElementById('pause-btn').addEventListener('click', togglePause);
document.getElementById('reset-btn').addEventListener('click', () => {
clearInterval(gameLoop);
gameRunning = false;
initGame();
});
document.getElementById('restart-btn').addEventListener('click', initGame);
// Pause/unpause function
function togglePause() {
if (!gameRunning) return;
gamePaused = !gamePaused;
const pauseBtn = document.getElementById('pause-btn');
if (gamePaused) {
pauseBtn.textContent = 'Resume';
pauseBtn.classList.remove('bg-yellow-600', 'hover:bg-yellow-500');
pauseBtn.classList.add('bg-blue-600', 'hover:bg-blue-500');
} else {
pauseBtn.textContent = 'Pause';
pauseBtn.classList.remove('bg-blue-600', 'hover:bg-blue-500');
pauseBtn.classList.add('bg-yellow-600', 'hover:bg-yellow-500');
}
}
// Initial draw
draw();
});
</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=Boody25/projects" style="color: #fff;text-decoration: underline;" target="_blank" >Remix</a></p></body>
</html>