-
Notifications
You must be signed in to change notification settings - Fork 0
/
jav.js
105 lines (97 loc) · 3.5 KB
/
jav.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
$(function() {
var playing = false;
var score;
var trialsLeft;
var step;
var step2;
var action;
var action2;
var fruits = ['apple', 'banana', 'cherry', 'grapes', 'mango', 'orange', 'lemon', 'guava', 'watermelon', 'pears', 'pineapple', 'strawberry'];
$("#startbut").click(function() {
if (playing) {
location.reload();
} else {
playing = true;
score = 0;
$("#scorevalue").html(score);
$("#trialsleft").show();
trialsLeft = 3;
addHearts();
$("#gameover").hide();
$("#startbut").html("Reset Game");
startAction();
startAction2();
}
});
$("#fruit1").mouseover(function() {
score++;
$("#scorevalue").html(score);
document.getElementById("slicesound").play();
clearInterval(action);
$("#fruit1").hide("explode", { pieces: 3 }, 500);
setTimeout(startAction, 1000);
});
$("#bombb").mouseover(function() {
score--;
$("#scorevalue").html(score);
document.getElementById("bombsound").play();
clearInterval(action2);
$("#bombb").hide("explode", { pieces: 3 }, 500);
setTimeout(startAction2, 1000);
});
function addHearts() {
$("#trialsleft").empty();
for (var i = 0; i < trialsLeft; i++) {
$("#trialsleft").append('<img src="heart2-removebg-preview.png" class="life">');
}
}
function startAction2() {
if (Math.random() > 0.3) {
$("#bombb").show();
step2 = 3 + Math.round(3 * Math.random());
$("#bombb").css({ 'left': Math.round(1200 * Math.random()), 'top': -50 });
action2 = setInterval(function() {
$("#bombb").css('top', $("#bombb").position().top + step2);
if ($("#bombb").position().top > $("#maincontainer2").height()) {
// $("#bombb").hide();
clearInterval(action2);
startAction2();
}
}, 22);
}
}
function startAction() {
$("#fruit1").show();
chooseFruit();
$("#fruit1").css({ 'left': Math.round(1200 * Math.random()), 'top': -50 });
step = 4+ Math.round(5 * Math.random());
action = setInterval(function() {
$("#fruit1").css('top', $("#fruit1").position().top + step);
if ($("#fruit1").position().top > $("#maincontainer2").height()) {
if (trialsLeft > 1) {
trialsLeft--;
addHearts();
// $("#fruit1").hide();
clearInterval(action);
startAction();
} else {
playing = false;
$("#startbut").html("Start Game");
$("#gameover").html('<p>Game Over!</p><p>Your score is ' + score + '</p>');
$("#gameover").show();
$("#trialsleft").hide();
stopAction();
}
}
}, 20);
}
function chooseFruit() {
$("#fruit1").attr('src', 'images/' + fruits[Math.floor(Math.random() * fruits.length)] + '.png');
}
function stopAction() {
clearInterval(action);
clearInterval(action2);
$("#fruit1").hide();
$("#bombb").hide();
}
});