-
Notifications
You must be signed in to change notification settings - Fork 1
/
input.js
41 lines (39 loc) · 1.33 KB
/
input.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
//input direction of user when clicking keys
let inputDirection = {
x: 0,
y: 0,
}
//making sure the user clicks on a direction that does not go opposite of the previous
//for example, if snake goes left, we cannot go right, so we need to keep track of last direction input
let lastInputDirection = {
x: 0,
y: 0,
}
//event listener to take in user input and control snake movement on the grid
//checks user's last direction to not be related to the y or x movements of new input
//(ex. up or down not allowed/y movement not allowed if last is y-axis related)
window.addEventListener('keydown', e => {
switch (e.key) {
case 'ArrowUp' :
if (lastInputDirection.y != 0) break
inputDirection = { x: 0, y: -1 }
break
case 'ArrowDown' :
if (lastInputDirection.y != 0) break
inputDirection = { x: 0, y: 1 }
break
case 'ArrowLeft' :
if (lastInputDirection.x != 0) break
inputDirection = { x: -1, y: 0 }
break
case 'ArrowRight' :
if (lastInputDirection.x != 0) break
inputDirection = { x: 1, y: 0 }
break
}
})
//sets the current input direction as last input direction
export function getInputDirection() {
lastInputDirection = inputDirection
return inputDirection
}