Compare commits

...

9 Commits
test ... main

Author SHA1 Message Date
e059ff91eb add lock 2026-02-01 23:12:02 +01:00
87e42d87a5 change color 2025-10-30 19:57:06 +01:00
c494780fad update color 2025-10-29 19:45:26 +01:00
d4610bf5e8 change color 2025-10-29 02:04:13 +01:00
6c8897746c update color 2025-10-29 01:36:35 +01:00
afe39c77c4 update color 2025-10-29 01:33:40 +01:00
0ad707476d change color 2025-10-29 01:06:51 +01:00
bf7a37e66c fix deps 2025-10-27 18:56:03 +01:00
c64d89b5db move to vite 2025-09-30 20:04:22 +02:00
7 changed files with 1127 additions and 5195 deletions

1
.gitignore vendored
View File

@ -30,4 +30,3 @@ glow.apk
compose-scripts.md
uploads
package-lock.json

6
index.html Normal file
View File

@ -0,0 +1,6 @@
<html>
<body>
<div id="app"></div>
<script type="module" src="/src/index.js"></script>
</body>
</html>

1068
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -9,11 +9,10 @@
"lodash": "^4.17.4"
},
"scripts": {
"start": "neutrino start --use neutrino-preset-web",
"build": "neutrino build --use neutrino-preset-web"
"start": "vite",
"build": "vite build --outDir build"
},
"devDependencies": {
"neutrino": "^6.1.5",
"neutrino-preset-web": "^6.1.5"
"vite": "^7.1.7"
}
}

View File

@ -1,5 +1,5 @@
export default {
bgColor: "grey",
unit: 20,
dimensions: {
width: 30,
@ -14,5 +14,5 @@ export default {
food: {
x: null,
y: null,
}
},
};

View File

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

5143
yarn.lock

File diff suppressed because it is too large Load Diff