import { useState, useEffect } from 'react'; import { Gamepad2, ChevronUp } from 'lucide-react'; export function ArcadeApp() { const [snake, setSnake] = useState([{ x: 10, y: 10 }]); const [food, setFood] = useState({ x: 15, y: 15 }); const [direction, setDirection] = useState({ x: 1, y: 0 }); const [gameOver, setGameOver] = useState(false); const [score, setScore] = useState(0); const [isPlaying, setIsPlaying] = useState(false); useEffect(() => { if (!isPlaying || gameOver) return; const interval = setInterval(() => { setSnake(prev => { const newHead = { x: prev[0].x + direction.x, y: prev[0].y + direction.y }; if (newHead.x < 0 || newHead.x >= 20 || newHead.y < 0 || newHead.y >= 20) { setGameOver(true); setIsPlaying(false); return prev; } if (prev.some(s => s.x === newHead.x && s.y === newHead.y)) { setGameOver(true); setIsPlaying(false); return prev; } const newSnake = [newHead, ...prev]; if (newHead.x === food.x && newHead.y === food.y) { setScore(s => s + 10); setFood({ x: Math.floor(Math.random() * 20), y: Math.floor(Math.random() * 20) }); } else { newSnake.pop(); } return newSnake; }); }, 150); return () => clearInterval(interval); }, [isPlaying, gameOver, direction, food]); useEffect(() => { const handleKey = (e: KeyboardEvent) => { if (!isPlaying) return; switch (e.key) { case 'ArrowUp': if (direction.y !== 1) setDirection({ x: 0, y: -1 }); break; case 'ArrowDown': if (direction.y !== -1) setDirection({ x: 0, y: 1 }); break; case 'ArrowLeft': if (direction.x !== 1) setDirection({ x: -1, y: 0 }); break; case 'ArrowRight': if (direction.x !== -1) setDirection({ x: 1, y: 0 }); break; } }; window.addEventListener('keydown', handleKey); return () => window.removeEventListener('keydown', handleKey); }, [isPlaying, direction]); const startGame = () => { setSnake([{ x: 10, y: 10 }]); setFood({ x: 15, y: 15 }); setDirection({ x: 1, y: 0 }); setGameOver(false); setScore(0); setIsPlaying(true); }; return (

Cyber Snake

Score: {score}
{Array.from({ length: 400 }).map((_, i) => { const x = i % 20; const y = Math.floor(i / 20); const isSnake = snake.some(s => s.x === x && s.y === y); const isHead = snake[0]?.x === x && snake[0]?.y === y; const isFood = food.x === x && food.y === y; return (
); })}
{!isPlaying && ( )} {isPlaying && (
)}
Use arrow keys to move Tap buttons to move
); }