-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake.js
More file actions
58 lines (52 loc) · 1.75 KB
/
snake.js
File metadata and controls
58 lines (52 loc) · 1.75 KB
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
export let snakeSpeed = 2// how many times per second does the snake move
export let snakeBody = []
export let movementDirection = {'direction':{'x':'add'}}
export const gameBoard = document.getElementById('game-board')
export function updateSnake() {
for (let step = snakeBody.length - 2; step >= 0; step--){
snakeBody[step + 1] = { ...snakeBody[step]}
}
snakeBody[0] = getHeadsNextCoordinates()
}
function getHeadsNextCoordinates(){
let snakeHead = {...snakeBody[0]}
const coordinate = Object.keys(movementDirection['direction'])[0]
const direction = Object.values(movementDirection['direction'])[0]
if (direction === 'add') {
snakeHead[coordinate] += 1
} else {
snakeHead[coordinate] -= 1
}
return snakeHead
}
export function appendNewSnakeSegment(){
snakeBody.unshift(getHeadsNextCoordinates())
}
export function drawSnake(){
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)
})
}
document.onkeydown = (event) => {
const pressedKey = event.key
if (pressedKey === 'ArrowUp'){
movementDirection['direction'] = {'y':'subtract'}
}else if (pressedKey === 'ArrowDown'){
movementDirection['direction'] = {'y':'add'}
}else if (pressedKey === 'ArrowRight'){
movementDirection['direction'] = {'x':'add'}
}else if (pressedKey === 'ArrowLeft'){
movementDirection['direction'] = {'x':'subtract'}
}
}
export function manipulateSpeeed(operator){
if (operator === 'add'){
snakeSpeed += 1
}else{
snakeSpeed -= 1
}
}