Skip to content

Commit 5399d00

Browse files
authored
Add files via upload
1 parent 85d7350 commit 5399d00

File tree

8 files changed

+183
-0
lines changed

8 files changed

+183
-0
lines changed

game.js

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
var buttonColours = ["red", "blue", "green", "yellow"];
2+
3+
var gamePattern = [];
4+
5+
var userClickedPattern = [];
6+
7+
var started = false;
8+
9+
var level = 0;
10+
11+
$(document).keypress(function() {
12+
if (!started) {
13+
$("#level-title").text("Level " + level);
14+
nextSequence();
15+
started = true;
16+
}
17+
});
18+
19+
$(".btn").on("click", function() {
20+
var userChosenColour = $(this).attr("id");
21+
22+
userClickedPattern.push(userChosenColour);
23+
24+
playSound(userChosenColour);
25+
26+
animatePress(userChosenColour);
27+
28+
checkAnswer(userClickedPattern.length-1);
29+
});
30+
31+
function nextSequence() {
32+
userClickedPattern = [];
33+
34+
level++;
35+
36+
$("#level-title").text("Level " + level);
37+
38+
var randomNumber = Math.floor(Math.random() * 4);
39+
40+
var randomChosenColour = buttonColours[randomNumber];
41+
42+
gamePattern.push(randomChosenColour);
43+
44+
$("#" + randomChosenColour).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100);
45+
46+
playSound(randomChosenColour);
47+
}
48+
49+
function playSound(name) {
50+
var audio = new Audio("sounds/" + name + ".mp3");
51+
52+
audio.play();
53+
}
54+
55+
function animatePress(currentColour) {
56+
$("#" + currentColour).addClass("pressed");
57+
setTimeout(function () {
58+
$("#" + currentColour).removeClass("pressed");
59+
}, 100);
60+
}
61+
62+
function checkAnswer(currentLevel) {
63+
if(gamePattern[currentLevel] === userClickedPattern[currentLevel] ) {
64+
if (userClickedPattern.length === gamePattern.length) {
65+
setTimeout(function () {
66+
nextSequence();
67+
}, 1000);
68+
}
69+
} else {
70+
playSound("wrong");
71+
72+
$("body").addClass("game-over");
73+
setTimeout(function() {
74+
$("body").removeClass("game-over");
75+
}, 200);
76+
77+
$("#level-title").text("Game Over, Press Any Key to Restart");
78+
79+
startOver();
80+
}
81+
}
82+
83+
function startOver() {
84+
level = 0;
85+
gamePattern = [];
86+
started = false;
87+
}

index.html

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<!DOCTYPE html>
2+
<html lang="en" dir="ltr">
3+
4+
<head>
5+
<meta charset="utf-8">
6+
<title>Simon</title>
7+
<link rel="stylesheet" href="styles.css">
8+
<link href="https://fonts.googleapis.com/css?family=Press+Start+2P" rel="stylesheet">
9+
<link rel="shortcut icon" href="#">
10+
</head>
11+
12+
<body>
13+
<h1 id="level-title">Press A Key to Start</h1>
14+
<div class="container">
15+
<div lass="row">
16+
17+
<div type="button" id="green" class="btn green">
18+
19+
</div>
20+
21+
<div type="button" id="red" class="btn red">
22+
23+
</div>
24+
</div>
25+
26+
<div class="row">
27+
28+
<div type="button" id="yellow" class="btn yellow">
29+
30+
</div>
31+
<div type="button" id="blue" class="btn blue">
32+
33+
</div>
34+
35+
</div>
36+
37+
</div>
38+
39+
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
40+
<script src="game.js" charset="utf-8"></script>
41+
</body>
42+
43+
</html>

sounds/blue.mp3

3.56 KB
Binary file not shown.

sounds/green.mp3

17.3 KB
Binary file not shown.

sounds/red.mp3

16.3 KB
Binary file not shown.

sounds/wrong.mp3

7.74 KB
Binary file not shown.

sounds/yellow.mp3

10.8 KB
Binary file not shown.

styles.css

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
body {
2+
text-align: center;
3+
background-color: #3b0e0e;
4+
}
5+
6+
#level-title {
7+
font-family: 'Press Start 2P', cursive;
8+
font-size: 3rem;
9+
margin: 5%;
10+
color: #FEF2BF;
11+
}
12+
13+
.container {
14+
display: block;
15+
width: 50%;
16+
margin: auto;
17+
18+
}
19+
20+
.btn {
21+
margin: 25px;
22+
display: inline-block;
23+
height: 200px;
24+
width: 200px;
25+
border: 10px solid black;
26+
border-radius: 20%;
27+
}
28+
29+
.game-over {
30+
background-color: red;
31+
opacity: 0.8;
32+
}
33+
34+
.red {
35+
background-color: red;
36+
}
37+
38+
.green {
39+
background-color: green;
40+
}
41+
42+
.blue {
43+
background-color: blue;
44+
}
45+
46+
.yellow {
47+
background-color: yellow;
48+
}
49+
50+
.pressed {
51+
box-shadow: 0 0 20px white;
52+
background-color: grey;
53+
}

0 commit comments

Comments
 (0)