-
Notifications
You must be signed in to change notification settings - Fork 681
Expand file tree
/
Copy pathmain.js
More file actions
67 lines (58 loc) · 2.28 KB
/
main.js
File metadata and controls
67 lines (58 loc) · 2.28 KB
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
class ColorGame {
constructor() {
this.colors = [
'#FF0000', '#00FF00', '#0000FF', '#FFFF00',
'#FF00FF', '#00FFFF', '#FFA500', '#800080',
'#008000', '#800000', '#008080', '#000080'
];
this.score = 0;
this.targetColor = '';
this.optionsContainer = document.querySelector('.options-container');
this.colorBox = document.querySelector('.box-color');
this.gameStatus = document.querySelector('.game-status');
this.scoreElement = document.querySelector('.score');
this.newGameButton = document.querySelector('.new-game-btn');
this.newGameButton.addEventListener('click', () => this.resetGame());
this.startNewGame();
}
resetGame() {
this.score = 0;
this.updateScore();
this.startNewGame();
}
updateScore() {
this.scoreElement.textContent = `Score: ${this.score}`;
}
startNewGame() {
this.gameStatus.textContent = '';
this.gameStatus.className = 'game-status';
this.optionsContainer.innerHTML = '';
const shuffledColors = [...this.colors]
.sort(() => Math.random() - 0.5)
.slice(0, 6);
this.targetColor = shuffledColors[Math.floor(Math.random() * 6)];
this.colorBox.style.backgroundColor = this.targetColor;
shuffledColors.forEach(color => {
const button = document.createElement('button');
button.className = 'color-option';
button.setAttribute('data-testid', 'colorOption');
button.style.backgroundColor = color;
button.addEventListener('click', () => this.checkGuess(color));
this.optionsContainer.appendChild(button);
});
}
checkGuess(color) {
if (color === this.targetColor) {
this.score++;
this.updateScore();
this.gameStatus.textContent = 'Correct!';
this.gameStatus.className = 'game-status correct-animation';
setTimeout(() => this.startNewGame(), 1000);
} else {
this.gameStatus.textContent = 'Wrong!';
this.gameStatus.className = 'game-status wrong-animation';
setTimeout(() => this.resetGame(), 1000);
}
}
}
new ColorGame();