cobrinha-ehe / index.html
ryck03's picture
Add 2 files
88056ec verified
Raw History Blame Contribute Delete
20.6 kB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Wraparound Snake Game</title>
<script src="https://cdn.tailwindcss.com"></script>
<style>
body {
background-color: #1a202c;
overflow: hidden;
font-family: 'Arial', sans-serif;
}
#game-container {
position: relative;
width: 100vw;
height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
#game-board {
border: 8px solid #4a5568;
border-radius: 8px;
box-shadow: 0 0 30px rgba(74, 85, 104, 0.5);
background-color: #2d3748;
}
.snake-segment {
position: absolute;
border-radius: 50%;
transition: all 0.1s ease;
box-shadow: 0 0 8px rgba(0, 0, 0, 0.3);
}
.snake-head {
background: linear-gradient(135deg, #48bb78, #38a169);
border: 2px solid #2f855a;
z-index: 10;
}
.snake-body {
background: linear-gradient(135deg, #68d391, #48bb78);
border: 2px solid #38a169;
}
.snake-tail {
background: linear-gradient(135deg, #9ae6b4, #68d391);
border: 2px solid #48bb78;
}
.food {
position: absolute;
border-radius: 50%;
background: linear-gradient(135deg, #f56565, #e53e3e);
border: 2px solid #c53030;
box-shadow: 0 0 10px rgba(245, 101, 101, 0.5);
animation: pulse 1.5s infinite alternate;
}
@keyframes pulse {
from { transform: scale(1); }
to { transform: scale(1.1); }
}
.eye {
position: absolute;
width: 6px;
height: 6px;
background-color: white;
border-radius: 50%;
border: 1px solid #2d3748;
}
.pupil {
position: absolute;
width: 3px;
height: 3px;
background-color: #2d3748;
border-radius: 50%;
}
#score-display {
position: absolute;
top: 20px;
left: 50%;
transform: translateX(-50%);
background-color: rgba(45, 55, 72, 0.8);
color: white;
padding: 8px 16px;
border-radius: 20px;
font-size: 1.2rem;
font-weight: bold;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
#game-over {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.7);
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
color: white;
z-index: 100;
display: none;
}
#start-screen {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.7);
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
color: white;
z-index: 100;
}
.btn {
background: linear-gradient(135deg, #4299e1, #3182ce);
color: white;
border: none;
padding: 12px 24px;
border-radius: 8px;
font-size: 1rem;
font-weight: bold;
cursor: pointer;
margin-top: 20px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
transition: all 0.2s ease;
}
.btn:hover {
transform: translateY(-2px);
box-shadow: 0 6px 8px rgba(0, 0, 0, 0.15);
}
.btn:active {
transform: translateY(0);
}
.controls {
display: flex;
flex-wrap: wrap;
justify-content: center;
margin-top: 30px;
gap: 10px;
}
.control-key {
background-color: rgba(255, 255, 255, 0.1);
border: 1px solid rgba(255, 255, 255, 0.2);
border-radius: 6px;
padding: 8px 12px;
font-size: 0.9rem;
}
</style>
</head>
<body>
<div id="game-container">
<div id="score-display">Score: 0</div>
<canvas id="game-board" width="400" height="400"></canvas>
<div id="start-screen">
<h1 class="text-4xl font-bold mb-4">Wraparound Snake</h1>
<p class="text-xl mb-8">Eat the food and grow! The snake can teleport through edges.</p>
<button id="start-btn" class="btn">Start Game</button>
<div class="controls">
<div class="control-key">↑</div>
<div class="control-key">↓</div>
<div class="control-key">←</div>
<div class="control-key">→</div>
<div class="control-key">W</div>
<div class="control-key">A</div>
<div class="control-key">S</div>
<div class="control-key">D</div>
</div>
</div>
<div id="game-over">
<h1 class="text-4xl font-bold mb-4">Game Over!</h1>
<p class="text-xl mb-2">Your score: <span id="final-score">0</span></p>
<button id="restart-btn" class="btn">Play Again</button>
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', () => {
const canvas = document.getElementById('game-board');
const ctx = canvas.getContext('2d');
const scoreDisplay = document.getElementById('score-display');
const startScreen = document.getElementById('start-screen');
const gameOverScreen = document.getElementById('game-over');
const finalScoreDisplay = document.getElementById('final-score');
const startBtn = document.getElementById('start-btn');
const restartBtn = document.getElementById('restart-btn');
// Game settings
const gridSize = 20;
const tileCount = canvas.width / gridSize;
let speed = 7;
// Game state
let snake = [];
let food = {};
let direction = 'right';
let nextDirection = 'right';
let score = 0;
let gameRunning = false;
let gameLoop;
// Initialize game
function initGame() {
// Create snake
snake = [];
for (let i = 3; i >= 0; i--) {
snake.push({ x: i, y: 0 });
}
// Place first food
placeFood();
// Reset score and direction
score = 0;
direction = 'right';
nextDirection = 'right';
scoreDisplay.textContent = `Score: ${score}`;
// Start game loop
if (gameLoop) clearInterval(gameLoop);
gameRunning = true;
gameLoop = setInterval(gameStep, 1000 / speed);
}
// Place food at random position
function placeFood() {
food = {
x: Math.floor(Math.random() * tileCount),
y: Math.floor(Math.random() * tileCount)
};
// Make sure food doesn't appear on snake
for (let segment of snake) {
if (segment.x === food.x && segment.y === food.y) {
return placeFood();
}
}
}
// Main game loop
function gameStep() {
// Update direction
direction = nextDirection;
// Get head position
const head = { ...snake[0] };
// Move head based on direction with wraparound
switch (direction) {
case 'up':
head.y--;
if (head.y < 0) head.y = tileCount - 1;
break;
case 'down':
head.y++;
if (head.y >= tileCount) head.y = 0;
break;
case 'left':
head.x--;
if (head.x < 0) head.x = tileCount - 1;
break;
case 'right':
head.x++;
if (head.x >= tileCount) head.x = 0;
break;
}
// Check for self collision (only way to die)
for (let i = 0; i < snake.length; i++) {
const segment = snake[i];
if (segment.x === head.x && segment.y === head.y) {
return gameOver();
}
}
// Add new head
snake.unshift(head);
// Check if snake ate food
if (head.x === food.x && head.y === food.y) {
// Increase score
score++;
scoreDisplay.textContent = `Score: ${score}`;
// Increase speed every 5 points
if (score % 5 === 0) {
speed += 0.5;
clearInterval(gameLoop);
gameLoop = setInterval(gameStep, 1000 / speed);
}
// Place new food
placeFood();
} else {
// Remove tail if no food eaten
snake.pop();
}
// Draw game
drawGame();
}
// Draw game elements
function drawGame() {
// Clear canvas
ctx.fillStyle = '#2d3748';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Draw grid lines
ctx.strokeStyle = '#4a5568';
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();
}
// Draw food
ctx.beginPath();
ctx.arc(
food.x * gridSize + gridSize / 2,
food.y * gridSize + gridSize / 2,
gridSize / 2 - 2,
0,
Math.PI * 2
);
ctx.fillStyle = '#f56565';
ctx.fill();
ctx.strokeStyle = '#c53030';
ctx.lineWidth = 2;
ctx.stroke();
// Draw snake
for (let i = 0; i < snake.length; i++) {
const segment = snake[i];
const isHead = i === 0;
const isTail = i === snake.length - 1;
// Segment color gradient
let gradient;
if (isHead) {
gradient = ctx.createRadialGradient(
segment.x * gridSize + gridSize / 2,
segment.y * gridSize + gridSize / 2,
0,
segment.x * gridSize + gridSize / 2,
segment.y * gridSize + gridSize / 2,
gridSize / 2
);
gradient.addColorStop(0, '#48bb78');
gradient.addColorStop(1, '#38a169');
} else if (isTail) {
gradient = ctx.createRadialGradient(
segment.x * gridSize + gridSize / 2,
segment.y * gridSize + gridSize / 2,
0,
segment.x * gridSize + gridSize / 2,
segment.y * gridSize + gridSize / 2,
gridSize / 2
);
gradient.addColorStop(0, '#9ae6b4');
gradient.addColorStop(1, '#68d391');
} else {
gradient = ctx.createRadialGradient(
segment.x * gridSize + gridSize / 2,
segment.y * gridSize + gridSize / 2,
0,
segment.x * gridSize + gridSize / 2,
segment.y * gridSize + gridSize / 2,
gridSize / 2
);
gradient.addColorStop(0, '#68d391');
gradient.addColorStop(1, '#48bb78');
}
// Draw segment
ctx.beginPath();
ctx.arc(
segment.x * gridSize + gridSize / 2,
segment.y * gridSize + gridSize / 2,
gridSize / 2 - 1,
0,
Math.PI * 2
);
ctx.fillStyle = gradient;
ctx.fill();
ctx.strokeStyle = isHead ? '#2f855a' : '#38a169';
ctx.lineWidth = 2;
ctx.stroke();
// Draw eyes on head
if (isHead) {
// Calculate eye positions based on direction
let leftEyeX, leftEyeY, rightEyeX, rightEyeY;
switch (direction) {
case 'up':
leftEyeX = segment.x * gridSize + gridSize / 3;
leftEyeY = segment.y * gridSize + gridSize / 3;
rightEyeX = segment.x * gridSize + gridSize * 2/3;
rightEyeY = segment.y * gridSize + gridSize / 3;
break;
case 'down':
leftEyeX = segment.x * gridSize + gridSize / 3;
leftEyeY = segment.y * gridSize + gridSize * 2/3;
rightEyeX = segment.x * gridSize + gridSize * 2/3;
rightEyeY = segment.y * gridSize + gridSize * 2/3;
break;
case 'left':
leftEyeX = segment.x * gridSize + gridSize / 3;
leftEyeY = segment.y * gridSize + gridSize / 3;
rightEyeX = segment.x * gridSize + gridSize / 3;
rightEyeY = segment.y * gridSize + gridSize * 2/3;
break;
case 'right':
leftEyeX = segment.x * gridSize + gridSize * 2/3;
leftEyeY = segment.y * gridSize + gridSize / 3;
rightEyeX = segment.x * gridSize + gridSize * 2/3;
rightEyeY = segment.y * gridSize + gridSize * 2/3;
break;
}
// Draw eyes
ctx.beginPath();
ctx.arc(leftEyeX, leftEyeY, 3, 0, Math.PI * 2);
ctx.fillStyle = 'white';
ctx.fill();
ctx.beginPath();
ctx.arc(rightEyeX, rightEyeY, 3, 0, Math.PI * 2);
ctx.fillStyle = 'white';
ctx.fill();
// Draw pupils
ctx.beginPath();
ctx.arc(leftEyeX, leftEyeY, 1.5, 0, Math.PI * 2);
ctx.fillStyle = '#2d3748';
ctx.fill();
ctx.beginPath();
ctx.arc(rightEyeX, rightEyeY, 1.5, 0, Math.PI * 2);
ctx.fillStyle = '#2d3748';
ctx.fill();
}
}
}
// Game over
function gameOver() {
clearInterval(gameLoop);
gameRunning = false;
finalScoreDisplay.textContent = score;
gameOverScreen.style.display = 'flex';
}
// Event listeners
document.addEventListener('keydown', (e) => {
// Prevent direction reversal
switch (e.key) {
case 'ArrowUp':
case 'w':
case 'W':
if (direction !== 'down') nextDirection = 'up';
break;
case 'ArrowDown':
case 's':
case 'S':
if (direction !== 'up') nextDirection = 'down';
break;
case 'ArrowLeft':
case 'a':
case 'A':
if (direction !== 'right') nextDirection = 'left';
break;
case 'ArrowRight':
case 'd':
case 'D':
if (direction !== 'left') nextDirection = 'right';
break;
}
// Start game on any key if not running
if (!gameRunning && startScreen.style.display !== 'none') {
startGame();
}
});
startBtn.addEventListener('click', startGame);
restartBtn.addEventListener('click', startGame);
function startGame() {
startScreen.style.display = 'none';
gameOverScreen.style.display = 'none';
initGame();
}
// Responsive canvas sizing
function resizeCanvas() {
const size = Math.min(window.innerWidth - 40, window.innerHeight - 120, 600);
canvas.width = size;
canvas.height = size;
if (gameRunning) initGame();
}
window.addEventListener('resize', resizeCanvas);
resizeCanvas();
});
</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=ryck03/cobrinha-ehe" style="color: #fff;text-decoration: underline;" target="_blank" >Remix</a></p></body>
</html>