-
Notifications
You must be signed in to change notification settings - Fork 0
/
snakeFun.js
63 lines (51 loc) · 1.84 KB
/
snakeFun.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { getInputDirections } from "./input.js";
export var SNAKE_SPEED= 4;
// creating the snake body inside the gameboard;
// how we are going to represent our snake {using x and y position since we are using grid method}
const snakeBody = [{x: 11, y: 11} ];
let segmentNumber = 0;
export function update(){
// we want to make our snake moves
const inputDirections = getInputDirections(); // inputing the controls for movement
addSegments();
for (var i = snakeBody.length-2; i>= 0; i--){
snakeBody[i+1]= { ...snakeBody[i] };// making another object of duplicate known as snakebody
}
snakeBody[0].x += inputDirections.x;
snakeBody[0].y += inputDirections.y;
}
export function checkSnakeHead(){
return snakeBody[0];
}
export function snakeIntersect(){
return onSnake(snakeBody[0], {ignoreHead: true});
}
export function draw(gameBoard){
snakeBody.forEach(segment => {
const snakeElement = document.createElement('div');
snakeElement.style.gridRowStart= segment.y;
snakeElement.style.gridColumnStart = segment.x;
snakeElement.classList.add('snake');
gameBoard.appendChild(snakeElement);
})
}
export function onSnake(position , {ignoreHead = false} = {}){ // function to see when snake is on the food
return snakeBody.some((segment,index) =>{
if(ignoreHead && index === 0) return false
return equalPosition(segment,position);
})
}
function equalPosition (pos1,pos2){
return pos1.x === pos2.x && pos1.y === pos2.y ;
}
export function expandSnake(amount){
return segmentNumber += amount;
}
function addSegments(){
for(let i=0 ; i<segmentNumber; i++ )
{
snakeBody[snakeBody.length]= {...snakeBody[snakeBody-1]}
} // why snakeBody[snakeBody.length-1]?
// we have to stop the segment number to make it move
segmentNumber = 0;
}