← BACK
CRYPTO SNAKE
SCORE: 0
₿: 0
Ξ: 0
ALT: 0

CRYPTO SNAKE

COLLECT COINS • GROW LONGER

₿ = 10 pts | Ξ = 5 pts | ALT = 1 pt

CLICK TO START

TOP SCORES

// Spawn a coin on the map function spawnCoin() { let type = Math.random(); let coinType; if (type < COINS.BITCOIN.rarity) { coinType = 'BITCOIN'; } else if (type < COINS.BITCOIN.rarity + COINS.ETH.rarity) { coinType = 'ETH'; } else if (type < COINS.BITCOIN.rarity + COINS.ETH.rarity + COINS.ALT.rarity) { coinType = 'ALT'; } else { return; } let x, y, valid; do { valid = true; x = Math.floor(Math.random() * GRID_SIZE); y = Math.floor(Math.random() * GRID_SIZE); // Check if position is on snake for (let segment of gameState.snake) { if (segment.x === x && segment.y === y) { valid = false; break; } } } while (!valid); gameState.coins[`${x},${y}`] = coinType; } // Initialize coins on the map function initCoins() { gameState.coins = {}; for (let i = 0; i < 3; i++) { spawnCoin(); } } // Draw the game function draw() { // Clear canvas ctx.fillStyle = '#0a0a0a'; ctx.fillRect(0, 0, CANVAS_SIZE, CANVAS_SIZE); // Draw grid ctx.strokeStyle = 'rgba(0, 255, 65, 0.05)'; ctx.lineWidth = 1; for (let i = 0; i <= GRID_SIZE; i++) { ctx.beginPath(); ctx.moveTo(i * CELL_SIZE, 0); ctx.lineTo(i * CELL_SIZE, CANVAS_SIZE); ctx.stroke(); ctx.beginPath(); ctx.moveTo(0, i * CELL_SIZE); ctx.lineTo(CANVAS_SIZE, i * CELL_SIZE); ctx.stroke(); } // Draw coins for (let key in gameState.coins) { const [x, y] = key.split(',').map(Number); const coinType = gameState.coins[key]; const coin = COINS[coinType]; ctx.fillStyle = coin.color; ctx.beginPath(); ctx.arc( x * CELL_SIZE + CELL_SIZE / 2, y * CELL_SIZE + CELL_SIZE / 2, CELL_SIZE / 3, 0, Math.PI * 2 ); ctx.fill(); // Draw coin symbol ctx.fillStyle = '#0a0a0a'; ctx.font = 'bold 10px Arial'; ctx.textAlign = 'center'; ctx.textBaseline = 'middle'; ctx.fillText( coin.symbol, x * CELL_SIZE + CELL_SIZE / 2, y * CELL_SIZE + CELL_SIZE / 2 ); } // Draw snake for (let i = gameState.snake.length - 1; i >= 0; i--) { const segment = gameState.snake[i]; if (i === gameState.snake.length - 1) { // Head - brighter green ctx.fillStyle = '#00ff41'; } else { // Body - slightly darker ctx.fillStyle = '#00cc33'; } ctx.fillRect( segment.x * CELL_SIZE + 2, segment.y * CELL_SIZE + 2, CELL_SIZE - 4, CELL_SIZE - 4 ); // Add outline ctx.strokeStyle = '#00ff41'; ctx.lineWidth = 1; ctx.strokeRect( segment.x * CELL_SIZE + 2, segment.y * CELL_SIZE + 2, CELL_SIZE - 4, CELL_SIZE - 4 ); } } // Update game state function update() { const now = Date.now(); if (now - lastMoveTime < gameState.speed) { return; } lastMoveTime = now; // Update direction gameState.direction = gameState.nextDirection; // Calculate new head position const head = gameState.snake[gameState.snake.length - 1]; const newHead = { x: head.x + gameState.direction.x, y: head.y + gameState.direction.y }; // Check wall collision if (newHead.x < 0 || newHead.x >= GRID_SIZE || newHead.y < 0 || newHead.y >= GRID_SIZE) { endGame(); return; } // Check self collision for (let segment of gameState.snake) { if (segment.x === newHead.x && segment.y === newHead.y) { endGame(); return; } } // Add new head gameState.snake.push(newHead); // Check for coin collision const key = `${newHead.x},${newHead.y}`; if (gameState.coins[key]) { const coinType = gameState.coins[key]; const coin = COINS[coinType]; gameState.score += coin.value; if (coinType === 'BITCOIN') { gameState.bitcoins++; } else if (coinType === 'ETH') { gameState.eths++; } else if (coinType === 'ALT') { gameState.alts++; } delete gameState.coins[key]; spawnCoin(); playEatSound(); // Increase speed every 50 points if (gameState.score % 50 === 0 && gameState.speed > 50) { gameState.speed -= 5; } } else { // Remove tail if no coin eaten gameState.snake.shift(); } updateScore(); } // Update score display function updateScore() { document.getElementById('score').textContent = gameState.score; document.getElementById('bitcoin-count').textContent = gameState.bitcoins; document.getElementById('eth-count').textContent = gameState.eths; document.getElementById('alt-count').textContent = gameState.alts; } // End game function endGame() { gameState.gameRunning = false; gameState.gameOver = true; playDieSound(); saveScore(gameState.score); loadLeaderboard(); document.getElementById('finalScore').textContent = `SCORE: ${gameState.score}`; document.getElementById('gameOverOverlay').classList.remove('hidden'); } // Save score to leaderboard function saveScore(score) { let scores = JSON.parse(localStorage.getItem('cryptoSnakeScores')) || []; scores.push({ score: score, date: new Date().toLocaleDateString() }); scores.sort((a, b) => b.score - a.score); scores = scores.slice(0, 5); localStorage.setItem('cryptoSnakeScores', JSON.stringify(scores)); } // Load and display leaderboard function loadLeaderboard() { let scores = JSON.parse(localStorage.getItem('cryptoSnakeScores')) || []; let html = ''; if (scores.length === 0) { html = '
NO SCORES YET
'; } else { scores.forEach((s, i) => { html += `
#${i + 1}: ${s.score} ${s.date}
`; }); } document.getElementById('leaderboardList').innerHTML = html; } // Keyboard controls document.addEventListener('keydown', (e) => { if (!gameState.gameRunning) return; let newDirection = gameState.nextDirection; switch(e.key.toLowerCase()) { case 'arrowup': case 'w': if (gameState.direction.y === 0) { newDirection = { x: 0, y: -1 }; playTurnSound(); } e.preventDefault(); break; case 'arrowdown': case 's': if (gameState.direction.y === 0) { newDirection = { x: 0, y: 1 }; playTurnSound(); } e.preventDefault(); break; case 'arrowleft': case 'a': if (gameState.direction.x === 0) { newDirection = { x: -1, y: 0 }; playTurnSound(); } e.preventDefault(); break; case 'arrowright': case 'd': if (gameState.direction.x === 0) { newDirection = { x: 1, y: 0 }; playTurnSound(); } e.preventDefault(); break; } gameState.nextDirection = newDirection; }); // Touch and swipe controls let touchStartX = 0; let touchStartY = 0; canvas.addEventListener('touchstart', (e) => { touchStartX = e.touches[0].clientX; touchStartY = e.touches[0].clientY; }); canvas.addEventListener('touchmove', (e) => { if (!gameState.gameRunning) return; e.preventDefault(); const touchEndX = e.touches[0].clientX; const touchEndY = e.touches[0].clientY; const diffX = touchEndX - touchStartX; const diffY = touchEndY - touchStartY; if (Math.abs(diffX) > Math.abs(diffY)) { if (diffX > 20 && gameState.direction.x === 0) { gameState.nextDirection = { x: 1, y: 0 }; playTurnSound(); touchStartX = touchEndX; } else if (diffX < -20 && gameState.direction.x === 0) { gameState.nextDirection = { x: -1, y: 0 }; playTurnSound(); touchStartX = touchEndX; } } else { if (diffY > 20 && gameState.direction.y === 0) { gameState.nextDirection = { x: 0, y: 1 }; playTurnSound(); touchStartY = touchEndY; } else if (diffY < -20 && gameState.direction.y === 0) { gameState.nextDirection = { x: 0, y: -1 }; playTurnSound(); touchStartY = touchEndY; } } }); // Mobile D-pad buttons document.querySelector('.dpad-up').addEventListener('click', () => { if (!gameState.gameRunning) return; if (gameState.direction.y === 0) { gameState.nextDirection = { x: 0, y: -1 }; playTurnSound(); } }); document.querySelector('.dpad-down').addEventListener('click', () => { if (!gameState.gameRunning) return; if (gameState.direction.y === 0) { gameState.nextDirection = { x: 0, y: 1 }; playTurnSound(); } }); document.querySelector('.dpad-left').addEventListener('click', () => { if (!gameState.gameRunning) return; if (gameState.direction.x === 0) { gameState.nextDirection = { x: -1, y: 0 }; playTurnSound(); } }); document.querySelector('.dpad-right').addEventListener('click', () => { if (!gameState.gameRunning) return; if (gameState.direction.x === 0) { gameState.nextDirection = { x: 1, y: 0 }; playTurnSound(); } }); // Start game on click document.getElementById('startOverlay').addEventListener('click', () => { startGame(); }); // Game over - restart on click document.getElementById('gameOverOverlay').addEventListener('click', () => { startGame(); }); // Start game function function startGame() { gameState.snake = [{ x: 10, y: 10 }]; gameState.direction = { x: 1, y: 0 }; gameState.nextDirection = { x: 1, y: 0 }; gameState.score = 0; gameState.bitcoins = 0; gameState.eths = 0; gameState.alts = 0; gameState.speed = 100; gameState.gameRunning = true; gameState.gameOver = false; lastMoveTime = 0; document.getElementById('startOverlay').classList.add('hidden'); document.getElementById('gameOverOverlay').classList.add('hidden'); initCoins(); updateScore(); gameLoop(); } // Main game loop function gameLoop() { if (gameState.gameRunning) { update(); draw(); requestAnimationFrame(gameLoop); } else if (gameState.gameOver) { draw(); } else { draw(); requestAnimationFrame(gameLoop); } } // Initialize on load window.addEventListener('load', () => { loadLeaderboard(); draw(); gameLoop(); });