-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.js
103 lines (87 loc) · 2.58 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
var playing = false;//initially we are not playing
var score;
var trialsleft;
var fruits = ["apple","cherry","grapes","pine","orange"];
var step;
var action;
$(function(){
$("#startreset").click(function(){
if(playing == true)
{
location.reload();// to reload the page
}
else
{
$("#gameover").hide();
playing = true;
score = 0;
$("#scorevalue").html(score);
$("#trialsleft").show(); // to show the trials left box
trialsleft = 3;
addhearts();
$("#startreset").html("Reset Game");
startaction();
}
});
$("#fruit1").mouseover(function(){
score++;
$("#scorevalue").html(score);
document.getElementById("audio").play();//using jquery will return an array. so use js or for jquery use the first[0]th element
clearInterval(action);
$("#fruit1").hide("explode",500);
setTimeout(startaction,500); //to wait for the animation to get over
});
function addhearts()
{
$("#trialsleft").empty();
for(i=0;i<trialsleft;i++)
{
$("#trialsleft").append('<img src="images/heart.png" class="life">');
}
}
function startaction()
{
$("#fruit1").show();
choosefruit();
//for random horizontal positions
$("#fruit1").css({'left': Math.round(Math.random()*500), 'top': -50});
//generate a random step
step = 1 + Math.round(Math.random()*5);
//move the fruit down by one step every 10ms
action = setInterval(function(){
$("#fruit1").css('top', $("#fruit1").position().top + step);
if($("#fruit1").position().top > $("#fruitcontainer").height())
{
if(trialsleft > 1)
{
$("#fruit1").show();
choosefruit();
//for random horizontal positions
$("#fruit1").css({'left': Math.round(Math.random()*500), 'top': -50});
//generate a random step
step = 1 + Math.round(Math.random()*5);
trialsleft--;
addhearts();
}
else
{
playing = false;
$("#trialsleft").hide();
$("#gameover").show();
$("#gameover").html('<p>Game Over!</p><p>Your score is ' + score +'</p>')
$("#startreset").html("Start Game");
stopaction();
}
}
},10);
}
function choosefruit()
{
$("#fruit1").attr('src','images/'+fruits[Math.round(4*Math.random())]+'.png');
}
function stopaction() // stop dropping the function
{
clearInterval(action);
$("#fruit1").hide();
}
});