-
Notifications
You must be signed in to change notification settings - Fork 0
/
gameplay.html
89 lines (79 loc) · 3.12 KB
/
gameplay.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Meta tags -->
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Link to the CSS stylesheet -->
<link rel="stylesheet" href="../static/style.css">
</head>
<body class="gameplay">
<!-- Heading for the gameplay page -->
<h1 id="question-text">LET THE GAMES BEGIN</h1>
<!-- Div to hold the option buttons -->
<div class="options-container">
<!-- Option buttons -->
<button class="option" data-answer="A">Option A</button>
<button class="option" data-answer="B">Option B</button>
</div>
<!-- Another div to hold the option buttons -->
<div class="options-container">
<!-- Option buttons -->
<button class="option" data-answer="C">Option C</button>
<button class="option" data-answer="D">Option D</button>
</div>
<!-- Paragraph to display the number of tries left -->
<p id="tries-left">Tries left: <span id="tries-count">3</span></p>
<!-- Script to handle the gameplay logic -->
<script>
// Define questions from server-side code
let question = {{ questions | tojson | safe }};
let currentIndex = 0;
let tries = 3;
let score = 0;
// Function to display the current question and options
function displayQuestion() {
if (currentIndex < question.length) {
let currentQuestion = question[currentIndex];
document.getElementById('question-text').innerText = currentQuestion[1];
let optionButtons = document.querySelectorAll('.option');
for (let i = 0; i < optionButtons.length; i++) {
optionButtons[i].innerText = currentQuestion[2].split(" ")[i];
}
} else {
redirectToScore();
}
}
// Function to check the user's answer
function checkAnswer(userAnswer) {
let currentQuestion = question[currentIndex];
let correctAnswer = currentQuestion[3].trim();
if (userAnswer.toUpperCase() === correctAnswer.toUpperCase()) {
score +=10;
} else {
tries--;
if (tries <= 0) {
redirectToScore();
return;
}
document.getElementById('tries-count').innerText = tries;
}
currentIndex++;
displayQuestion();
}
// Add event listeners to option buttons
let optionButtons = document.querySelectorAll('.option');
optionButtons.forEach(button => {
button.addEventListener('click', () => {
checkAnswer(button.getAttribute('data-answer'));
});
});
// Function to redirect to score page
function redirectToScore() {
window.location.href = `/scores?score=${score}&tries=${tries}`;
}
// Display the first question
displayQuestion();
</script>
</body>
</html>