-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclassic_snake.js
186 lines (140 loc) · 3.73 KB
/
classic_snake.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
const cvs = document.getElementById("snakecanvas");
const context = cvs.getContext("2d");
const box = 20;
const w = 1000;
const h = 500;
// sounds
let dead = new Audio();
let eat = new Audio();
dead.src = '/sounds/dead.mp3';
eat.src = '/sounds/eat.mp3';
let snake = [];
// intialize snake
snake[0] = {
x: 20 * box,
y: 20 * box
};
// intialize food
let food = {
x: Math.floor(Math.random() * (w / box)) * box,
y: Math.floor(Math.random() * (h / box)) * box
}
let score = 0;
//snake controlling part
let direction;
document.addEventListener("keydown", findDirection);
function findDirection(event) {
let key = event.keyCode;
if(key == 37 || key == 38 || key == 39 || key == 40)
document.getElementById('instruction').innerHTML = 'press p to pause';
if (key == 37 && direction != "RIGHT") {
direction = "LEFT";
delay();
} else if (key == 38 && direction != "DOWN") {
direction = "UP";
delay();
} else if (key == 39 && direction != "LEFT") {
direction = "RIGHT";
delay();
} else if (key == 40 && direction != "UP") {
direction = "DOWN";
delay();
} else if (key == 80) {
togglePause();
delay();
}
}
// collison of snake with snake body
function collision(head, array) {
for (let i = 0; i < array.length; i++) {
if (head.x == array[i].x && head.y == array[i].y) {
return true;
}
}
return false;
}
function drawFood(x, y) {
context.fillStyle = "red";
context.fillRect(x, y, box, box);
context.strokeStyle = 'black';
context.strokeRect(x, y, box, box);
}
function draw() {
context.fillStyle = 'lightgrey';
context.fillRect(0, 0, w, h);
context.strokeStyle = 'black';
context.strokeRect(0, 0, w, h);
for (let i = 0; i < snake.length; i++) {
context.fillStyle = (i == 0) ? "green" : "yellow";
context.fillRect(snake[i].x, snake[i].y, box, box);
context.strokeStyle = "black";
context.strokeRect(snake[i].x, snake[i].y, box, box);
}
drawFood(food.x, food.y);
//old direction positions
let snakeX = snake[0].x;
let snakeY = snake[0].y;
//moving in direction
if (direction == "LEFT") snakeX -= box;
if (direction == "UP") snakeY -= box;
if (direction == "RIGHT") snakeX += box;
if (direction == "DOWN") snakeY += box;
// if the snake eats the food
if (snakeX == food.x && snakeY == food.y) {
score++;
eat.play();
food = {
x: Math.floor(Math.random() * (w / box)) * box,
y: Math.floor(Math.random() * (h / box)) * box
}
for (let i = 0; i < snake.length; i++) {
if (food.x === snake[i].x && food.y === snake[i].y) {
food = {
x: Math.floor(Math.random() * (w / box)) * box,
y: Math.floor(Math.random() * (h / box)) * box
}
i = 0;
}
}
} else {
snake.pop(); //removing tail of snake
}
//adding new head for snake
delay();
let newHead = {
x: snakeX,
y: snakeY
}
// checking game over condition
if (snakeX < 0 || snakeX == w || snakeY < 0 || snakeY == h || collision(newHead, snake)) {
dead.play();
clearInterval(game);
alert(` Oops: Game Over \n Your Score: ${score}`);
name = prompt('Enter Your Name ');
if (name == '') name = 'anonymous'
window.location = `${window.location.protocol}gameover/${name}/${score}`;
}
snake.unshift(newHead);
displayScore();
}
// call draw function every 100 ms
let game = setInterval(draw, 100);
let paused = false;
//function to pause the game
function togglePause() {
if (!paused) {
paused = true;
clearInterval(game);
} else {
paused = false;
game = setInterval(draw, 100);
}
}
let name;
function displayScore() {
document.getElementById('score').innerHTML = score;
}
function delay(){
for(let i = 0;i<200;i++)
for(let j = 0;j<1000;j++);
}