|
| 1 | +import { useEffect, useState, useRef } from "react"; |
| 2 | +import { Box, Typography } from "@mui/material"; |
| 3 | +import EmulatorState from "../../../javascript-terminal/emulator-state/EmulatorState"; |
| 4 | + |
| 5 | +type Position = { x: number; y: number }; |
| 6 | + |
| 7 | +const GRID_WIDTH = 25; |
| 8 | +const GRID_HEIGHT = 12; |
| 9 | +const CELL_SIZE = 20; |
| 10 | +const INITIAL_SNAKE: Position[] = [{ x: 12, y: 6 }]; |
| 11 | +const INITIAL_DIRECTION: Position = { x: 1, y: 0 }; |
| 12 | +const GAME_SPEED = 150; |
| 13 | + |
| 14 | +const SnakeGame = ({ emulatorState }: { emulatorState: EmulatorState }) => { |
| 15 | + const [snake, setSnake] = useState<Position[]>(INITIAL_SNAKE); |
| 16 | + const [direction, setDirection] = useState<Position>(INITIAL_DIRECTION); |
| 17 | + const [food, setFood] = useState<Position>({ x: 18, y: 6 }); |
| 18 | + const [gameOver, setGameOver] = useState(false); |
| 19 | + const [score, setScore] = useState(0); |
| 20 | + const [countdown, setCountdown] = useState(3); |
| 21 | + const [loading, setLoading] = useState(true); |
| 22 | + const directionRef = useRef(direction); |
| 23 | + |
| 24 | + const generateFood = (): Position => { |
| 25 | + let newFood: Position; |
| 26 | + do { |
| 27 | + newFood = { |
| 28 | + x: Math.floor(Math.random() * GRID_WIDTH), |
| 29 | + y: Math.floor(Math.random() * GRID_HEIGHT) |
| 30 | + }; |
| 31 | + } while (snake.some((segment) => segment.x === newFood.x && segment.y === newFood.y)); |
| 32 | + return newFood; |
| 33 | + }; |
| 34 | + |
| 35 | + useEffect(() => { |
| 36 | + directionRef.current = direction; |
| 37 | + }, [direction]); |
| 38 | + |
| 39 | + useEffect(() => { |
| 40 | + if (gameOver) { |
| 41 | + const timer = setTimeout(() => { |
| 42 | + emulatorState.setBlockingMode(undefined); |
| 43 | + }, 1500); |
| 44 | + return () => clearTimeout(timer); |
| 45 | + } |
| 46 | + }, [gameOver, emulatorState]); |
| 47 | + |
| 48 | + useEffect(() => { |
| 49 | + if (countdown > 0) { |
| 50 | + const timer = setTimeout(() => { |
| 51 | + setCountdown(countdown - 1); |
| 52 | + }, 1000); |
| 53 | + return () => clearTimeout(timer); |
| 54 | + } else { |
| 55 | + setLoading(false); |
| 56 | + } |
| 57 | + }, [countdown]); |
| 58 | + |
| 59 | + useEffect(() => { |
| 60 | + const handleKeyPress = (e: KeyboardEvent) => { |
| 61 | + if (gameOver || loading) return; |
| 62 | + |
| 63 | + const key = e.key; |
| 64 | + const currentDir = directionRef.current; |
| 65 | + |
| 66 | + if (key === "ArrowUp" && currentDir.y === 0) { |
| 67 | + e.preventDefault(); |
| 68 | + setDirection({ x: 0, y: -1 }); |
| 69 | + } else if (key === "ArrowDown" && currentDir.y === 0) { |
| 70 | + e.preventDefault(); |
| 71 | + setDirection({ x: 0, y: 1 }); |
| 72 | + } else if (key === "ArrowLeft" && currentDir.x === 0) { |
| 73 | + e.preventDefault(); |
| 74 | + setDirection({ x: -1, y: 0 }); |
| 75 | + } else if (key === "ArrowRight" && currentDir.x === 0) { |
| 76 | + e.preventDefault(); |
| 77 | + setDirection({ x: 1, y: 0 }); |
| 78 | + } |
| 79 | + }; |
| 80 | + |
| 81 | + window.addEventListener("keydown", handleKeyPress); |
| 82 | + return () => window.removeEventListener("keydown", handleKeyPress); |
| 83 | + }, [gameOver, loading]); |
| 84 | + |
| 85 | + useEffect(() => { |
| 86 | + if (gameOver || loading) return; |
| 87 | + |
| 88 | + const gameLoop = setInterval(() => { |
| 89 | + setSnake((prevSnake) => { |
| 90 | + const head = prevSnake[0]; |
| 91 | + const newHead: Position = { |
| 92 | + x: head.x + directionRef.current.x, |
| 93 | + y: head.y + directionRef.current.y |
| 94 | + }; |
| 95 | + |
| 96 | + if ( |
| 97 | + newHead.x < 0 || |
| 98 | + newHead.x >= GRID_WIDTH || |
| 99 | + newHead.y < 0 || |
| 100 | + newHead.y >= GRID_HEIGHT |
| 101 | + ) { |
| 102 | + setGameOver(true); |
| 103 | + return prevSnake; |
| 104 | + } |
| 105 | + |
| 106 | + if ( |
| 107 | + prevSnake.some((segment) => segment.x === newHead.x && segment.y === newHead.y) |
| 108 | + ) { |
| 109 | + setGameOver(true); |
| 110 | + return prevSnake; |
| 111 | + } |
| 112 | + |
| 113 | + const newSnake = [newHead, ...prevSnake]; |
| 114 | + |
| 115 | + if (newHead.x === food.x && newHead.y === food.y) { |
| 116 | + setScore((prev) => prev + 10); |
| 117 | + setFood(generateFood()); |
| 118 | + return newSnake; |
| 119 | + } |
| 120 | + |
| 121 | + newSnake.pop(); |
| 122 | + return newSnake; |
| 123 | + }); |
| 124 | + }, GAME_SPEED); |
| 125 | + |
| 126 | + return () => clearInterval(gameLoop); |
| 127 | + }, [gameOver, food, loading]); |
| 128 | + |
| 129 | + return ( |
| 130 | + <Box sx={{ padding: 2 }}> |
| 131 | + <Typography sx={{ color: "#2BC903", mb: 1, fontFamily: "monospace" }}> |
| 132 | + SNAKE - Score: {score} |
| 133 | + </Typography> |
| 134 | + {loading ? ( |
| 135 | + <Box |
| 136 | + sx={{ |
| 137 | + display: "flex", |
| 138 | + alignItems: "center", |
| 139 | + justifyContent: "center", |
| 140 | + width: GRID_WIDTH * CELL_SIZE + 4, |
| 141 | + height: GRID_HEIGHT * CELL_SIZE + 4, |
| 142 | + border: "2px solid #2BC903" |
| 143 | + }} |
| 144 | + > |
| 145 | + <Typography sx={{ color: "#2BC903", fontSize: "1.5em" }}> |
| 146 | + {countdown > 0 ? countdown : "GO!"} |
| 147 | + </Typography> |
| 148 | + </Box> |
| 149 | + ) : gameOver ? ( |
| 150 | + <Box |
| 151 | + sx={{ |
| 152 | + display: "flex", |
| 153 | + flexDirection: "column", |
| 154 | + alignItems: "center", |
| 155 | + justifyContent: "center", |
| 156 | + width: GRID_WIDTH * CELL_SIZE + 4, |
| 157 | + height: GRID_HEIGHT * CELL_SIZE + 4, |
| 158 | + border: "2px solid #ff0606", |
| 159 | + gap: 2 |
| 160 | + }} |
| 161 | + > |
| 162 | + <Typography sx={{ color: "#ff0606", fontSize: "2em", fontWeight: "bold" }}> |
| 163 | + GAME OVER |
| 164 | + </Typography> |
| 165 | + <Typography sx={{ color: "#FCFCFC", fontSize: "1.2em" }}> |
| 166 | + Final Score: {score} |
| 167 | + </Typography> |
| 168 | + </Box> |
| 169 | + ) : ( |
| 170 | + <Box |
| 171 | + sx={{ |
| 172 | + display: "grid", |
| 173 | + gridTemplateColumns: `repeat(${GRID_WIDTH}, ${CELL_SIZE}px)`, |
| 174 | + gridTemplateRows: `repeat(${GRID_HEIGHT}, ${CELL_SIZE}px)`, |
| 175 | + gap: 0, |
| 176 | + border: "2px solid #2BC903", |
| 177 | + width: "fit-content" |
| 178 | + }} |
| 179 | + > |
| 180 | + {Array.from({ length: GRID_WIDTH * GRID_HEIGHT }).map((_, index) => { |
| 181 | + const x = index % GRID_WIDTH; |
| 182 | + const y = Math.floor(index / GRID_WIDTH); |
| 183 | + const snakeIndex = snake.findIndex( |
| 184 | + (segment) => segment.x === x && segment.y === y |
| 185 | + ); |
| 186 | + const isHead = snakeIndex === 0; |
| 187 | + const isSnake = snakeIndex !== -1; |
| 188 | + const isFood = food.x === x && food.y === y; |
| 189 | + |
| 190 | + return ( |
| 191 | + <Box |
| 192 | + key={index} |
| 193 | + sx={{ |
| 194 | + width: CELL_SIZE, |
| 195 | + height: CELL_SIZE, |
| 196 | + backgroundColor: isSnake |
| 197 | + ? "#2BC903" |
| 198 | + : isFood |
| 199 | + ? "#ff0606" |
| 200 | + : "#121212", |
| 201 | + border: "1px solid #1a1a1a", |
| 202 | + display: "flex", |
| 203 | + alignItems: "center", |
| 204 | + justifyContent: "center", |
| 205 | + fontSize: "10px", |
| 206 | + color: "#000000", |
| 207 | + fontWeight: "bold" |
| 208 | + }} |
| 209 | + > |
| 210 | + {isHead && "• •"} |
| 211 | + </Box> |
| 212 | + ); |
| 213 | + })} |
| 214 | + </Box> |
| 215 | + )} |
| 216 | + <Typography sx={{ color: "#FCFCFC", mt: 1, fontSize: "0.9em" }}> |
| 217 | + Use arrow keys to move |
| 218 | + </Typography> |
| 219 | + </Box> |
| 220 | + ); |
| 221 | +}; |
| 222 | + |
| 223 | +export default SnakeGame; |
0 commit comments