-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
189 lines (163 loc) · 5.32 KB
/
index.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
let lastPaintTime = 0;
let speed = 5;
let currentDirection = {x:0,y:0};
let food = {x:10,y:10};
let snake = [
{x:4,y:4}
];
let score = 0;
let a = 1;
let b = 19;
createSnake = (board) =>{
snake.forEach((element, index) => {
var htmlElement = document.createElement('div');
htmlElement.style.gridRowStart = element.x;
htmlElement.style.gridColumnStart= element.y;
if(index === 0){
htmlElement.classList.add('snakeHead');
}
else{
htmlElement.classList.add('snakeBody');
}
board.appendChild(htmlElement);
});
}
createFood = (board) => {
var htmlElement = document.createElement('div');
htmlElement.style.gridRowStart = food.x;
htmlElement.style.gridColumnStart= food.y;
htmlElement.classList.add('foodBody');
board.appendChild(htmlElement);
}
getBoard = () =>{
let board = document.getElementById('gameboard');
board.innerHTML="";
return board;
}
updateSnakeBody = () => {
for(var i = snake.length - 2 ; i >=0 ; i--){
snake[i+1] = {...snake[i]};
}
snake[0].x += currentDirection.x;
snake[0].y += currentDirection.y;
}
updateFoodLocation = () => {
var snakeHead = snake[0];
if(snakeHead.x == food.x && snakeHead.y == food.y){
snake.unshift({x:snakeHead.x + currentDirection.x ,y:snakeHead.y + currentDirection.y});
food = {x : Math.round(a + (b - a) * Math.random()), y: Math.round(a + (b - a) * Math.random())};
score++;
}
}
hasCollisionHappened = () => {
// If you bump into yourself
for (let i = 1; i < snake.length; i++) {
if(snake[i].x === snake[0].x && snake[i].y === snake[0].y){
return true;
}
}
// If you bump into the wall
if(snake[0].x >= 20 || snake[0].x <=0 || snake[0].y >= 20 || snake[0].y <=0){
return true;
}
return false;
}
refreshBoard = () => {
if(hasCollisionHappened()){
alert('Game Over! Your score is '+score+'.\nLet\'s Play again.');
score = 0;
currentDirection = {x:0,y:0};
food = {x : Math.round(a + (b - a) * Math.random()), y: Math.round(a + (b - a) * Math.random())};
snake = [
{x : Math.round(a + (b - a) * Math.random()), y: Math.round(a + (b - a) * Math.random())}
];
}
// update food location
updateFoodLocation();
//else update snake
updateSnakeBody();
//clear board
var board = getBoard();
//place Snake
createSnake(board);
//place food
createFood(board);
}
renderGame = (curentTime) =>{
window.requestAnimationFrame(renderGame);
if((curentTime - lastPaintTime) /1000 < (1 / speed)){
return;
}
lastPaintTime = curentTime;
refreshBoard();
}
window.requestAnimationFrame(renderGame);
window.addEventListener('keydown', e =>{
switch (e.key) {
case "ArrowUp":
currentDirection.x = -1;
currentDirection.y = 0;
break;
case "ArrowDown":
currentDirection.x = 1;
currentDirection.y = 0;
break;
case "ArrowLeft":
currentDirection.x = 0;
currentDirection.y = -1;
break;
case "ArrowRight":
currentDirection.x = 0;
currentDirection.y = 1;
break;
default:
break;
}
});
document.addEventListener('touchstart', handleTouchStart, false);
document.addEventListener('touchmove', handleTouchMove, false);
var xDown = null;
var yDown = null;
function getTouches(evt) {
return evt.touches || // browser API
evt.originalEvent.touches; // jQuery
}
function handleTouchStart(evt) {
const firstTouch = getTouches(evt)[0];
xDown = firstTouch.clientX;
yDown = firstTouch.clientY;
};
function handleTouchMove(evt) {
if ( ! xDown || ! yDown ) {
return;
}
var xUp = evt.touches[0].clientX;
var yUp = evt.touches[0].clientY;
var xDiff = xDown - xUp;
var yDiff = yDown - yUp;
if ( Math.abs( xDiff ) > Math.abs( yDiff ) ) {/*most significant*/
if ( xDiff > 0 ) {
/* left swipe */
currentDirection.x = 0;
currentDirection.y = -1;
} else {
/* right swipe */
currentDirection.x = 0;
currentDirection.y = 1;
}
} else {
if ( yDiff > 0 ) {
/* up swipe */
currentDirection.x = -1;
currentDirection.y = 0;
} else {
/* down swipe */
currentDirection.x = 1;
currentDirection.y = 0;
}
}
/* reset values */
xDown = null;
yDown = null;
};
alert("Press any Arrow key/Swipe(Mobile users) to start the game.");