-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
92 lines (82 loc) · 2.18 KB
/
game.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
var buttonColours = ["red", "blue", "green", "yellow"];
var gamePattern = [];
var userSelection = [];
var level = 1;
var start = false;
$("#start").click(function () {
start = true;
if($("#start").hasClass("playingButton") == false){
StartGame();
}
});
function StartGame(){
$("#level-title").text("Level " + level);
$("#start").text("Playing");
$("#start").addClass("playingButton");
setTimeout(() => {
NextSequence();
}, 500);
}
function NextSequence() {
userSelection = [];
$("#level-title").text("Level " + level++);
var randomNumber = Math.random() * 4;
randomNumber = Math.floor(randomNumber);
var randomChosenColour = buttonColours[randomNumber];
gamePattern.push(randomChosenColour);
//Selection Animation
$("#"+randomChosenColour).fadeIn(300).fadeOut(300).fadeIn(300);
//Selection Sound
playSound(randomChosenColour);
}
function CheckUserAnswer(currentLevel){
if(gamePattern[currentLevel] === userSelection[currentLevel]){
if(userSelection.length === gamePattern.length){
setTimeout(function(){
NextSequence();
},1000);
}
} else {
GameOver();
}
}
function GameOver(){
start = false;
$("body").addClass("redBody");
$("#level-title").text("GAME OVER!" + "Level " + (level-1));
playSound("wrong");
StartOver();
setTimeout(() => {
$("body").removeClass("redBody");
}, 200);
$("#start").removeClass("playingButton");
$("#start").addClass("restartButton");
$("#start").text("Restart");
}
function StartOver(){
gamePattern = [];
level = 1;
}
//Check for button selection
$(".btn").click(function(){
var userChosenColour = $(this).attr("id");
userSelection.push(userChosenColour);
playSound(userChosenColour);
PressAnimation(userChosenColour);
if(start == true){
CheckUserAnswer(userSelection.length-1);
}
});
function playSound(name){
var audio = new Audio("sounds/"+name+".mp3");
audio.play();
}
function PressAnimation(currentColour){
$("#"+currentColour).addClass("pressed"+currentColour);
setTimeout(function(){
$("#"+currentColour).removeClass("pressed"+currentColour);
},100);
}
$("#rule").click(function(){
$(".rulesText").toggleClass("removeDisplay");
});