-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
147 lines (132 loc) · 4.07 KB
/
script.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
const timeBoxElem = document.querySelector('.timeBox')
const instructionsButtonElem = document.querySelector('.instructionsButton')
const instructionsModalElem = document.querySelector('.modalContainer')
const modalCloseButtonElem = document.querySelector('.closeButton')
const startGameButtonElem = document.querySelector('.startGameButton')
const scoreBoxElem = document.querySelector('.scoreBox')
const displayQuoteElem = document.querySelector('.displayQuote')
const playerChoicesElem = document.querySelector('.playerChoices')
const finalScoreElem = document.querySelector('.finalScore')
const gameOverModalElem = document.querySelector('.gameOverModal')
const playAgainButtonElem = document.querySelector('.playAgainButton')
let films = []
let totalScore = 0
instructionsButtonElem.addEventListener('click', () => {
instructionsModalElem.showModal()
})
modalCloseButtonElem.addEventListener('click', () => {
instructionsModalElem.close()
})
startGameButtonElem.addEventListener('click', () => {
runGame()
startGameButtonElem.style.display = 'none'
timeBoxElem.style.visibility = 'visible'
scoreBoxElem.style.visibility = 'visible'
})
playAgainButtonElem.addEventListener('click', () => {
resetGame()
})
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
let j = Math.floor(Math.random() * (i + 1));
let temp = array[i];
array[i] = array[j];
array[j] = temp;
}
return array
}
const getFilms = (() => {
return fetch('quotes.json')
.then((response) => {
return response.json()
})
.then((filmsList) => {
return filmsList['films']
})
})
const startGame = (() => {
getFilms()
.then((filmsList) => {
films = filmsList
})
})
const playRound = (() => {
films = shuffleArray(films)
const answer = films.pop()
hint = answer['year']
const choices = shuffleArray([answer['title'], films[0]['title'], films[1]['title']])
const quote = document.createElement('p')
quote.textContent = answer['quote']
displayQuoteElem.append(quote)
choices.forEach((item) => {
const choice = document.createElement('button')
choice.classList.add('choice')
choice.textContent = item
choice.addEventListener('click', () => {
if (item === answer['title']) {
choice.style.backgroundColor = 'green'
score()
}
else {
choice.style.backgroundColor = 'red'
}
setTimeout(clearContainer, 500, displayQuoteElem)
setTimeout(clearContainer, 500, playerChoicesElem)
setTimeout(playRound, 500)
})
playerChoicesElem.append(choice)
})
const hintButtonElem = document.createElement('button')
hintButtonElem.classList.add('hintButton')
hintButtonElem.textContent = 'Hint'
hintButtonElem.addEventListener('click', () => {
hintButtonElem.textContent = answer['year']
})
playerChoicesElem.append(hintButtonElem)
})
const clearContainer = (container) => {
container.textContent = ''
}
const runGame = () => {
playRound()
runTimer()
}
const score = (() => {
totalScore ++
scoreBoxElem.textContent = totalScore
if (totalScore % 5 === 0) {
confetti({
particleCount: 100,
spread: 70,
origin: {y: .6}
})
}
})
const runTimer = (() => {
let time = 30
const timer = setInterval(() => {
if (time > 0) {
time --
timeBoxElem.textContent = String(time)
}
else {
clearInterval(timer)
gameFinish()
}
}, 1000)
})
const gameFinish = () => {
finalScoreElem.textContent = `Your score: ${totalScore}`
gameOverModalElem.showModal()
}
const resetGame = () => {
clearContainer(displayQuoteElem)
clearContainer(playerChoicesElem)
totalScore = 0
timeBoxElem.textContent = '30'
scoreBoxElem.textContent = totalScore
startGame()
gameOverModalElem.close()
runGame()
}
startGame()