-
Notifications
You must be signed in to change notification settings - Fork 1
/
jquery.js
124 lines (103 loc) · 2.99 KB
/
jquery.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
var isPlaying=false;
var score =0;
var lives;
var dropSpeed=1;
var action;
var highScore=0;
var fruits = ['apple', 'banana', 'grapes', 'mango', 'orange', 'peach', 'pear', 'pineapple','tomato','watermelon'];
function getHighScore() {
var dbRef = db.ref().child("scores");
dbRef.on('value', (snapshot) => {
const data = snapshot.val();
if(data!=null){
highScore=data;
}
});
}
function showHighScore(){
window.alert("High Score: "+highScore);
}
$(function(){
getHighScore();
$("#highScore").click(function(){
showHighScore();
});
$("#highScore1").click(function(){
showHighScore();
});
$("#start").click(function(){
if(isPlaying==true){
location.reload();
}
else{
isPlaying=true;
score=0;
$("#value").html(score);
$("#menubar").hide();
$("#liferem").css('display','flex');
$("#container").css({'display':'flex'});
lives=3;
addHeart();
startFruits();
}
});
$("#restart").click(function(){
location.reload();
});
});
$("#fruit").mouseover(cut);
function addHeart(){
$("#life").empty();
for(i=0;i<lives;i++){
$("#life").append(`<img src="images/heart.png" class="heart">`);
}
}
function startFruits(){
chooseFruit();
$("#fruit").css({'left' : Math.round(($("#container").width()-350)*Math.random())+200, 'top' : -50});
$("#fruit").css({'display':'flex'});
if(dropSpeed<=13){
dropSpeed+=1;
}
console.log("dropSpeed", dropSpeed)
action = setInterval(function(){
$("#fruit").css('top', $("#fruit").position().top + dropSpeed);
if($("#fruit").position().top > $("#container").height()){
if(lives > 1 ){
$("#fruit").css({'display':'flex'});
chooseFruit();
$("#fruit").css({'left' : Math.round(($("#container").width()-350)*Math.random())+200, 'top' : -50});
lives-=1;
addHeart();
}else{
playing = false;
$("#liferem").css('display','none');
$("#fsc").text(score);
lives-=1;
addHeart();
$("#endgame").show();
stopAction();
}
}
},10)
}
function chooseFruit(){
$("#fruit").attr('src' , 'images/' + fruits[Math.round(9*Math.random())] +'.png');
}
function stopAction(){
if(score>highScore){
highScore=score;
db.ref().child("scores").set(highScore);
}
clearInterval(action);
$("#fruit").hide();
}
function cut(){
score++;
$("#value").html(score);
$("#slicesound")[0].play();
$("#fruit").hide("explode", 500);
$("#fruit").css({'display':'flex'});
clearInterval(action);
setTimeout(startFruits, 800);
}