-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
95 lines (87 loc) · 2.26 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
93
94
95
var buttonColor = ["red", "blue", "green", "yellow"];
var gamePattern = [];
var userChosenColor;
var userClickedPattern = [];
var pressedKey = false;
var level = 1;
$("body").keydown(function() {
if (pressedKey===false) {
pressedkey = true;
nextSequence();
}
else {
level--;
}
});
function nextSequence() {
$("#level-title").text("Level " + level++);
var randomNumber = Math.floor((Math.random()) * 4);
var randomChosenColor = buttonColor[randomNumber];
gamePattern.push(randomChosenColor);
$("#" + randomChosenColor).fadeOut(100).fadeIn(100);
playSound(gamePattern[gamePattern.length - 1]);
return randomNumber;
}
$(".btn").click(function(event) {
userChosenColor = event.target.id;
userClickedPattern.push(userChosenColor);
checkAnswer(userClickedPattern.length - 1);
playSound(userChosenColor);
animatePress(userChosenColor);
});
function playSound(color) {
// switch (color) {
// case ("red"):
// var redSound = new Audio('sounds/red.mp3');
// redSound.play();
// break;
//
// case ("blue"):
// var blueSound = new Audio('sounds/blue.mp3');
// blueSound.play();
// break;
//
// case ("green"):
// var greenSound = new Audio('sounds/green.mp3');
// greenSound.play();
// break;
//
// case ("yellow"):
// var yellowSound = new Audio('sounds/yellow.mp3');
// yellowSound.play();
// break;
// }
var audio = new Audio('sounds/' + color + '.mp3');
audio.play();
}
function animatePress(color) {
$("#" + color).addClass("pressed");
setTimeout(function() {
$("#" + color).removeClass("pressed");
}, 100);
}
function checkAnswer(currentLevel) {
if (userClickedPattern[currentLevel] === gamePattern[currentLevel]) {
console.log("success");
if (userClickedPattern.length === gamePattern.length) {
setTimeout(function() {
userClickedPattern = [];
nextSequence();
}, 1000);
}
} else {
console.log("fail");
$("body").addClass("game-over");
$("#level-title").text("Game Over, Press Any Key to Restart");
setTimeout(function() {
$("body").removeClass("game-over");
},200);
startOver();
}
}
function startOver() {
gamePattern=[];
userClickedPattern=[];
pressedKey=false;
level=0;
}