-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
130 lines (128 loc) · 3.75 KB
/
Copy pathindex.js
File metadata and controls
130 lines (128 loc) · 3.75 KB
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
// Array of buttons
var buttonColours = ["red", "blue", "green", "yellow"];
// Array will fill up with Simon Sequence
var gamePattern = [];
// Array will fill up with User clicked pattern
var userClickedPattern = [];
// START THE GAME
var started = false;
// Stores the Level
var level = 0;
// Starts game
var start = function () {
if (!started) {
// Changes h1
$("#level-title")
.text("Level " + level)
.animate({ letterSpacing: "10px" });
nextSequence();
started = true;
// Brings game down
$("#row1").animate({ marginTop: "5%" });
}
};
// Adds click detection to Start the game
$("#start").click(start);
// Key press function
$(document).keypress(start);
$(document).keypress(function () {
$("h2").remove();
});
// Handler function on button click
$(".btn").click(function () {
// Stores the id of button that got clicked
var userChosenColour = $(this).attr("id");
// User clicked Array
userClickedPattern.push(userChosenColour);
//Logs the user clicked pattern as an array
console.log(userClickedPattern);
// Play sound from button that user clicked
playSound(userChosenColour);
// Animates the clicked button
animatePress(userChosenColour);
checkAnswer(userClickedPattern.length - 1);
});
// Checks the userClickedPattern against the gamePattern
function checkAnswer(currentLevel) {
if (gamePattern[currentLevel] === userClickedPattern[currentLevel]) {
if (userClickedPattern.length === gamePattern.length) {
setTimeout(function () {
nextSequence();
}, 1000);
}
} else {
// Updates the h1
$("#level-title").text("Level " + level + " ❌ Game Over");
// Adds restart instructions
$("#start")
.append("<h2 class='start'>Restart here<br/>Recomece aqui</h2>")
.animate({ letterSpacing: "5px" });
// Adds class to concatenation of id and userChosenColour or randomChosenColour
$("body").addClass("game-over");
// Delays the removal of the class
setTimeout(function () {
$("body").removeClass("game-over");
}, 1000);
startOver();
}
}
// Remove restart instructions
$("#start").on("click", function () {
$("h2").remove();
});
// Function Creates a new sequence of buttons to be pressed
function nextSequence() {
// Resets userClickedPattern
userClickedPattern = [];
// Adds a level everytime nextSequence is called
level++;
// Changes h1 to new level when next sequence is called
$("#level-title").text("Level " + level);
// Creates A New Pattern
var randomNumber = Math.floor(Math.random() * 4);
// Selects one of the buttonColours
var randomChosenColour = buttonColours[randomNumber];
// Add the new number in the gamePattern
gamePattern.push(randomChosenColour);
// Flashes the button to show sequence to User
$("#" + randomChosenColour)
.fadeIn(100)
.fadeOut(100)
.fadeIn(100);
// Plays audio when flashes *REFACTORED
playSound(randomChosenColour);
// Animates the randomChosenColour
}
// Play correct sound when clicked and when creating new sequence
function playSound(name) {
var audio = new Audio("./sounds/" + name + ".mp3");
audio.play();
}
// Animation
// Animation for user
function animatePress(currentColour) {
// Adds class to concatenation of id and userChosenColour or randomChosenColour
$("#" + currentColour).addClass("pressed");
// Delays the removal of the class
setTimeout(function () {
$("#" + currentColour).removeClass("pressed");
}, 100);
}
// Restarts the game
function startOver() {
// Resets the game
level = 0;
gamePattern = [];
started = false;
}
// Starts game by keyboard
$("");
// Bring back instructions when clicked
$("p").click(function () {
$(".pt-instructions").animate({
height: "toggle",
});
$(".en-instructions").animate({
height: "toggle",
});
});