-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
297 lines (211 loc) · 7.97 KB
/
scripts.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
var dropdown = document.getElementById("dropdown").style;
document.getElementById("clickSpeed").addEventListener("click", function() {
document.getElementById("dropdown").style.display = "block";
});
var speed = 200;
document.getElementById("speed1").addEventListener("click", function() {
speed = 800;
});
document.getElementById("speed2").addEventListener("click", function() {
speed = 400;
});
document.getElementById("speed3").addEventListener("click", function() {
speed = (200 * (4 / 3));
});
document.getElementById("speed4").addEventListener("click", function() {
speed = 200;
});
document.getElementById("speed5").addEventListener("click", function() {
speed = (200 * (4 / 5));
});
document.getElementById("speed6").addEventListener("click", function() {
speed = (200 * (2 / 3));
});
document.getElementById("speed7").addEventListener("click", function() {
speed = (200 * (4 / 7));
});
document.getElementById("speed8").addEventListener("click", function() {
speed = (200 * (1 / 2));
});
function backtohome() {
location.reload();
}
document.getElementById("start").addEventListener("click", startTheGame);
function startTheGame() {
document.getElementById("dropdown").style.display = "none";
document.getElementById("disScore").style.display = "block";
var cvs = document.getElementById("canvas");
var ctx = cvs.getContext("2d");
var displayScore = document.getElementById("disScore");
var cvsW = cvs.width;
var cvsH = cvs.height;
var snakeW = 10;
var snakeH = 10;
var score = 0;
//direction
var direction = "right";
//read users diection
document.addEventListener("keydown", getDirection);
function getDirection(e) {
if (e.keyCode === 37 && direction != "right") {
direction = "left";
} else if (e.keyCode === 38 && direction != "down") {
direction = "up";
} else if (e.keyCode === 39 && direction != "left") {
direction = "right";
} else if (e.keyCode === 40 && direction != "up") {
direction = "down";
}
}
function swipedetect(el, callback) {
var el = document.getElementById("canvas");
var swipedir,
startX,
startY,
distX,
distY,
threshold = 90, //required min distance traveled to be considered swipe
restraint = 150, // maximum distance allowed at the same time in perpendicular direction
allowedTime = 300, // maximum time allowed to travel that distance
elapsedTime,
startTime,
handleswipe = callback || function(swipedir) {}
document.getElementById("canvas").addEventListener('touchstart', function(e) {
var touchobj = e.changedTouches[0]
swipedir = 'none'
dist = 0
startX = touchobj.pageX
startY = touchobj.pageY
startTime = new Date().getTime() // record time when finger first makes contact with surface
e.preventDefault()
}, false)
document.getElementById("canvas").addEventListener('touchmove', function(e) {
e.preventDefault() // prevent scrolling when inside DIV
}, false)
document.getElementById("canvas").addEventListener('touchend', function(e) {
var touchobj = e.changedTouches[0]
distX = touchobj.pageX - startX // get horizontal dist traveled by finger while in contact with surface
distY = touchobj.pageY - startY // get vertical dist traveled by finger while in contact with surface
elapsedTime = new Date().getTime() - startTime // get time elapsed
if (elapsedTime <= allowedTime) { // first condition for awipe met
if (Math.abs(distX) >= threshold && Math.abs(distY) <= restraint) { // 2nd condition for horizontal swipe met
swipedir = (distX < 0) ? 'left' : 'right' // if dist traveled is negative, it indicates left swipe
} else if (Math.abs(distY) >= threshold && Math.abs(distX) <= restraint) { // 2nd condition for vertical swipe met
swipedir = (distY < 0) ? 'up' : 'down' // if dist traveled is negative, it indicates up swipe
}
}
handleswipe(swipedir)
e.preventDefault()
}, false)
}
swipedetect(cvs, function(swipedir) {
if (swipedir == 'left' && direction != "right") {
direction = "left";
} else if (swipedir == 'up' && direction != "down") {
direction = "up";
} else if (swipedir == 'right' && direction != "left") {
direction = "right";
} else if (swipedir == 'down' && direction != "up") {
direction = "down";
}
})
function drawSnake(x, y) {
ctx.fillStyle = "white";
ctx.fillRect(x * snakeW, y * snakeH, snakeW, snakeH);
}
//create snake array
var len = 4;
var snake = [];
for (var i = len - 1; i >= 0; i--) {
snake.push({
x: i,
y: 0
});
}
//create food
food = {
x: Math.floor(Math.random() * (cvsW / snakeW)),
y: Math.floor(Math.random() * (cvsH / snakeH))
}
function drawFood(x, y) {
ctx.fillStyle = "yellow";
ctx.fillRect(x * snakeW, y * snakeH, snakeW, snakeH);
}
//check collision
function checkCollision(x, y, array) {
for (var i = 0; i < array.length; i++) {
if (x === array[i].x && y === array[i].y) {
return true;
}
}
return false;
}
function foodOnSnake(array) {
for (var i = 0; i < snake.length; i++) {
if (food.x === snake[i].x && food.y === snake[i].y) {
return true;
}
}
return false;
}
function drawScore(x) {
displayScore.textContent = "Score: " + x;
}
function displayPopup() {
document.getElementById("popupId").style.display = "flex";
}
function draw() {
ctx.clearRect(0, 0, cvsW, cvsH);
for (var i = 0; i < snake.length; i++) {
var x = snake[i].x;
var y = snake[i].y;
drawSnake(x, y);
}
if (foodOnSnake(snake)) {
food = {
x: Math.floor(Math.random() * (cvsW / snakeW)),
y: Math.floor(Math.random() * (cvsH / snakeH))
}
drawFood(food.x, food.y);
} else {
drawFood(food.x, food.y);
}
//snake head
var snakeX = snake[0].x;
var snakeY = snake[0].y;
if (direction === "left") {
snakeX--;
} else if (direction === "up") {
snakeY--;
} else if (direction === "right") {
snakeX++;
} else if (direction === "down") {
snakeY++;
}
if (snakeX < 0 || snakeY < 0 || snakeX >= cvsW / snakeW || snakeY >= cvsH / snakeH || checkCollision(snakeX, snakeY, snake)) {
//location.reload();
setTimeout(displayPopup, 200);
var finalScore = score;
}
if (snakeX === food.x && snakeY === food.y) {
food = {
x: Math.floor(Math.random() * (cvsW / snakeW)),
y: Math.floor(Math.random() * (cvsH / snakeH))
}
var newHead = {
x: snakeX,
y: snakeY
};
score++;
} else {
snake.pop();
var newHead = {
x: snakeX,
y: snakeY
};
}
snake.unshift(newHead);
drawScore(score);
}
setInterval(draw, speed);
}