-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
116 lines (68 loc) · 4.46 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
var buttonColours = ["red", "blue", "green", "yellow"]; // four colours in the game
var gamePattern = []; // empty array to which we will push the new generated colours
var userClickedPattern = []; // will store the userChosenColour every time
var started = false; // keep track of whether the game has started or not, so we only call nextSequence() on the first keydown.
var level = 0; // will show the level and it starts at level 0
$(document).keypress(function () { // detect when a keyboard key has been pressed, when that happens for the first time, call nextSequence()
if (!started) { // if started is false, which is, because that is how we built it to begin
$("#level-title").text("Level " + level); // will change h1 to "Level 0" when the game starts
nextSequence(); // calls the function that generates a random colour
started = true; // change started status from false to true
}
});
$(".btn").click(function () { // will detect when any of the buttons are clicked and trigger a handler function
var userChosenColour = $(this).attr("id"); // userChosenColour will store the id of the button that got clicked (this)
userClickedPattern.push(userChosenColour);
playSound(userChosenColour);
animatePress(userChosenColour); // will add .pressed class to the chosen colour
checkAnswer(userClickedPattern.length - 1); // call after a user has clicked and chosen their answer, passing in the index of the last answer in the user's sequence.
console.log("userClickedPatter: " + userClickedPattern.length);
});
function checkAnswer(currentLevel) {
if (gamePattern[currentLevel] === userClickedPattern[currentLevel]) { // check if the most recent user answer is the same as the game pattern. If so then log "success", otherwise log "wrong".
console.log("success");
// var soundRight = new Audio("sounds/" + userChosenColour + ".mp3");
// soundRight.play();
if (userClickedPattern.length === gamePattern.length) { // if the user got the most recent answer right in step 3, then check that they have finished their sequence with another if statement.
setTimeout(function () { // call nextSequence() after a 1000 millisecond delay.
nextSequence();
}, 1000);
}
} else {
console.log("wrong");
playSound("wrong");
$("body").addClass("game-over"); // gives the body the class "game-over"
setTimeout(function () {
$("body").removeClass("game-over"); // will removed the given class after 200 miliseconds = 0.2 seconds
}, 200);
// $("h1").text("Game Over, Press Any Key to Restart");
$("#level-title").text("Game Over, Press Any Key to Restart");
startOver();
}
}
function nextSequence() { // will generate the next colour
userClickedPattern = []; // once nextSequence() is triggered, reset the userClickedPattern to an empty array ready for the next level.
level++; // inside nextSequence(), increase the level by 1 every time nextSequence() is called.
$("#level-title").text("Level " + level); // inside nextSequence(), update the h1 with this change in the value of level.
var randomNumber = Math.floor(Math.random() * 4); // create a random number 0-3
var randomChosenColour = buttonColours[randomNumber]; // select a colour from the array according to the random number
gamePattern.push(randomChosenColour); // push selected colour into the empty array
console.log("randomChosenColour: " + randomChosenColour);
$("#" + randomChosenColour).fadeIn(100).fadeOut(100).fadeIn(100); // add animation for the selected colour
playSound(randomChosenColour);
}
function playSound(name) { // creates a sound for a specific colour. We will call it when a random colour is selected and when a user clicks on a chosen button.
var audio = new Audio("sounds/" + name + ".mp3"); // play a sound for the clicked colour
audio.play();
};
function animatePress(currentColour) { // will animate the pressed colour
$("#" + currentColour).addClass("pressed"); // gives the pressed colour a new class: ".pressed"
setTimeout(function () {
$("#" + currentColour).removeClass("pressed"); // will removed the given class after 100 miliseconds = 0.1 seconds
}, 100);
}
function startOver() { // reset the values of level, gamePattern and started variables. Will be called at the "wrong" if statement.
level = 0;
gamePattern = [];
started = false;
}