forked from AdityaChaudhary3/Lost-Treasure
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
treasure.js
136 lines (96 loc) · 3.57 KB
/
treasure.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
const box1 = document.getElementById("1");
const box2 = document.getElementById("2");
const box3 = document.getElementById("3");
const box4 = document.getElementById("4");
const box5 = document.getElementById("5");
const box6 = document.getElementById("6");
const box7 = document.getElementById("7");
const box8 = document.getElementById("8");
const box9 = document.getElementById("9");
const lostTreasure = document.querySelector("h1");
const correct = document.getElementById("correct");
const wrong = document.getElementById("wrong");
const bonfire = new Audio("assets/bonfire.mp3"); //background music
bonfire.volume = 0.6;
const ocean = new Audio("assets/ocean.mp3"); //ocean sounds
ocean.volume = 0.8;
const pirate = new Audio("assets/pirate.mp3");
let treasure = randomRange(1,9); //this is where the treasure is hidden
let correctCount = 0;
let wrongCount = 0;
correct.innerHTML = correctCount;
wrong.innerHTML = wrongCount;
box1.onclick = function() { selection(1) };
box2.onclick = function() { selection(2) };
box3.onclick = function() { selection(3) };
box4.onclick = function() { selection(4) };
box5.onclick = function() { selection(5) };
box6.onclick = function() { selection(6) };
box7.onclick = function() { selection(7) };
box8.onclick = function() { selection(8) };
box9.onclick = function() { selection(9) };
lostTreasure.onclick = function() {
if(pirate.paused) {
pirate.play();
}
}
//Returns a random number within a chosen range
function randomRange(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
//Math.random() returns a random decimal between 0 - 0.99
//Math.floor() rounds down to the nearest whole number e.i. 10 = 0 - 9
}
//compares the user's treasure selection to the randomly chosen selection
function selection(box) {
let arrg;
let lose = document.querySelector(".lose");
let win = document.querySelector(".win");
let open = new Audio("assets/open-chest.mp3");
let treasureChest = new Audio("assets/treasure-chest.mp3");
//updates guessing score
function score(guess) {
if (guess == "win") {
correctCount++;
correct.innerHTML = correctCount;
} else if (guess == "lose") {
wrongCount++;
wrong.innerHTML = wrongCount;
}
}
if (box == treasure) {
bonfire.volume = 0.3;
treasureChest.play(); //plays sound
win.style.visibility = "visible"; //shows message
score("win"); //updates score
setTimeout(function() {
bonfire.volume = 0.6;
win.style.visibility = "hidden";
}, 10000); //Gives time to celebrate, hides again
treasure = randomRange(1,9); //after win asigns a different random number to treasure
} else {
open.play(); //plays sound
lose.style.visibility = "visible"; //shows message
if(ocean.paused) {
ocean.play();
if(pirate.paused) {
arrg = randomRange(0,1); //won't play everytime
if(arrg == 1) {
setTimeout(function(){
pirate.play();
}, 600); //let's other sounds play first
}
}
}
setTimeout(function() {
score("lose");
}, 500 ); //updates score with suspense
setTimeout(function() {
lose.style.visibility = "hidden"; //hides again
}, 1750 ); //waits 1.75 seconds
}
}
window.onclick = function() {
if(bonfire.paused) {
bonfire.play(); //plays music
}
};