-
Notifications
You must be signed in to change notification settings - Fork 0
/
food.js
32 lines (27 loc) · 1003 Bytes
/
food.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
// in this file we will draw food that the snake will be eating inside the gameboard
import { randomGridPosition } from "./gridPos.js";
import { onSnake, expandSnake } from "./snakeFun.js";
const EXPANSION_RATE = 5;
let food = randomGridPosition(); // 0 is technically outside of the grid
export function update(){
if (onSnake(food)){
expandSnake(EXPANSION_RATE);
food = randomGridPosition();
}
}
export function draw(gameBoard){
const foodElement = document.createElement('div');
foodElement.style.gridColumnStart = food.x;
foodElement.style.gridRowStart = food.y;
foodElement.classList.add('food');
gameBoard.appendChild(foodElement);
}
function getRandomFoodPosition(){// this function will help to randomize the food position
// food must not be on the snake
let newFoodPosition;
while (newFoodPosition == null || onSnake(newFoodPosition))
{
newFoodPosition = randomGridPosition();
}
return newFoodPosition;
}