-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.js
136 lines (102 loc) · 3.71 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
125
126
127
128
129
130
131
132
133
134
135
136
//slice fruit
//play sound and explode fruit
//increase score by 1
var playing = false;
var score = 0;
var hearts;
var step;
var action;
var fruits = ['apple','banana','cherries','grapes','mango','orange','peach','pear','watermelon'];
//click on start-reset
$(document).ready(function(){
$("#startreset").click(function(){
//playing - reload
if(playing){
location.reload();
}
else{
playing=true;
score = 0;
$("#scoreVal").html(score);
$("#lives").show();
hearts=3;
//show "lives"
addhearts();
//change button text to reset game
$("#startreset").html("Reset Game");
//hide game over
$("#gameover").hide();
//start game
startGame();
}
});
function addhearts(){
$("#hearts").empty();
for(i=0; i<hearts; i++){
$("#hearts").append('<img class="heartImage" src="images/LogoMakr_7iifJT.png">');
}
}
$("#fruits1").mouseover(function(){
//inc score
score++;
$("#scoreVal").html(score);
//sound - DO NOT USE JQUERY selector - the audio will be taken as array
document.getElementById("sliceSound").play();
//fruit stop
clearInterval(action);
//hide by animation
$("#fruits1").hide("explode",400); //needjquery ui
//new fruits
setTimeout(startGame,400); //allow hide to complete
});
function startGame(){
$("#fruits1").show();
//1.create a random fruit
chooseFruit();
$("#fruits1").css({
left: Math.floor(510*Math.random()),
top: -50,
});
//define a random step - every new fruit will have a different speed
step = 2 +Math.floor(5*Math.random());
//2.move fruit down 1 step - every 10ms
action = setInterval(function(){
$("#fruits1").css('top',$("#fruits1").position().top + step);
//if too low?
if($("#fruits1").position().top > $("#fruitContainer").height()){
//hearts left?
if(hearts > 1){
$("#fruits1").show();
chooseFruit();
$("#fruits1").css({
left: Math.floor(510*Math.random()),
top: -50,
});
step = 2 +Math.floor(5*Math.random());
//reduce heart
hearts--;
addhearts();
}
//game - over
else{
playing=false;
//reset to start again
$("#startreset").html("Start Game");
//show Game over
$("#gameover").show();
$("#gameover").html("<p>Game Over! </p><p> Your score is "+score+"</p>");
//hide lives
$("#lives").hide();
//stop the process
stopAction();
}
}
},10);
}
function chooseFruit(){ $("#fruits1").attr('src','images/'+fruits[Math.floor(9*Math.random())]+'.png');
}
function stopAction(){
clearInterval(action);
$("#fruits1").hide();
}
});