-
Notifications
You must be signed in to change notification settings - Fork 0
/
addition.html
180 lines (157 loc) · 5.67 KB
/
addition.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
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
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Jeu d'Addition</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
background-color: #ffe6f2;
}
#game {
background-color: #ffccff;
border: 1px solid #ff99cc;
border-radius: 10px;
padding: 20px;
display: inline-block;
margin-top: 50px;
box-shadow: 0 0 20px rgba(255, 105, 180, 0.5);
}
#options button {
margin: 5px;
padding: 10px 20px;
font-size: 18px;
cursor: pointer;
border: none;
border-radius: 5px;
background-color: #ff66b3;
color: white;
transition: background-color 0.3s;
}
#options button:hover {
background-color: #ff3399;
}
#question {
font-size: 24px;
font-weight: bold;
margin-bottom: 20px;
color: #ff0080;
}
#score {
margin-top: 20px;
}
#goodScore {
color: green;
}
#badScore {
color: red;
}
#config {
margin-bottom: 20px;
}
#celebration {
display: none;
margin-top: 20px;
}
#celebration img {
width: 150px;
height: auto;
animation: celebrate 1s infinite;
}
@keyframes celebrate {
0% { transform: scale(1); }
50% { transform: scale(1.5); }
100% { transform: scale(1); }
}
.hidden {
display: none;
}
</style>
</head>
<body>
<div id="game">
<h1>Jeu d'Addition</h1>
<div id="config">
<label for="maxValue">Chiffre maximum: </label>
<input type="number" id="maxValue" value="20" min="1" max="100">
<button onclick="startGame()">Démarrer</button>
</div>
<div id="question"></div>
<div id="options"></div>
<div id="score">
<p>Bons points: <span id="goodScore">0</span></p>
<p>Mauvais points: <span id="badScore">0</span></p>
</div>
<div id="celebration" class="hidden">
<img id="rewardImage" src="" alt="Récompense">
</div>
</div>
<script>
let goodScore = 0;
let badScore = 0;
const rewardImages = [
'https://media1.autourdugateau.fr/13412/impression-alimentaire-danseuse-etoile.jpg',
'https://media.istockphoto.com/id/1364557568/fr/vectoriel/cartoon-belle-t%C3%AAte-de-licorne-est-sur-le-nuage-avec-de-belles-fleurs.jpg?s=612x612&w=0&k=20&c=AMHehlvaLDNXNRcpa9s7I_4I84sVcX8wrw7CD6NMOxs=',
'https://www.color-stickers.com/1906-thickbox_default/sticker-ocean-sirene-rose.jpg',
'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRML2tzDdcZr7lKkQ5NN8pjSOsDeHLptryUiQ&s'
];
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function generateQuestion() {
const maxVal = parseInt(document.getElementById('maxValue').value);
const num1 = getRandomInt(1, maxVal);
const num2 = getRandomInt(1, maxVal);
const correctAnswer = num1 + num2;
let wrongAnswer1 = correctAnswer;
let wrongAnswer2 = correctAnswer;
while (wrongAnswer1 === correctAnswer) {
wrongAnswer1 = correctAnswer + getRandomInt(1, 10);
}
while (wrongAnswer2 === correctAnswer || wrongAnswer2 === wrongAnswer1 || wrongAnswer2 < 0) {
wrongAnswer2 = correctAnswer - getRandomInt(1, 10);
}
const answers = [correctAnswer, wrongAnswer1, wrongAnswer2].sort(() => Math.random() - 0.5);
document.getElementById('question').innerText = `${num1} + ${num2} = ?`;
const optionsDiv = document.getElementById('options');
optionsDiv.innerHTML = '';
answers.forEach(answer => {
const button = document.createElement('button');
button.innerText = answer;
button.addEventListener('click', () => checkAnswer(answer, correctAnswer));
optionsDiv.appendChild(button);
});
}
function checkAnswer(selectedAnswer, correctAnswer) {
if (selectedAnswer === correctAnswer) {
goodScore++;
document.getElementById('goodScore').innerText = goodScore;
if (goodScore % 10 === 0) {
showCelebration();
}
} else {
badScore++;
document.getElementById('badScore').innerText = badScore;
}
generateQuestion();
}
function startGame() {
document.getElementById('config').style.display = 'none';
generateQuestion();
}
function showCelebration() {
const randomImage = rewardImages[Math.floor(Math.random() * rewardImages.length)];
document.getElementById('rewardImage').src = randomImage;
const celebration = document.getElementById('celebration');
celebration.classList.remove('hidden');
celebration.style.display = 'block';
setTimeout(() => {
celebration.classList.add('hidden');
celebration.style.display = 'none';
}, 5000);
}
</script>
</body>
</html>