-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.js
116 lines (95 loc) · 3.5 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
let playing = false;
let score;
let trialsLeft;
let action;
let step;
let fruits = ['apple','orange','peach','banana','grapes','cherries','pear','pineapple','watermelon'];
$(function(){
$("#startreset").click(function(){
if(playing==true){
//if we are playing then this method relodes the page
location.reload();
}else{
playing = true;
//when we start playing it sets the score to 0
score = 0;
$("#scorevalue").html(score);
//shows how many trials Left
$("#trialsLeft").show();
trialsLeft = 3;
addHearts();
//hide the gameOver box after a game
$("#gameOver").hide();
//changes the start button text to Reset Game
$("#startreset").html("Reset Game");
//starts sending fruits
startAction();
}
});
$("#fruit1").mouseover(function(){
score+=10;
//Updating the score by 10....
$("#scorevalue").html(score);
//document.getElementById("slicesound").play();
$("#slicesound")[0].play();
//stop the fruit after slice
clearInterval(action);
//shows slice animation
$("#fruit1").hide("explode", 3);
//send new fruit
setTimeout(startAction, 3);
//startAction();
});
//functions
function addHearts(){
$("#trialsLeft").empty();
for(i = 0; i< trialsLeft; i++){
$("#trialsLeft").append('<img src="img/heart.png" class="life">');
}
}
//start sending fruits
function startAction(){
$("#fruit1").show();
chooseFruit();
$("#fruit1").css({'left' : Math.round(550*Math.random()), 'top' : -50});//generate random position for fruits
//generate random step
step = 1 + Math.round(5*Math.random());
//moving fruits down in steps every 10ms
action = setInterval(function(){
$("#fruit1").css('top', $("#fruit1").position().top + step);
//checking if the fruit position is too low
if( $("#fruit1").position().top > $("#fruitsContainer").height()){
//check if we have any life left
if(trialsLeft > 1){
$("#fruit1").show();
chooseFruit();
$("#fruit1").css({'left' : Math.round(550*Math.random()), 'top' : -50});//generate random position for fruits
//generate random step
step = 1 + Math.round(5*Math.random());
//reduce trial by one
trialsLeft --;
//add heart
addHearts();
}else{
//game Over
playing = false;
$("#startreset").html("Start Game");//changing button to start game
$("#gameOver").show();
$("#gameOver").html('<p>Game Over!</p><p>Your Score is '+score+'</p>');
$("#trialsLeft").hide();
stopAction();
}
}
},10);
}
//generate a random fruit
function chooseFruit()
{
$("#fruit1").attr('src','img/' + fruits[Math.round(8*Math.random())] + '.png');
}
//stops the action or we can say dropping fruits...
function stopAction(){
clearInterval(action);
$("fruit1").hide();
}
});