-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfruit.js
101 lines (79 loc) · 3.57 KB
/
fruit.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
<html>
<head>
<title>Fruit Zen</title>
<link rel="stylesheet" href="style.css">
<link href="https://fonts.googleapis.com/css2?family=Patrick+Hand+SC&display=swap" rel="stylesheet">
</head>
<body>
<h1 id='heading'></h1>
<div class="container">
<div class="slot" id="one"></div>
<div class="slot" id="two"></div>
<div class="slot" id="three"></div>
<button id="btn" onclick="stop();">Stop</button>
<audio src="won.mp3" id="winningSound"></audio>
<audio src="music.mp3" id="backgroundMusic"></audio>
<button id="soundBtn"
onclick="document.getElementById('backgroundMusic').play();">
Play Sound
</button>
<button id="musicBtn" onclick="stopMusic();">Stop Music</button>
<div id="gameOver">
</div>
</div>
<script>
var name = prompt("Enter your name");
document.getElementById('heading').innerHTML = name + "'s Fruit Zen";
var things = [ "banana.png",
"pineapple.png",
"cherry.png",
"strawberry.png",
"apple.png",
"pear.png",
"watermelon.png",
"orange.png",
"juice.png"
];
var total = things.length - 1;
function randomNumber() {
var num = Math.round(Math.random()*total);
return num;
}
function randomize() {
var num1 = randomNumber();
var num2 = randomNumber();
var num3 = randomNumber();
var html1 = "<img src=" + things[num1] + ">";
document.getElementById("one").innerHTML = html1;
var html2 = "<img src=" + things[num2] + ">";
document.getElementById("two").innerHTML = html2;
var html3 = "<img src=" + things[num3] + ">";
document.getElementById("three").innerHTML = html3;
}
var interval = setInterval(randomize, 500);
function stop(){
document.getElementById("backgroundMusic").pause();
clearInterval(interval); // this stops the setInterval() function from calling the randomize() function repeatedly...
var one = document.getElementById("one").innerHTML;
var two = document.getElementById("two").innerHTML;
var three = document.getElementById("three").innerHTML;
if(one == two && two == three){
document.getElementById("winningSound").play();
document.getElementById("heading").innerHTML = "You Won!!";
}else{
document.getElementById("heading").innerHTML = "Better luck next time!";
}
document.getElementById("gameOver").innerHTML = "<button id='btn' onclick='restart();'>Try again</button>"; // draws a button in the div...
}
function restart(){
document.getElementById("backgroundMusic").play();
document.getElementById("gameOver").innerHTML = " ";
document.getElementById('heading').innerHTML = name + "'s Fruit Zen";
interval = setInterval(randomize, 500);
}
function stopMusic(){
document.getElementById("backgroundMusic").pause();
}
</script>
</body>
</html>