fix formatting
This commit is contained in:
parent
f24ab96870
commit
0ffaa4d08e
@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
export default {
|
export default {
|
||||||
unit: 20,
|
unit: 20,
|
||||||
dimensions: {
|
dimensions: {
|
||||||
@ -14,5 +13,5 @@ export default {
|
|||||||
food: {
|
food: {
|
||||||
x: null,
|
x: null,
|
||||||
y: null,
|
y: null,
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
91
src/index.js
91
src/index.js
@ -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(
|
||||||
width: `${unit}px`,
|
`${i + 1}.${j + 1}`,
|
||||||
height: `${unit}px`,
|
{
|
||||||
display: 'inline-block',
|
width: `${unit}px`,
|
||||||
background: '#000',
|
height: `${unit}px`,
|
||||||
}, 'board');
|
display: "inline-block",
|
||||||
|
background: "#000",
|
||||||
|
},
|
||||||
|
"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();
|
||||||
|
|
||||||
|
|||||||
@ -3,7 +3,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
export const createDivElement = (name, styles, target) => {
|
export const createDivElement = (name, styles, target) => {
|
||||||
window[name] = document.createElement('div');
|
window[name] = document.createElement("div");
|
||||||
window[name].id = name;
|
window[name].id = name;
|
||||||
|
|
||||||
Object.assign(window[name].style, styles);
|
Object.assign(window[name].style, styles);
|
||||||
@ -29,14 +29,19 @@ export const setDivColor = (color, x, y) => {
|
|||||||
|
|
||||||
export const setDivsColor = (divs, color) => {
|
export const setDivsColor = (divs, color) => {
|
||||||
divs.forEach(({ x, y }) => {
|
divs.forEach(({ x, y }) => {
|
||||||
((document.getElementById(`${x}.${y}`) || {}).style || {}).background = color;
|
((document.getElementById(`${x}.${y}`) || {}).style || {}).background =
|
||||||
|
color;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Draw charchter
|
* Draw charchter
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export const drawLetter = (char) => {
|
export const drawLetter = (char) => {
|
||||||
setDivColor('#333', char.toString().split('.')[0], char.toString().split('.')[1]);
|
setDivColor(
|
||||||
};
|
"#333",
|
||||||
|
char.toString().split(".")[0],
|
||||||
|
char.toString().split(".")[1]
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|||||||
@ -8,4 +8,4 @@ export default {
|
|||||||
UP: 38,
|
UP: 38,
|
||||||
RIGHT: 39,
|
RIGHT: 39,
|
||||||
DOWN: 40,
|
DOWN: 40,
|
||||||
}
|
};
|
||||||
|
|||||||
@ -3,7 +3,18 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
export const S = [2.2, 2.3, 2.4, 2.5, 3.2, 4.3, 5.4, 6.5, 7.5, 7.4, 7.3, 7.2];
|
export const S = [2.2, 2.3, 2.4, 2.5, 3.2, 4.3, 5.4, 6.5, 7.5, 7.4, 7.3, 7.2];
|
||||||
export const N = [2.7, 3.7, 4.7, 5.7, 6.7, 7.7, 3.8, 4.9, 5.9, 6.10, 6.11, 7.11, 6.11, 5.11, 4.11, 3.11, 2.11];
|
export const N = [
|
||||||
export const A = [2.13, 3.13, 4.13, 5.13, 6.13, 7.13, 2.14, 2.15, 2.16, 2.17, 3.17, 4.17, 5.17, 6.17, 7.17, 5.16, 5.15, 5.14];
|
2.7, 3.7, 4.7, 5.7, 6.7, 7.7, 3.8, 4.9, 5.9, 6.1, 6.11, 7.11, 6.11, 5.11,
|
||||||
export const K = [2.19, 3.19, 4.19, 4.20, 5.19, 5.20, 6.19, 7.19, 6.21, 7.22, 3.21, 2.22];
|
4.11, 3.11, 2.11,
|
||||||
export const E = [2.24, 3.24, 4.24, 5.24, 6.24, 7.24, 2.25, 2.26, 2.27, 2.28, 7.25, 7.26, 7.27, 7.28, 4.25];
|
];
|
||||||
|
export const A = [
|
||||||
|
2.13, 3.13, 4.13, 5.13, 6.13, 7.13, 2.14, 2.15, 2.16, 2.17, 3.17, 4.17, 5.17,
|
||||||
|
6.17, 7.17, 5.16, 5.15, 5.14,
|
||||||
|
];
|
||||||
|
export const K = [
|
||||||
|
2.19, 3.19, 4.19, 4.2, 5.19, 5.2, 6.19, 7.19, 6.21, 7.22, 3.21, 2.22,
|
||||||
|
];
|
||||||
|
export const E = [
|
||||||
|
2.24, 3.24, 4.24, 5.24, 6.24, 7.24, 2.25, 2.26, 2.27, 2.28, 7.25, 7.26, 7.27,
|
||||||
|
7.28, 4.25,
|
||||||
|
];
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user