-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
107 lines (92 loc) · 2.77 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
// ARRAY
var buttonColours = ["red", "blue", "green", "yellow"]
var gamePattern = []
var userPattern= []
var highestScore = []
var switcher= false
var level = 1
var generatedColour
var selectedColour
// starter Keypress
$(document).keypress(function(){
$("#level-title").html("Level "+ 0)
if (!switcher) {
newSequence()
switcher = true
}
})
// User choice
$(".btn").click(function(){
selectedColour = this.id
userPattern.push(selectedColour)
console.log("You selected: "+ selectedColour)
console.log(level)
playSound(selectedColour)
checkArray(userPattern.length - 1)
showAnimation(selectedColour)
})
// Generate Random Variable
function newSequence(){
userPattern = []
var randomNumber= Math.floor(Math.random()*4)
var generatedColour = buttonColours[randomNumber]
gamePattern.push(generatedColour)
console.log("generated colour is: "+ generatedColour)
showAnimation(generatedColour)
playSound(generatedColour)
}
// Check the answer
function checkArray(currentLevel){
if (userPattern[currentLevel] === gamePattern[currentLevel]){
console.log(gamePattern)
console.log(userPattern)
if(gamePattern.length == userPattern.length){
setTimeout(function () {
newSequence()
}, 1000)
$("h1").html("Level "+ level++)
highestScore.push(level-1)
var max = Math.max.apply(null, highestScore)
highestLevel(max)
}
}
else {
$("body").addClass("game-over")
$("#level-title").text("Game Over, Press any key to Restart")
setTimeout(function () {
$("body").removeClass("game-over")
}, 1500)
// highestScore.push(level)
console.log(level)
gameOver()
}
}
//
//play sound
function playSound(noise){
var audioRandom = new Audio('sounds/'+noise+'.mp3')
audioRandom.play()
}
// Animation
function showAnimation(light){
setTimeout(function() {
$("#"+light).addClass("pressed")
}, 200)
$("#"+light).addClass("pressed")
setTimeout(function() {
$("#"+light).removeClass("pressed")
}, 500)
}
// Game Over
function gameOver(){
playSound("wrong")
gamePattern = []
console.log(userPattern)
switcher = false
level = 1
}
// Highest Score
function highestLevel(reachedLevel){
console.log(highestScore)
$("#highest").html("Highest Score: " + reachedLevel)
}