-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfood.js
30 lines (26 loc) · 784 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
// THIS IS FOR GENERATING AND DRAWING FOOD
import { onSnake, expandSnake } from "./snake.js";
let food = generateFood();
const EXPANSION_RATE = 1;
function generateFood() {
const foodPosition = {
x: Math.floor(Math.random() * 22) + 1,
y: Math.floor(Math.random() * 22) + 1,
};
if (foodPosition == null || onSnake(foodPosition)) return generateFood();
return foodPosition;
}
export function update() {
console.log(food);
if (onSnake(food)) {
expandSnake(EXPANSION_RATE);
food = generateFood();
}
}
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);
}