"use client"; import { useState, useEffect, useCallback, useRef } from "react"; import { Play, RotateCcw, Monitor, Globe, X, Minus, Square } from "lucide-react"; import { useTranslations } from "@/lib/i18n"; const GRID_SIZE = 20; const INITIAL_SPEED = 150; type Point = { x: number; y: number }; export default function SnakeGame() { const [snake, setSnake] = useState([{ x: 10, y: 10 }, { x: 10, y: 11 }, { x: 10, y: 12 }]); const [food, setFood] = useState({ x: 5, y: 5 }); const [direction, setDirection] = useState({ x: 0, y: -1 }); const [playing, setPlaying] = useState(false); const [gameOver, setGameOver] = useState(false); const [score, setScore] = useState(0); const [highScore, setHighScore] = useState(0); const t = useTranslations(); const gameLoopRef = useRef(null); const generateFood = useCallback((currentSnake: Point[]) => { let newFood; while (true) { newFood = { x: Math.floor(Math.random() * GRID_SIZE), y: Math.floor(Math.random() * GRID_SIZE), }; if (!currentSnake.some(s => s.x === newFood!.x && s.y === newFood!.y)) break; } return newFood; }, []); const moveSnake = useCallback(() => { setSnake((prev) => { const head = { x: prev[0].x + direction.x, y: prev[0].y + direction.y }; // Wall Collision if (head.x < 0 || head.x >= GRID_SIZE || head.y < 0 || head.y >= GRID_SIZE) { setGameOver(true); setPlaying(false); return prev; } // Self Collision if (prev.some(s => s.x === head.x && s.y === head.y)) { setGameOver(true); setPlaying(false); return prev; } const newSnake = [head, ...prev]; // Food Collision if (head.x === food.x && head.y === food.y) { setScore(s => s + 10); setFood(generateFood(newSnake)); } else { newSnake.pop(); } return newSnake; }); }, [direction, food, generateFood]); useEffect(() => { if (playing && !gameOver) { gameLoopRef.current = setInterval(moveSnake, Math.max(INITIAL_SPEED - Math.floor(score / 50) * 10, 60)); } else { if (gameLoopRef.current) clearInterval(gameLoopRef.current); } return () => { if (gameLoopRef.current) clearInterval(gameLoopRef.current); }; }, [playing, gameOver, moveSnake, score]); useEffect(() => { const handleKeyDown = (e: KeyboardEvent) => { const key = e.key.toLowerCase(); if ((key === "w" || key === "arrowup") && direction.y !== 1) setDirection({ x: 0, y: -1 }); if ((key === "s" || key === "arrowdown") && direction.y !== -1) setDirection({ x: 0, y: 1 }); if ((key === "a" || key === "arrowleft") && direction.x !== 1) setDirection({ x: -1, y: 0 }); if ((key === "d" || key === "arrowright") && direction.x !== -1) setDirection({ x: 1, y: 0 }); }; window.addEventListener("keydown", handleKeyDown); return () => window.removeEventListener("keydown", handleKeyDown); }, [direction]); const startGame = () => { setSnake([{ x: 10, y: 10 }, { x: 10, y: 11 }, { x: 10, y: 12 }]); setFood({ x: 5, y: 5 }); setDirection({ x: 0, y: -1 }); setScore(0); setGameOver(false); setPlaying(true); }; useEffect(() => { if (score > highScore) setHighScore(score); }, [score, highScore]); return (
{/* Browser Bar */}
agent-skill.co/simulator/snake
{/* Game Content Area */}
{/* Scoreboard */}
{t.snake.score} {score}
{t.snake.best} {highScore}
{/* The Grid Canvas */}
{/* Render Snake */} {snake.map((s, i) => (
{/* Snake Head Eyes */} {i === 0 && (
)}
))} {/* Render Food */}
{/* Background Grid Pattern */}
{/* Overlays */} {!playing && (
{gameOver ? t.snake.gameOver : t.snake.title}

{t.snake.desc}

)}
{/* Footer controls instruction */}
{t.snake.controls} {t.snake.mode}
); }