Skip to content

Commit 010b64d

Browse files
Create The Simon Game folder and add game files
1 parent 9fe0746 commit 010b64d

File tree

8 files changed

+152
-0
lines changed

8 files changed

+152
-0
lines changed

Diff for: javascript/the-simon-game/gameFiles.js

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

Diff for: javascript/the-simon-game/index.html

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

Diff for: javascript/the-simon-game/sounds/blue.mp3

3.56 KB
Binary file not shown.

Diff for: javascript/the-simon-game/sounds/green.mp3

17.3 KB
Binary file not shown.

Diff for: javascript/the-simon-game/sounds/red.mp3

16.3 KB
Binary file not shown.

Diff for: javascript/the-simon-game/sounds/wrong.mp3

7.74 KB
Binary file not shown.

Diff for: javascript/the-simon-game/sounds/yellow.mp3

10.8 KB
Binary file not shown.

Diff for: javascript/the-simon-game/styles.css

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
body {
2+
text-align: center;
3+
background-color: #011F3F;
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)