-
Notifications
You must be signed in to change notification settings - Fork 0
/
simon.js
90 lines (78 loc) · 2.26 KB
/
simon.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
var buttoncolor=["red", "blue", "green", "yellow"]
var gamepattern=[]
var userClickedPattern=[]
var level=0;
var started=false;
function nextsequence()
{
userClickedPattern=[]
level++;
var n=Math.random();
n=n*3;
$("#level-title").text("Level "+level);
var randomchosencolor=buttoncolor[Math.round(n)]
gamepattern.push(randomchosencolor)
$("#"+randomchosencolor).fadeIn(100).fadeOut(100).fadeIn(100);
playSound(randomchosencolor)
}
$(".btn").on("click",function()
{
var userChosenColor=$(this).attr("id");
userClickedPattern.push(userChosenColor);
playSound(userChosenColor);
animatePress(userChosenColor)
checkAnswer(userClickedPattern.length-1)
//console.log(userClickedPattern)
})
function playSound(name)
{
var audio = new Audio("./sounds/"+name+".mp3");
audio.play();
}
//animation
function animatePress(currentColor)
{
var activeButton=document.querySelector("."+currentColor);
activeButton.classList.add("pressed");
setTimeout(function()
{
activeButton.classList.remove("pressed")
},100)
}
document.addEventListener("keypress",function(){
if (!started) {
$("#level-title").text("Level " + level);
nextsequence();
started = true;
}
})
function checkAnswer(currentLevel)
{
//3. Write an if statement inside checkAnswer() to check if the most recent user answer is the same as the game pattern. If so then log "success", otherwise log "wrong".
if (gamepattern[currentLevel] === userClickedPattern[currentLevel]) {
console.log("success");
//If the user got the most recent answer right in step 3, then check that they have finished their sequence with another if statement.
if (userClickedPattern.length === gamepattern.length){
setTimeout(function () {
nextsequence();
}, 1000);
}
} else {
console.log("wrong");
playSound("wrong")
$("body").addClass("game-over")
setTimeout(function()
{
$("body").removeClass("game-over")
},200)
$("#level-title").text("Game Over, Press Any Key to Restart");
startOver()
}
}
function startOver()
{
level=0
gamepattern=[]
userClickedPattern=[]
started=false
}