working example 🎉
This commit is contained in:
parent
d876180f82
commit
e1fca87276
@ -3,3 +3,7 @@
|
|||||||
A snake implementation with vanilla JavaScript
|
A snake implementation with vanilla JavaScript
|
||||||
|
|
||||||
🚨 work in progress 🚨
|
🚨 work in progress 🚨
|
||||||
|
|
||||||
|
```
|
||||||
|
yarn start
|
||||||
|
```
|
||||||
13
src/index.js
13
src/index.js
@ -16,6 +16,7 @@ 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',
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -41,7 +42,7 @@ const showSnake = () => {
|
|||||||
for(var i = 0; i < snake.body; i++) {
|
for(var i = 0; i < snake.body; i++) {
|
||||||
const logEvent = snake.log[snake.log.length - (1 + i)];
|
const logEvent = snake.log[snake.log.length - (1 + i)];
|
||||||
|
|
||||||
setDivCOlor(
|
setDivColor(
|
||||||
'yellow',
|
'yellow',
|
||||||
(logEvent || {}).x,
|
(logEvent || {}).x,
|
||||||
(logEvent || {}).y,
|
(logEvent || {}).y,
|
||||||
@ -53,7 +54,7 @@ const hideSnake = () => {
|
|||||||
for(var i = 0; i < snake.body; i++) {
|
for(var i = 0; i < snake.body; i++) {
|
||||||
const logEvent = snake.log[snake.log.length - (1 + snake.body + i)];
|
const logEvent = snake.log[snake.log.length - (1 + snake.body + i)];
|
||||||
|
|
||||||
setDivCOlor(
|
setDivColor(
|
||||||
'#000',
|
'#000',
|
||||||
(logEvent || {}).x,
|
(logEvent || {}).x,
|
||||||
(logEvent || {}).y,
|
(logEvent || {}).y,
|
||||||
@ -148,6 +149,9 @@ const makeFood = () => {
|
|||||||
const x = random(1, config.dimensions.width);
|
const x = random(1, config.dimensions.width);
|
||||||
const y = random(1, config.dimensions.height);
|
const y = random(1, config.dimensions.height);
|
||||||
|
|
||||||
|
const snakeLength = snake.log.length - 1;
|
||||||
|
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...');
|
||||||
/**
|
/**
|
||||||
@ -160,10 +164,7 @@ const makeFood = () => {
|
|||||||
food.x = x;
|
food.x = x;
|
||||||
food.y = y;
|
food.y = y;
|
||||||
|
|
||||||
const snakeLength = snake.log.length - 1;
|
setDivColor('red', x, y);
|
||||||
const snakeBody = snake.log.slice((snakeLength - snake.body), snakeLength);
|
|
||||||
|
|
||||||
setDivCOlor('red', x, y);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user