This commit is contained in:
Fredrik Jensen 2025-10-27 18:56:03 +01:00
parent c64d89b5db
commit bf7a37e66c
3 changed files with 47 additions and 5545 deletions

View File

@ -13,8 +13,6 @@
"build": "vite build --outDir build" "build": "vite build --outDir build"
}, },
"devDependencies": { "devDependencies": {
"neutrino": "^6.1.5",
"neutrino-preset-web": "^6.1.5",
"vite": "^7.1.7" "vite": "^7.1.7"
} }
} }

View File

@ -1,15 +1,15 @@
import { random, find, takeRight } from 'lodash'; import { random, find, takeRight } from "lodash";
import config from './config'; import config from "./config";
import { createDivElement, setDivColor, setDivsColor, drawLetter } from './utils/div'; import {
import keys from './utils/keys'; createDivElement,
import { S, N, A, K, E } from './utils/letters'; setDivColor,
setDivsColor,
drawLetter,
} from "./utils/div";
import keys from "./utils/keys";
import { S, N, A, K, E } from "./utils/letters";
const { const { dimensions, unit, snake, food } = config;
dimensions,
unit,
snake,
food,
} = config;
let GAME_STARTED = false; let GAME_STARTED = false;
let GAME_RUNNING; let GAME_RUNNING;
@ -21,11 +21,11 @@ let SNAKE_SPEED = 100;
* Draw the board * Draw the board
*/ */
createDivElement('board', { createDivElement("board", {
height: `${unit * dimensions.height}px`, height: `${unit * dimensions.height}px`,
width: `${unit * dimensions.width}px`, width: `${unit * dimensions.width}px`,
position: 'relative', position: "relative",
fontSize: '0', fontSize: "0",
}); });
/** /**
@ -34,12 +34,16 @@ createDivElement('board', {
for (let i = 0; i < dimensions.height; i++) { for (let i = 0; i < dimensions.height; i++) {
for (let j = 0; j < dimensions.width; j++) { for (let j = 0; j < dimensions.width; j++) {
createDivElement(`${i + 1}.${j + 1}`, { createDivElement(
`${i + 1}.${j + 1}`,
{
width: `${unit}px`, width: `${unit}px`,
height: `${unit}px`, height: `${unit}px`,
display: 'inline-block', display: "inline-block",
background: '#000', background: "blue",
}, 'board'); },
"board"
);
} }
} }
@ -47,9 +51,10 @@ for (let i = 0; i < dimensions.height; i++) {
* Draw the snake * Draw the snake
*/ */
const showSnake = () => setDivsColor(takeRight(SNAKE_LOG, snake.body), 'yellow'); const showSnake = () =>
setDivsColor(takeRight(SNAKE_LOG, snake.body), "yellow");
const hideSnake = () => setDivsColor(takeRight(SNAKE_LOG, snake.body), 'black'); const hideSnake = () => setDivsColor(takeRight(SNAKE_LOG, snake.body), "black");
/** /**
* Move the snake * Move the snake
@ -62,19 +67,19 @@ const pushToSnakeLog = (cords) => {
const moveSnake = (direction) => { const moveSnake = (direction) => {
hideSnake(); hideSnake();
if (direction === 'left') { if (direction === "left") {
snake.y--; snake.y--;
} }
if (direction === 'up') { if (direction === "up") {
snake.x--; snake.x--;
} }
if (direction === 'right') { if (direction === "right") {
snake.y++; snake.y++;
} }
if (direction === 'down') { if (direction === "down") {
snake.x++; snake.x++;
} }
@ -103,29 +108,29 @@ const moveSnake = (direction) => {
* Set key events * Set key events
*/ */
const setSnakeDirection = direction => SNAKE_DIRECTION = direction; const setSnakeDirection = (direction) => (SNAKE_DIRECTION = direction);
const keyFunctions = ({ keyCode }) => { const keyFunctions = ({ keyCode }) => {
switch (keyCode) { switch (keyCode) {
case keys.ENTER: case keys.ENTER:
if (GAME_STARTED) return; if (GAME_STARTED) return;
startGame(); startGame();
case keys.LEFT: case keys.LEFT:
setSnakeDirection('left'); setSnakeDirection("left");
break; break;
case keys.UP: case keys.UP:
setSnakeDirection('up'); setSnakeDirection("up");
break; break;
case keys.RIGHT: case keys.RIGHT:
setSnakeDirection('right'); setSnakeDirection("right");
break; break;
case keys.DOWN: case keys.DOWN:
setSnakeDirection('down'); setSnakeDirection("down");
break; break;
} }
}; };
document.addEventListener('keydown', keyFunctions); document.addEventListener("keydown", keyFunctions);
/** /**
* Make food * Make food
@ -136,24 +141,24 @@ const makeFood = () => {
const y = random(1, config.dimensions.height); const y = random(1, config.dimensions.height);
const snakeLength = SNAKE_LOG.length - 1; const snakeLength = SNAKE_LOG.length - 1;
const snakeBody = SNAKE_LOG.slice((snakeLength - snake.body), snakeLength); const snakeBody = SNAKE_LOG.slice(snakeLength - snake.body, snakeLength);
if (find(snakeBody, { x, y })) { if (find(snakeBody, { x, y })) {
console.log('food placed under the snake...'); console.log("food placed under the snake...");
return makeFood(); return makeFood();
} }
food.x = x; food.x = x;
food.y = y; food.y = y;
setDivColor('red', x, y); setDivColor("red", x, y);
}; };
/** /**
* Check if snake is eating * Check if snake is eating
*/ */
const checkIfSnakeIsEating = (snake, food) => { const checkIfSnakeIsEating = (snake, food) => {
if (snake.x === food.x && snake.y === food.y) { if (snake.x === food.x && snake.y === food.y) {
snake.body = snake.body + 1; snake.body = snake.body + 1;
SNAKE_SPEED = SNAKE_SPEED - 10; SNAKE_SPEED = SNAKE_SPEED - 10;
@ -165,16 +170,16 @@ const checkIfSnakeIsEating = (snake, food) => {
* Check collision * Check collision
*/ */
const checkCollision = ({ x, y }) => { const checkCollision = ({ x, y }) => {
const snakeLength = SNAKE_LOG.length - 2; const snakeLength = SNAKE_LOG.length - 2;
const snakeBody = SNAKE_LOG.slice((snakeLength - snake.body), snakeLength); const snakeBody = SNAKE_LOG.slice(snakeLength - snake.body, snakeLength);
if (find(snakeBody, { x, y })) { if (find(snakeBody, { x, y })) {
GAME_STARTED = false; GAME_STARTED = false;
clearInterval(GAME_RUNNING); clearInterval(GAME_RUNNING);
startScreen(); startScreen();
} }
} };
/** /**
* Start screen * Start screen
@ -186,13 +191,12 @@ const startScreen = () => {
A.forEach(drawLetter); A.forEach(drawLetter);
K.forEach(drawLetter); K.forEach(drawLetter);
E.forEach(drawLetter); E.forEach(drawLetter);
} };
/** /**
* Run game * Run game
*/ */
const startGame = () => { const startGame = () => {
const runGame = () => { const runGame = () => {
moveSnake(SNAKE_DIRECTION); moveSnake(SNAKE_DIRECTION);
@ -207,4 +211,3 @@ const startGame = () => {
}; };
startScreen(); startScreen();

5499
yarn.lock

File diff suppressed because it is too large Load Diff