-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
140 lines (116 loc) · 3.31 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
const eat = new Audio('./eat.mp3.mp3');
const gameover = new Audio('./gameover.mp3.wav');
let snakearr = [
{x:1, y:2}
];
let velocity = {x: 0, y:0};
let food = {x:8, y:13};
let lastPaintTime = 0;
let speed = 15;
let point = 0;
let box = document.getElementById('box');
let score = document.getElementById('sc');
// Rendering.....
function main(ctime){
window.requestAnimationFrame(main);
//console.log(ctime)
if((ctime - lastPaintTime)/1000 < 1/speed){
return;
}
lastPaintTime = ctime;
gameEngine();
}
function isCollide(array){
for (let i= 1; i < array.length; i++) {
if(snakearr[0].x === array[i].x && snakearr[0].y === array[i].y){
//console.log("1");
return true;
}
}
if(snakearr[0].x > 40 || snakearr[0].x <= 0 || snakearr[0].y <= 0 || snakearr[0].y > 40){
console.log("2");
return true;
}
//console.log("3");
return false;
}
//game function
function gameEngine() {
// updating snake and food
if(isCollide(snakearr)){
console.log("4");
snakearr = [
{x:1, y:2}
];
velocity = {x: 0, y:0};
food = {x:8, y:13};
point = 0;
score.innerHTML = "Point: " + point;
gameover.play();
}
if(snakearr[0].x === food.x && snakearr[0].y === food.y){
eat.play();
point = point + 1;
score.innerHTML = "Point: " + point;
snakearr.unshift({x:velocity.x + snakearr[0].x, y:velocity.y + snakearr[0].y });
let a = 2;
let b = 39;
food = {x: Math.round(a +(b-a)*Math.random()), y: Math.round(a +(b-a)*Math.random())};
}
// Moving snake
for (let i = snakearr.length - 2; i >= 0; i--) {
snakearr[i+1] = {...snakearr[i]};
}
snakearr[0].x += velocity.x;
snakearr[0].y += velocity.y;
// display of food and snake and snake body
box.innerHTML = "";
snakearr.forEach((e,index) => {
//console.log(e);
headelement = document.createElement('div');
headelement.style.gridRowStart = e.y;
headelement.style.gridColumnStart = e.x;
if(index === 0){
headelement.classList.add('head');
}
else{
headelement.classList.add('body');
}
//console.log(headelement);
box.appendChild(headelement);
});
foodelement = document.createElement('div');
foodelement.style.gridRowStart = food.y;
foodelement.style.gridColumnStart = food.x;
foodelement.classList.add('food');
box.appendChild(foodelement);
}
window.requestAnimationFrame(main);
window.addEventListener('keydown', e =>{
// console.log("fwf00");
velocity = {x: 0, y: 1};
switch (e.key) {
case "ArrowUp":
console.log("arrowup");
velocity.x = 0;
velocity.y = -1;
break;
case "ArrowDown":
console.log("arrod");
velocity.x = 0;
velocity.y = 1;
break;
case "ArrowRight":
console.log("arrowr");
velocity.x = 1;
velocity.y = 0;
break;
case "ArrowLeft":
console.log("arrowl");
velocity.x = -1;
velocity.y = 0;
break;
default:
break;
}
});