-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mygame.js
104 lines (67 loc) · 2.28 KB
/
Mygame.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
let buttonColours = ["red", "blue", "green", "yellow"];
let gamePattern = [];
let userClickedPattern = [];
let started = false;
let level = 0;
function nextsequence() {
level++;
$("h1").text("level " + level);
let randomNumber = Math.floor(Math.random() * 4); //0-3.99999999
let randomChosenColour = buttonColours[randomNumber];
gamePattern.push(randomChosenColour);
// 1. Use jQuery to select the button with the same id as the randomChosenColour
//2.Use Google/Stackoverflow to figure out how you can use jQuery to animate a flash to the button selected in step
$("." + randomChosenColour).fadeOut(50).fadeIn(50);
soundPlay(randomChosenColour);
}
$(document).click(function (event) {
let userChosenColour = event.target.id;
userClickedPattern.push(userChosenColour);
soundPlay(userChosenColour);
animatePress(userChosenColour);
checkAnswer(userClickedPattern.length-1);
});
function soundPlay(color) {
let sound = new Audio("sounds/" + color + ".mp3");
sound.play();
}
function animatePress(currentColour) {
$("." + currentColour).addClass("pressed");
setTimeout(function () {
$("." + currentColour).removeClass("pressed");
}, 100);
}
$(document).keydown(function () {
if (started === false) {
nextsequence();
started = true;
}
})
function checkAnswer(currentLevel){
if(gamePattern[currentLevel] === userClickedPattern[currentLevel]){
console.log("success");
if(userClickedPattern.length == gamePattern.length)
{
setTimeout(function (){
nextsequence();
}, 1000);
userClickedPattern = [];
}
}
else{
console.log("wrong");
let wrong = new Audio("sounds/wrong.mp3");
wrong.play();
$("body").addClass("game-over");
$("h1").text("Game Over, Press Any Key to Restart");
setTimeout(function(){
$("body").removeClass("game-over");
}, 200);
startOver();
}
}
function startOver(){
started= false;
level= 0;
gamePattern= [];
}