-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.js
154 lines (114 loc) · 4.18 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
var playing= false;
var score;
var trialsLeft;
var step;
var fruits = ['apple','banana','cherries','grapes','mango','orange','peach','pear','watermelon'];
$(function(){
//click on start reset button
$("#startreset").click(function(){
//are we playing
if(playing == true){
//yes
//reload page
location.reload();
}else{
window.console.log("game initiated success");
//We are not playing
playing = true;//Game initiated
score = 0;// set score to zero
$("#scorevalue").html(score);
// show trials left
$("#trialsLeft").show();
trialsLeft =3;
addHearts();
$("#gameOver").hide();
// change button text to reset game2
$("#startreset").html("Reset Game");
startAction();
}
}
);
$("#fruit1").mouseover(function(){
score++;
$("#scoreValue").html(score);
// document.getElementById("sliceSound").play();
$("#sliceSound")[0].play();//play sound
//stop fruit
clearInterval(action);
//hide fruit
$("#fruit1").hide("explode",500);//slicing the fruit
//send new fruit
setTimeout(startAction,600);
});
//slice a fruit
//play sound
//explode fruit
function addHearts(){
$("#trialsLeft").empty();
for(i=0; i<trialsLeft;i++){
$("#trialsLeft").append('<img src="images/heart.png" class="life">');
}
}
//start sending fruits
function startAction(){
//1create a random fruit
// define random step
//2move fruit down one step every 30 sec
//is fruit too low
//no > repeat 2.
// yes > any trials left?
// yes: repeat a
//no: show gameover, button text: start game
//generate a fruit
$("#fruit1").show();
chooseFruit(); //choose a random fruit
$("#fruit1").css({
'left': Math.round(550*Math.random()),
'top': -50
//locate fruit in a random position
});
//Generate a random step
step = Math.round(5*Math.random())+1;//change step
//Move fruit by one step every 10ms
// Move one step every 10ms
action = setInterval(function(){
$("#fruit1").css("top",$("#fruit1").position().top +step);
// check if fruit is too low
if($("#fruit1").position().top > $("#fruitsContainer").height()){
if(trialsLeft > 1){
//generate a fruit
$("#fruit1").show();
chooseFruit(); //choose a random fruit
$("#fruit1").css({
'left': Math.round(550*Math.random()),
'top': -50
//locate fruit in a random position
});
//Generate a random step
step = Math.round(5*Math.random())+1;//change step
//reduce trials by one
trialsLeft--;
//populate trialsleft box
addHearts();
}else{
//gameover
playing= false;//We are not playing anymore
$("#startreset").html("Play Again");//Change button to play again
$("#gameOver").show();
$("#gameOver").html("<p>Game Over</p><P>Your score is "+ score+ "</p>");
$("#trialsLeft").hide();
stopAction();
}
}else{
}
},10);
}
function chooseFruit(){
$("#fruit1").attr('src','images/'+fruits[Math.round(8*Math.random())]+'.png');
}
function stopAction(){
//stop dropping fruits
clearInterval(action);
$("#fruit1").hide();
}
/*For document ready function*/});