-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
317 lines (266 loc) · 10.8 KB
/
index.html
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Snake :-: Awake</title>
<link rel='stylesheet' type='text/css' media='screen' href='style.css'>
</head>
<body>
</body>
<script>
window.onload = () => {
letsPlay();
};
window.addEventListener("click", function () {
click.play();
})
const click = new Audio("sound/click.mp3");
const gameMusic = new Audio("sound/thescore.mp3");
gameMusic.volume = 0.4;
gameMusic.loop = true;//to replay the music everytime it ends.
const snakeMove = new Audio("sound/move.mp3");
snakeMove.volume = 1;
const foodEaten = new Audio("sound/crunchyeat.mp3");
const topScore = new Audio("sound/sparkle.mp3");
const hitTail = new Audio("sound/snare.wav");
let body = document.querySelector("body");
let input;
const letsPlay = () => {
let btnState = 1;
body.innerHTML = `<div class="container">
<div class="strtCont">
<h1 class="head">Snake:-:Awake</h1>
<button class="btn" onclick="myCompany()">Click to play</button>
</div>
</div>`
btnRotate = setInterval(() => {
let btn = document.querySelector(".btn");
if (btnState % 2 === 0) {
btn.classList.remove("btn-rotate");
btnState++;
}
else {
btn.classList.add("btn-rotate");
btnState++;
}
}, 1500);
}
var loading1,loading2,loading3;
const skipIntro=()=>{
document.removeEventListener('keyup',skipIntro);
clearTimeout(loading1);
clearTimeout(loading2);
clearTimeout(loading3);
enterName();
}
const myCompany = () => {
clearInterval(btnRotate);
document.querySelector('.btn').innerHTML = "Setting Up...";
setTimeout(() => {
loading1=setTimeout(() => {
document.addEventListener('keyup', skipIntro)//press any key to skip intro
body.innerHTML = `<div class="container">
<div class="creator">
<span>Created & Designed By Kalash Agrahari</span>
</div>
</div>`
})
loading2=setTimeout(() => {
body.innerHTML = `<div class="container">
<div class="creator">
<span>Co-powered By Kalash Agrahari</span>
</div>
</div>`
}, 8000)
loading3=setTimeout(()=>{enterName()},16000)
}, 1000)
}
const enterName = () => {
setTimeout(() => {
body.innerHTML = `<div class="container">
<div class=box>
<div class="snake-image"></div>
<div class="form-subcont">
<form class="nameform">
<input type="text" id="name" placeholder="Enter your name" spellcheck="false" required>
<button type="submit" class="btn-submit">Continue</button>
</form>
</div>
<div class="snake-image"></div>
</div>
</div>`;
input = document.querySelector("#name");
document.querySelector(".btn-submit").addEventListener("click", () => {
console.log("This is name of the player: ", input.value);
if (input.value == "") {
// if input is left empty then nothing will happen, during this form will show "please fill out this field"
}
else {
body.innerHTML = '<div class="container container-blur"></div> <div class="rules-box"><h3 class="rules-head"> RULES </h3><ol class="rules-container"><li class="rules">Use arrow keys to move the snake.</li><li class="rules">If you hit your tail you will lose a live.</li></ol><button onclick="snakeComes()" class="rules-ok">OK</button></div>';
}
})
})
}
const snakeComes = () => {
var highScoreVal;
var youMadeTopScore = true;
if (localStorage.getItem('highScore') === null) {
highScoreVal = 0;
localStorage.setItem('highScore', highScoreVal);
}
else {
highScoreVal = localStorage.getItem('highScore');
}
body.innerHTML = `<div class="container">
<div class="sub-cont">
<div class="score-board">
<span>Player : ${input.value}</span><span id="score">Score : 0</span><span id="highScore">Top Score : ${highScoreVal}</span><span id="lives">Lives : <img class="heart" src="images/heart.png"><img class="heart" src="images/heart.png"><img class="heart" src="images/heart.png"></span>
</div>
<canvas id="gc" width="400" height="400"></canvas>
</div>
</div>
`;
// confirm("Click any key to continue.Use direction keys to move the snake");
var dx = dy = 0;
var livesLeft = 3;
var score = 0;
var buttonClicked = false;
var canv = document.getElementById("gc");
var px = py = 10;
var ax = ay = 15;
var trail = [];
var tail = 5;
const game = () => {
//to start playing the music as player press the arrow keys.
if (buttonClicked === true) {
gameMusic.play();
}
else {
gameMusic.pause();
}
const resetAllValues = () => {
dx = dy = 0;
px = 10;
py = 10;
ax = ay = 15;
trail = [];
tail = 5;
livesLeft = 3;
score = 0;
buttonClicked = false;
}
const scoreBoardUpdate = () => {
if (score > highScoreVal) {
if (youMadeTopScore) {
topScore.play();//sound when topScore is achieved.
youMadeTopScore = false;
}
//using localstorage for storing highscore.
localStorage.setItem('highScore', score);
highScoreVal = localStorage.getItem('highScore');
}
document.getElementById("score").innerHTML = `Score : ${score}`;
document.getElementById("highScore").innerHTML = `Top Score : ${highScoreVal}`;
switch (livesLeft) {
case 1:
document.getElementById("lives").innerHTML = 'Lives : <img class="heart" src="images/heart.png">';
break;
case 2:
document.getElementById("lives").innerHTML = 'Lives : <img class="heart" src="images/heart.png"><img class="heart" src="images/heart.png">';
break;
case 3:
document.getElementById("lives").innerHTML = 'Lives : <img class="heart" src="images/heart.png"><img class="heart" src="images/heart.png"><img class="heart" src="images/heart.png">';
break;
default:
document.getElementById("lives").innerHTML = 'Lives :';
}
}
px += dx;
py += dy;
ctx.fillStyle = "black";
ctx.fillRect(0, 0, canv.width, canv.height);
//NOTE: Always keep in mind that coordinates are with respect to the top-left of the rectangle box(snake's head).
if (px < 0) {
px = 19; //so that it directly jump to the right most side without stucking out of the screen.
}
if (px > 19) { //here it will work when px==20 as 20 will come just after 19.
px = 0;
}
if (py < 0) {
py = 19;
}
if (py > 19) {
py = 0;
}
ctx.fillStyle = "limegreen";
for (var i = 0; i < trail.length; i++) {
ctx.fillRect(trail[i].x * 20, trail[i].y * 20, 18, 18);
if (trail[i].x === px && trail[i].y === py) {
if (buttonClicked === true) { //this is done to start decreasing lives only after movement of snakes starts.
livesLeft -= 1;
hitTail.play();
scoreBoardUpdate();
}
tail = 5;
}
}
if (livesLeft === 0) {
gameMusic.pause();
gameMusic.currentTime = 0;
if (confirm("Game Over... You have eaten your tail!!\nClick OK to play again.\nCancel to return to main menu") === false) {
document.location.href = "/snakeawake";
}
else {
resetAllValues();//reset all values to start the game again.
scoreBoardUpdate();
}
}
trail.push({ x: px, y: py });
while (trail.length > tail) {
trail.shift();
}
if (px === ax && py === ay) {
foodEaten.play();
score += 5;
scoreBoardUpdate();
tail++;
ax = Math.floor(Math.random() * 20);
ay = Math.floor(Math.random() * 20);
}
ctx.fillStyle = "red";
ctx.fillRect(ax * 20, ay * 20, 18, 18);
}
const keyPush = (evt) => {
switch (evt.keyCode) {
case 37:
snakeMove.play();
buttonClicked = true;
dx = -1;
dy = 0;
break;
case 38:
snakeMove.play();
buttonClicked = true;
dy = -1; dx = 0;
break;
case 39:
snakeMove.play();
buttonClicked = true;
dx = 1; dy = 0;
break;
case 40:
snakeMove.play();
buttonClicked = true;
dy = 1;
dx = 0;
break;
}
}
ctx = canv.getContext("2d");
document.addEventListener("keydown", keyPush);
setInterval(game, 1000 / 15);
}
</script>
</html>