-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.js
134 lines (100 loc) · 3.55 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
//jquery.js
var playing = false;
var score;
var trialsLeft;
var step;
var action; //used for setInterval
var fruits = ['apple', 'banana', 'cherries', 'grapes', 'mango', 'orange', 'peach', 'pear', 'watermelon'];
$(function(){
//click on start reset button
$("#startreset").click(function(){
//we are playing
if(playing == true){
//reload page
location.reload();
}else{
//we are not playing
playing = true; //game initiated
//set score to 0
score = 0; //set score to 0
$("#scorevalue").html(score);
//show trials left
$("#trialsLeft").show();
trialsLeft = 3;
addHearts();
//hide game over box
$("#gameOver").hide();
//change button text to reset game
$("#startreset").html("Reset Game");
//start sending fruits
startAction();
}
});
//slice a fruit
$("#fruit1").mouseover(function(){
score++;
$("#scorevalue").html(score); //update score
// document.getElementById("slicesound").play();
$("#slicesound")[0].play();//play sound
//stop fruit
clearInterval(action);
//hide fruit
$("#fruit1").hide("explode", 500); //slice fruit
//send new fruit
setTimeout(startAction, 800);
});
//functions
//fill trialLeft box with hearts
function addHearts(){
$("#trialsLeft").empty();
for(i = 0; i < trialsLeft; i++){
$("#trialsLeft").append('<img src="images/heart.png" class="life">');
}
}
//start sending fruits
function startAction(){
//generate a fruit
$("#fruit1").show();
chooseFruit(); //choose a random fruit
$("#fruit1").css({'left' : Math.round(550*Math.random()), 'top' : -50}); //random position
//generate a random step
step = 1+ Math.round(5*Math.random()); // change step
// Move fruit down by one step every 10ms
action = setInterval(function(){
//move fruit by one step
$("#fruit1").css('top', $("#fruit1").position().top + step);
//check if the fruit is too low
if($("#fruit1").position().top > $("#fruitsContainer").height()){
//check if we have trials left
if(trialsLeft > 1 ){
//generate a fruit
$("#fruit1").show();
chooseFruit(); //choose a random fruit
$("#fruit1").css({'left' : Math.round(550*Math.random()), 'top' : -50}); //random position
//generate a random step
step = 1+ Math.round(5*Math.random()); // change step
//reduce trials by one
trialsLeft --;
//populate trialsLeft box
addHearts();
}else{ // game over
playing = false; //we are not playing anymore
$("#startreset").html("Start Game"); // change 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' , 'images/' + fruits[Math.round(8*Math.random())] +'.png');
}
//Stop dropping fruits
function stopAction(){
clearInterval(action);
$("#fruit1").hide();
}
});