forked from ChromeGaming/GameSphere
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
134 lines (109 loc) · 3.54 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
const round = document.getElementById('round');
const simonButtons = document.getElementsByClassName('square');
const startButton = document.getElementById('startButton');
class Simon {
constructor(simonButtons, startButton, round) {
this.round = 0;
this.userPosition = 0;
this.sequence = [];
this.speed = 1000;
this.blockedButtons = true;
this.buttons = Array.from(simonButtons);
this.display = {
startButton,
round
}
this.errorSound = new Audio('./sounds/error.wav');
this.buttonSounds = [
new Audio('./sounds/1.mp3'),
new Audio('./sounds/2.mp3'),
new Audio('./sounds/3.mp3'),
new Audio('./sounds/4.mp3'),
]
}
init() {
this.display.startButton.onclick = () => this.startGame();
}
startGame() {
let totalRound=document.getElementById('total-round').value
this.totalRounds=Number(totalRound)
console.log(totalRound)
this.display.startButton.disabled = true;
this.updateRound(0);
this.userPosition = 0;
this.sequence = this.createSequence();
this.buttons.forEach((element, i) => {
element.classList.remove('winner');
element.onclick = () => this.buttonClick(i);
});
this.showSequence();
}
updateRound(value) {
this.round = value;
this.display.round.textContent = `Round ${this.round}`;
}
createSequence() {
return Array.from({length: this.totalRounds}, () => this.getRandomColor());
}
getRandomColor() {
return Math.floor(Math.random() * 4);
}
buttonClick(value) {
!this.blockedButtons && this.validateChosenColor(value);
}
validateChosenColor(value) {
if(this.sequence[this.userPosition] === value) {
this.buttonSounds[value].play();
if(this.round === this.userPosition) {
this.updateRound(this.round + 1);
this.speed /= 1.02;
this.isGameOver();
} else {
this.userPosition++;
}
} else {
this.gameLost();
}
}
isGameOver() {
if (this.round === this.totalRounds) {
this.gameWon();
} else {
this.userPosition = 0;
this.showSequence();
};
}
showSequence() {
this.blockedButtons = true;
let sequenceIndex = 0;
let timer = setInterval(() => {
const button = this.buttons[this.sequence[sequenceIndex]];
this.buttonSounds[this.sequence[sequenceIndex]].play();
this.toggleButtonStyle(button)
setTimeout( () => this.toggleButtonStyle(button), this.speed / 2)
sequenceIndex++;
if (sequenceIndex > this.round) {
this.blockedButtons = false;
clearInterval(timer);
}
}, this.speed);
}
toggleButtonStyle(button) {
button.classList.toggle('active');
}
gameLost() {
this.errorSound.play();
this.display.startButton.disabled = false;
this.blockedButtons = true;
}
gameWon() {
this.display.startButton.disabled = false;
this.blockedButtons = true;
this.buttons.forEach(element =>{
element.classList.add('winner');
});
this.updateRound('you win 🏆');
}
}
const simon = new Simon(simonButtons, startButton, round);
simon.init();