Spaces:
Running

tear / index.html
Koptan's picture
Add 3 files
c829ec4 verified
Raw History Blame Contribute Delete
16.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 Fusion: Numbers & Words</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>
@keyframes pulse {
0%, 100% { transform: scale(1); }
50% { transform: scale(1.05); }
}
.pulse-animation {
animation: pulse 1.5s infinite;
}
.snake-segment {
transition: all 0.1s ease;
}
.food-item {
transition: transform 0.2s ease;
}
.food-item:hover {
transform: scale(1.2);
}
#game-board {
box-shadow: 0 0 30px rgba(0, 0, 0, 0.1);
}
</style>
</head>
<body class="bg-gradient-to-br from-indigo-50 to-purple-50 min-h-screen flex flex-col items-center justify-center p-4">
<div class="w-full max-w-4xl mx-auto">
<header class="text-center mb-6">
<h1 class="text-4xl md:text-5xl font-bold text-transparent bg-clip-text bg-gradient-to-r from-purple-600 to-indigo-600 mb-2">
Snake Fusion
</h1>
<p class="text-gray-600 text-lg">Combine numbers and words in this educational twist on the classic Snake game!</p>
</header>
<div class="flex flex-col lg:flex-row gap-6">
<!-- Game Controls Panel -->
<div class="w-full lg:w-1/3 bg-white rounded-xl shadow-lg p-6 flex flex-col">
<div class="flex justify-between items-center mb-4">
<div>
<h2 class="text-xl font-semibold text-gray-800">Game Controls</h2>
<p class="text-sm text-gray-500">Use arrow keys or swipe</p>
</div>
<div class="flex gap-2">
<button id="pause-btn" class="bg-yellow-500 hover:bg-yellow-600 text-white p-2 rounded-full">
<i class="fas fa-pause"></i>
</button>
<button id="sound-btn" class="bg-blue-500 hover:bg-blue-600 text-white p-2 rounded-full">
<i class="fas fa-volume-up"></i>
</button>
</div>
</div>
<div class="mb-6">
<div class="flex justify-between mb-2">
<span class="font-medium">Score:</span>
<span id="score" class="font-bold text-purple-600">0</span>
</div>
<div class="flex justify-between mb-4">
<span class="font-medium">High Score:</span>
<span id="high-score" class="font-bold text-indigo-600">0</span>
</div>
<div class="h-2 bg-gray-200 rounded-full mb-2">
<div id="progress-bar" class="h-full bg-gradient-to-r from-green-400 to-blue-500 rounded-full" style="width: 0%"></div>
</div>
<p id="progress-text" class="text-xs text-gray-500 text-right">0% to next level</p>
</div>
<div class="mb-6">
<h3 class="font-medium mb-2">Game Mode</h3>
<div class="flex gap-2">
<button id="math-mode" class="flex-1 py-2 px-4 rounded-lg bg-purple-100 text-purple-800 font-medium border-2 border-purple-300">
<i class="fas fa-calculator mr-2"></i>Math
</button>
<button id="word-mode" class="flex-1 py-2 px-4 rounded-lg bg-indigo-100 text-indigo-800 font-medium border-2 border-indigo-300">
<i class="fas fa-font mr-2"></i>Words
</button>
</div>
</div>
<div id="current-target" class="bg-gray-50 rounded-lg p-4 mb-6 text-center">
<h3 class="font-medium mb-1 text-gray-600">Current Target</h3>
<p id="target-display" class="text-2xl font-bold text-gray-800">Select a mode</p>
</div>
<div id="collected-items" class="bg-gray-50 rounded-lg p-4 flex-1">
<h3 class="font-medium mb-2 text-gray-600">Collected Items</h3>
<div id="items-display" class="flex flex-wrap gap-2 min-h-20">
<div class="text-gray-400 italic">Items will appear here</div>
</div>
</div>
</div>
<!-- Game Board -->
<div class="w-full lg:w-2/3 flex flex-col">
<div id="game-board" class="w-full aspect-square bg-white rounded-xl shadow-lg overflow-hidden relative">
<div id="game-overlay" class="absolute inset-0 flex flex-col items-center justify-center bg-black bg-opacity-70 text-white z-10 hidden">
<h2 class="text-3xl font-bold mb-4">Game Over!</h2>
<p id="final-score" class="text-xl mb-6">Your score: 0</p>
<button id="restart-btn" class="bg-green-500 hover:bg-green-600 text-white font-bold py-3 px-6 rounded-full pulse-animation">
Play Again
</button>
</div>
<div id="start-screen" class="absolute inset-0 flex flex-col items-center justify-center bg-white z-10">
<h2 class="text-3xl font-bold mb-6 text-gray-800">Snake Fusion</h2>
<p class="text-gray-600 mb-8 text-center max-w-md px-4">
Combine numbers to solve math problems or collect letters to form words!
</p>
<button id="start-btn" class="bg-gradient-to-r from-purple-500 to-indigo-600 hover:from-purple-600 hover:to-indigo-700 text-white font-bold py-3 px-8 rounded-full pulse-animation">
Start Game
</button>
</div>
</div>
<div class="mt-4 flex justify-between items-center">
<div class="flex gap-2">
<button id="up-btn" class="bg-gray-200 hover:bg-gray-300 text-gray-800 p-3 rounded-full">
<i class="fas fa-arrow-up"></i>
</button>
</div>
<div class="flex gap-2">
<button id="left-btn" class="bg-gray-200 hover:bg-gray-300 text-gray-800 p-3 rounded-full">
<i class="fas fa-arrow-left"></i>
</button>
<button id="right-btn" class="bg-gray-200 hover:bg-gray-300 text-gray-800 p-3 rounded-full">
<i class="fas fa-arrow-right"></i>
</button>
</div>
<div class="flex gap-2">
<button id="down-btn" class="bg-gray-200 hover:bg-gray-300 text-gray-800 p-3 rounded-full">
<i class="fas fa-arrow-down"></i>
</button>
</div>
</div>
</div>
</div>
<!-- Instructions Modal -->
<div id="instructions-modal" class="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-20 hidden">
<div class="bg-white rounded-xl p-6 max-w-md w-full mx-4 max-h-[80vh] overflow-y-auto">
<div class="flex justify-between items-center mb-4">
<h3 class="text-xl font-bold text-gray-800">How to Play</h3>
<button id="close-instructions" class="text-gray-500 hover:text-gray-700">
<i class="fas fa-times"></i>
</button>
</div>
<div class="space-y-4 text-gray-700">
<div>
<h4 class="font-semibold text-purple-600">Math Mode</h4>
<p>Collect numbers to solve the displayed equation. The snake grows with each correct number.</p>
</div>
<div>
<h4 class="font-semibold text-indigo-600">Word Mode</h4>
<p>Collect letters to form the displayed word. The snake grows with each correct letter.</p>
</div>
<div>
<h4 class="font-semibold">Controls</h4>
<p>Use arrow keys or on-screen buttons to control the snake. Avoid walls and yourself!</p>
</div>
<div>
<h4 class="font-semibold">Scoring</h4>
<p>Earn points for each correct item collected. Complete targets for bonus points!</p>
</div>
</div>
<button id="got-it-btn" class="mt-6 w-full bg-purple-500 hover:bg-purple-600 text-white font-bold py-2 px-4 rounded-lg">
Got It!
</button>
</div>
</div>
</div>
<footer class="mt-8 text-center text-gray-500 text-sm">
<button id="help-btn" class="text-blue-500 hover:text-blue-700 underline">
<i class="fas fa-question-circle mr-1"></i>Need Help?
</button>
<p class="mt-2">Snake Fusion - Combining learning with fun!</p>
</footer>
<script>
// Game variables
let canvas, ctx;
let snake = [];
let food = [];
let gridSize = 20;
let tileCount = 20;
let xVelocity = 0;
let yVelocity = 0;
let score = 0;
let highScore = localStorage.getItem('snakeFusionHighScore') || 0;
let gameSpeed = 150;
let gameInterval;
let isPaused = false;
let isGameOver = false;
let gameMode = null; // 'math' or 'word'
let currentTarget = '';
let collectedItems = [];
let soundEnabled = true;
let levelProgress = 0;
let levelThreshold = 5;
// Math problems and word lists
const mathProblems = [
{ equation: "5 + 3", answer: "8" },
{ equation: "10 - 4", answer: "6" },
{ equation: "2 × 3", answer: "6" },
{ equation: "12 ÷ 4", answer: "3" },
{ equation: "7 + 8", answer: "15" },
{ equation: "9 - 5", answer: "4" },
{ equation: "4 × 5", answer: "20" },
{ equation: "18 ÷ 3", answer: "6" }
];
const wordList = [
{ word: "apple", letters: ["a", "p", "p", "l", "e"] },
{ word: "banana", letters: ["b", "a", "n", "a", "n", "a"] },
{ word: "cherry", letters: ["c", "h", "e", "r", "r", "y"] },
{ word: "orange", letters: ["o", "r", "a", "n", "g", "e"] },
{ word: "grape", letters: ["g", "r", "a", "p", "e"] },
{ word: "melon", letters: ["m", "e", "l", "o", "n"] },
{ word: "pear", letters: ["p", "e", "a", "r"] },
{ word: "kiwi", letters: ["k", "i", "w", "i"] }
];
// DOM elements
const scoreElement = document.getElementById('score');
const highScoreElement = document.getElementById('high-score');
const targetDisplay = document.getElementById('target-display');
const itemsDisplay = document.getElementById('items-display');
const gameOverlay = document.getElementById('game-overlay');
const startScreen = document.getElementById('start-screen');
const finalScoreElement = document.getElementById('final-score');
const progressBar = document.getElementById('progress-bar');
const progressText = document.getElementById('progress-text');
const currentTargetSection = document.getElementById('current-target');
// Initialize the game
function initGame() {
canvas = document.createElement('canvas');
canvas.width = tileCount * gridSize;
canvas.height = tileCount * gridSize;
document.getElementById('game-board').appendChild(canvas);
ctx = canvas.getContext('2d');
// Set high score
highScoreElement.textContent = highScore;
// Event listeners for buttons
document.getElementById('start-btn').addEventListener('click', startGame);
document.getElementById('restart-btn').addEventListener('click', restartGame);
document.getElementById('pause-btn').addEventListener('click', togglePause);
document.getElementById('sound-btn').addEventListener('click', toggleSound);
document.getElementById('math-mode').addEventListener('click', () => setGameMode('math'));
document.getElementById('word-mode').addEventListener('click', () => setGameMode('word'));
document.getElementById('help-btn').addEventListener('click', showInstructions);
document.getElementById('close-instructions').addEventListener('click', hideInstructions);
document.getElementById('got-it-btn').addEventListener('click', hideInstructions);
// Arrow buttons for mobile
document.getElementById('up-btn').addEventListener('click', () => changeDirection(0, -1));
document.getElementById('down-btn').addEventListener('click', () => changeDirection(0, 1));
document.getElementById('left-btn').addEventListener('click', () => changeDirection(-1, 0));
document.getElementById('right-btn').addEventListener('click', () => changeDirection(1, 0));
// Keyboard controls
document.addEventListener('keydown', keyDown);
// Touch controls for mobile
let touchStartX = 0;
let touchStartY = 0;
canvas.addEventListener('touchstart', (e) => {
touchStartX = e.touches[0].clientX;
touchStartY = e.touches[0].clientY;
}, false);
canvas.addEventListener('touchmove', (e) => {
if (!touchStartX || !touchStartY) return;
const touchEndX = e.touches[0].clientX;
const touchEndY = e.touches[0].clientY;
const dx = touchEndX - touchStartX;
const dy = touchEndY - touchStartY;
if (Math.abs(dx) > Math.abs(dy)) {
// Horizontal swipe
if (dx > 0) changeDirection(1, 0); // Right
else changeDirection(-1, 0); // Left
} else {
// Vertical swipe
if (dy > 0) changeDirection(0, 1); // Down
else changeDirection(0, -1); // Up
}
touchStartX = 0;
touchStartY = 0;
e.preventDefault();
}, false);
}
// Start the game
function startGame() {
if (!gameMode) {
alert('Please select a game mode first!');
return;
}
startScreen.classList.add('hidden');
gameOverlay.classList.add('hidden');
isGameOver = false;
score = 0;
levelProgress = 0;
updateScore();
updateProgressBar();
// Initialize snake
snake = [];
for (let i = 3; i >= 0; i--) {
snake.push({ x: i, y: 0 });
}
// Reset direction
xVelocity = 1;
yVelocity = 0;
// Generate initial food
generateFood();
// Start game loop
clearInterval(gameInterval);
gameInterval = setInterval(gameLoop, gameSpeed);
}
// Game loop
function gameLoop() {
if (isPaused || isGameOver) return;
// Clear canvas
ctx.fillStyle = 'white';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Move snake
const head = { x: snake[0].x + xVelocity, y: snake[0].y + yVelocity };
// Check collision with walls
if (head.x < 0 || head.x >= tileCount || head.y < 0 || head.y >= tileCount) {
gameOver();
return;
}
// Check collision with self
</html>