-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
376 lines (330 loc) · 8.52 KB
/
app.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
const canvas =document.getElementById("gameCanvas");
const ctx=canvas.getContext("2d");
//bulr
const gameContain=document.getElementById("contain");
const sheep = new Image();
sheep.src="asset/sheep2.PNG";
const shspeed =-3;
const shwidth=64; const shheight=64;
const pipe = new Image();
pipe.src="asset/pipe.PNG";
const pipewidth =64;
const pipeheight=512;
const star= new Image();
star.src="asset/star.png";
const moutain= new Image();
moutain.src="asset/moutain.png";
let x=32; let y = 32;
let velo=0;
let acc=0.1;
let pipex=960;
let pipey=canvas.height-160;
let pipegap=90;
let pipex2=640;
let pipey2=canvas.height-160;
let pipegap2=90;
let mx=0; let my=0;
let sx=0; let sy=0;
let mx2=640; let my2=0;
let sx2=640; let sy2=0;
let score=0;
let highscore=0;
let scorediv=document.getElementById('point');
let rebtn = document.getElementById('butn');
let stbtn = document.getElementById('stbtn');
let sound =true;
const openSound = new Audio();
openSound.src="asset/sound.mp3";
let sbtn = document.getElementById('button');
const openSound2 = new Audio();
openSound2.src="asset/sound2.mp3";
//sheep sound
let sheepbtn = document.getElementById('buttonsheep');
const sheepSound = new Audio();
sheepSound.src="asset/sheepAudio.mp3";
//jump sound
const jSound = new Audio();
jSound.src="asset/pop.mp3";
function baa() {
sheepSound.play();
}
sheepbtn .addEventListener('click', baa);
//event
document.body.onkeyup = function(e){
if(e.code=="Space"){
jSound.play();
velo=shspeed;
}
}
function handleScreenClick() {
jSound.play();
velo=shspeed;
}
document.addEventListener('click', handleScreenClick);
function showconsole(){
hidemenu();
reset();
loop();
return;
}
rebtn.addEventListener('click',showconsole);
//score
function updateScore(newScore) {
localStorage.setItem('hscore', newScore);
}
function getScore() {
const score = localStorage.getItem('hscore');
return score ? parseInt(score) : 0;
}
highscore =getScore();
//sound
function playSound() {
var z=Math.floor(Math.random() * 2);
if(z===0){
openSound.play();
openSound.addEventListener('ended', function() {
// When the audio ends, replay it by setting the current time to 0 and calling the play() method again.
this.currentTime = 0;
this.play();
});
}else{
openSound2.play();
openSound2.addEventListener('ended', function() {
// When the audio ends, replay it by setting the current time to 0 and calling the play() method again.
this.currentTime = 0;
this.play();
});
}
}
function stopSound(){
openSound.pause();
openSound.currentTime = 0;
openSound2.pause();
openSound2.currentTime = 0;
}
function sss() {
if(sound){
sound=false;
stopSound();
document.getElementById('soundimg').src="asset/sound2.png";
}else{
sound=true;
playSound();
document.getElementById('soundimg').src="asset/sound1.png"
}
}
sbtn.addEventListener('click', sss);
function plusscore(){
if(x==Math.floor(pipex+pipewidth)){
score++;
}
if(x==Math.floor(pipex2+pipewidth)){
score++;
}
scorediv.innerHTML=score;
}
function hitted(){
const sheepbox={
sx:x,
sy:y,
swidth:shwidth,
sheight:shheight
}
//pipe1
const topPipebox={
px:pipex,
py:pipey-pipegap,
pwidth:pipewidth,
pheight:pipeheight
}
const downPipebox={
px:pipex,
py:pipey+pipegap,
pwidth:pipewidth,
pheight:pipeheight
}
if(sheepbox.sx+sheepbox.swidth-8>topPipebox.px &&
sheepbox.sx+8<topPipebox.px +topPipebox.pwidth&&
sheepbox.sy<topPipebox.py-4){
return true;
}
if(sheepbox.sx+sheepbox.swidth-8>downPipebox.px &&
sheepbox.sx+8<downPipebox.px +downPipebox.pwidth&&
sheepbox.sy+sheepbox.sheight-4>downPipebox.py){
return true;
}
//pipe2
const topPipebox2={
px:pipex2,
py:pipey2-pipegap2,
pwidth:pipewidth,
pheight:pipeheight
}
const downPipebox2={
px:pipex2,
py:pipey2+pipegap2,
pwidth:pipewidth,
pheight:pipeheight
}
if(sheepbox.sx+sheepbox.swidth-8>topPipebox2.px &&
sheepbox.sx+8<topPipebox2.px +topPipebox2.pwidth&&
sheepbox.sy<topPipebox2.py-4){
return true;
}
if(sheepbox.sx+sheepbox.swidth-8>downPipebox2.px &&
sheepbox.sx+8<downPipebox2.px +downPipebox2.pwidth&&
sheepbox.sy+sheepbox.sheight-4>downPipebox2.py){
return true;
}
if(y<-64||y+shheight>canvas.height+32){
return true;
}
return false;
}
//end menu pop-unpop when died
function hidemenu(){
if(sound){playSound();}
document.getElementById("end").style.display="none";
gameContain.classList.remove("bulr");
}
function reset(){
x = 32;
y = 32;
velo=0;
acc=0.1;
pipex=960;
pipey=canvas.height-160;
pipegap=90;
/*
let mx=0; let my=0;
let sx=0; let sy=0;
let mx2=640; let my2=0;
let sx2=640; let sy2=0;
*/
pipex2=640;
pipey2=canvas.height-160;
pipegap2=90;
score=0;
document.body.onkeyup = function(e){
if(e.code=="Space"){
jSound.play();
velo=shspeed;
}
}
document.addEventListener('click', handleScreenClick);
}
function endmenu(){
if(sound){
stopSound();
}
document.getElementById("end").style.display="block";
gameContain.classList.add("bulr");
document.getElementById("endscore").innerHTML=score;
if(highscore<score){
highscore=score;
updateScore(score);
}
document.getElementById("bestscore").innerHTML=highscore;
document.body.onkeyup = function(x){
if(x.code=="KeyR"){
hidemenu();
reset();
loop();
}
}
}
function hidconsole(){
playSound();
document.getElementById("start").style.display="none";
document.body.onkeyup = function(e){
if(e.code=="Space"){
jSound.play();
velo=shspeed;
}
}
loop();
}
function loop(){
//reset
ctx.clearRect(0,0,canvas.width,canvas.height);
scorediv.innerHTML=score;
//drawbg
ctx.drawImage(star,sx,sy);
ctx.drawImage(star,sx2,sy2);
ctx.drawImage(moutain,mx,my);
ctx.drawImage(moutain,mx2,my2);
//drawSheep
ctx.drawImage(sheep,x,y);
//pipe
ctx.drawImage(pipe,pipex,pipey-512-pipegap,pipewidth,pipeheight);
ctx.drawImage(pipe,pipex,pipey+pipegap,pipewidth,pipeheight);
ctx.drawImage(pipe,pipex2,pipey2-512-pipegap2,pipewidth,pipeheight);
ctx.drawImage(pipe,pipex2,pipey2+pipegap2,pipewidth,pipeheight);
//move
pipex-=1.5;
mx-=0.2;
mx2-=0.2;
sx-=0.1;
sx2-=0.1;
if(pipex<-64){
pipex=640;
if(pipegap>=70){pipegap-=1; }
pipey=Math.floor(Math.random() * (canvas.height));
if(pipey<pipegap){
pipey+=pipegap;
}else if(pipey>(canvas.height-pipegap)){
pipey-=pipegap;
}
}
pipex2-=1.5;
if(pipex2<-64){
pipex2=640;
if(pipegap2>=70){ pipegap2-=2;}
pipey2=Math.floor(Math.random() * (canvas.height));
if(pipey2<pipegap2){
pipey2+=pipegap2;
}else if(pipey2>(canvas.height-pipegap2)){
pipey2-=pipegap2;
}
}
if(sx<= -640){
sx=640;
}if(sx2<= -640){
sx2=640;
}
if(mx<=-640){
mx=640;
}if(mx2<= -640){
mx2=640;
}
//collisionCheck
if(hitted()){
endmenu();
return;
}
plusscore();
//sheepFly
velo+=acc;
y+=velo;
//console.log(velo);
if(velo>0){
sheep.src="asset/sheep2.PNG";
}else{
sheep.src="asset/sheep1.PNG";
}
requestAnimationFrame(loop);
}
function soundsheep(){
hidemenu();
reset();
loop();
return;
}
rebtn.addEventListener('click',showconsole);
//st
stbtn.addEventListener('click',hidconsole);
document.body.onkeyup = function(e){
if(e.code=="KeyS"){
hidconsole();
}
}