forked from suryanshsk/Python-Voice-Assistant-Suryanshsk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Quiz_Game
273 lines (240 loc) · 7.14 KB
/
Quiz_Game
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#####html#########
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Quiz Application</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container">
<h1>Quiz Application</h1>
<div id="quiz-container" class="hidden">
<div id="question"></div>
<div id="options"></div>
<button id="next-button" class="hidden">Next Question</button>
</div>
<button id="start-button">Start Quiz</button>
<div id="result" class="hidden"></div>
<button id="new-quiz-button" class="hidden">Start New Quiz</button>
</div>
<script src="script.js"></script>
</body>
</html>
########css#######
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 20px;
}
.container {
max-width: 600px;
margin: auto;
padding: 20px;
background: white;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
border-radius: 8px;
}
h1 {
text-align: center;
color: #333;
}
#question {
font-size: 1.2em;
margin-bottom: 20px;
}
#options {
margin-bottom: 20px;
}
.option {
display: block;
padding: 10px;
margin: 10px 0;
background: #e7e7e7;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background 0.3s;
}
.option:hover {
background: #d0d0d0;
}
.correct {
background-color: #4caf50; /* Green color for correct answer */
color: white;
}
.incorrect {
background-color: #f44336; /* Red color for incorrect answer */
color: white;
}
button {
padding: 10px 15px;
background: #007BFF;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background 0.3s;
}
button:hover {
background: #0056b3;
}
.hidden {
display: none;
}
.popup {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: rgba(0, 0, 0, 0.8);
color: white;
padding: 15px 20px;
border-radius: 5px;
z-index: 10;
opacity: 0;
transition: opacity 0.3s ease;
text-align: center;
}
.popup.show {
opacity: 1; /* Show popup */
}
#######js##################
const questions = [
{
question: "What is the capital of France?",
options: ["Berlin", "Madrid", "Paris", "Rome"],
answer: 2 // Correct answer: Paris
},
{
question: "What is 2 + 2?",
options: ["3", "4", "5", "6"],
answer: 1 // Correct answer: 4
},
{
question: "In which year did the Titanic sink?",
options: ["1905", "1912", "1918", "1925"],
answer: 1 // Correct answer: 1912
},
{
question: "What is the largest planet in our solar system?",
options: ["Earth", "Mars", "Jupiter", "Saturn"],
answer: 2 // Correct answer: Jupiter
},
{
question: "What is the chemical symbol for gold?",
options: ["Au", "Ag", "Fe", "Pb"],
answer: 0 // Correct answer: Au
},
{
question: "Which language is primarily spoken in Brazil?",
options: ["Spanish", "Portuguese", "English", "French"],
answer: 1 // Correct answer: Portuguese
},
{
question: "Who wrote 'Romeo and Juliet'?",
options: ["Charles Dickens", "Mark Twain", "William Shakespeare", "Jane Austen"],
answer: 2 // Correct answer: William Shakespeare
},
{
question: "What is the hardest natural substance on Earth?",
options: ["Gold", "Iron", "Diamond", "Quartz"],
answer: 2 // Correct answer: Diamond
},
{
question: "What is the largest mammal in the world?",
options: ["Elephant", "Blue Whale", "Great White Shark", "Giraffe"],
answer: 1 // Correct answer: Blue Whale
},
{
question: "What element does 'O' represent on the periodic table?",
options: ["Osmium", "Oxygen", "Gold", "Silver"],
answer: 1 // Correct answer: Oxygen
}
];
let score = 0;
let questionIndex = 0;
const quizContainer = document.getElementById('quiz-container');
const questionElement = document.getElementById('question');
const optionsElement = document.getElementById('options');
const nextButton = document.getElementById('next-button');
const startButton = document.getElementById('start-button');
const newQuizButton = document.getElementById('new-quiz-button');
const resultElement = document.getElementById('result');
startButton.addEventListener('click', startGame);
nextButton.addEventListener('click', loadQuestion);
newQuizButton.addEventListener('click', resetGame);
function startGame() {
score = 0;
questionIndex = 0;
showQuiz();
loadQuestion();
}
function showQuiz() {
startButton.classList.add('hidden');
quizContainer.classList.remove('hidden');
resultElement.classList.add('hidden');
newQuizButton.classList.add('hidden');
}
function loadQuestion() {
if (questionIndex < questions.length) {
const currentQuestion = questions[questionIndex];
questionElement.textContent = currentQuestion.question;
optionsElement.innerHTML = '';
currentQuestion.options.forEach((option, index) => {
const button = document.createElement('button');
button.classList.add('option');
button.textContent = option;
button.addEventListener('click', () => checkAnswer(index));
optionsElement.appendChild(button);
});
nextButton.classList.add('hidden');
} else {
displayResult();
}
}
function checkAnswer(selectedIndex) {
const currentQuestion = questions[questionIndex];
// Highlight the selected answer
const options = optionsElement.children;
// Clear previous highlights
for (let i = 0; i < options.length; i++) {
options[i].classList.remove('correct', 'incorrect');
}
// Check if the selected answer is correct
if (selectedIndex === currentQuestion.answer) {
options[selectedIndex].classList.add('correct'); // Correct answer turns green
score++;
showPopup("Correct!"); // Show popup for correct answer
} else {
options[selectedIndex].classList.add('incorrect'); // Wrong answer turns red
options[currentQuestion.answer].classList.add('correct'); // Correct answer turns green
showPopup(`Incorrect! The correct answer is: ${currentQuestion.options[currentQuestion.answer]}`); // Show popup for incorrect answer
}
// Proceed to the next question after a delay
questionIndex++;
setTimeout(() => {
loadQuestion();
}, 2000); // Delay before loading the next question
}
function displayResult() {
quizContainer.classList.add('hidden');
resultElement.textContent = `You scored ${score} out of ${questions.length}!`;
resultElement.classList.remove('hidden');
newQuizButton.classList.remove('hidden');
}
function resetGame() {
startGame();
}
function showPopup(message) {
const popup = document.createElement("div");
popup.classList.add("popup");
popup.textContent = message;
document.body.appendChild(popup);
// Remove the popup after 1.5 seconds
setTimeout(() => {
popup.remove();
}, 1500);
}