Skip to content

Commit ad5b867

Browse files
committed
simon game added
1 parent 8b15e42 commit ad5b867

File tree

14 files changed

+258
-0
lines changed

14 files changed

+258
-0
lines changed

Simon-Game/.DS_Store

6 KB
Binary file not shown.

Simon-Game/.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"liveServer.settings.port": 5501
3+
}

Simon-Game/game.js

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

Simon-Game/index.html

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
</head>
10+
11+
<body>
12+
<h1 id="level-title">Press A Key to Start</h1>
13+
<div class="container">
14+
<div class="row">
15+
16+
<div type="button" id="green" class="btn green">
17+
18+
</div>
19+
20+
<div type="button" id="red" class="btn red">
21+
22+
</div>
23+
</div>
24+
25+
<div class="row">
26+
27+
<div type="button" id="yellow" class="btn yellow">
28+
29+
</div>
30+
<div type="button" id="blue" class="btn blue">
31+
32+
</div>
33+
34+
</div>
35+
36+
</div>
37+
38+
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
39+
<script src="game.js"></script>
40+
</body>
41+
42+
</html>

Simon-Game/readme

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Turn on the Simon game console to start a new game.
2+
The game console will produce a short sequence of button presses, beginning with just one. For example, it may light up and play a sound for the red button.
3+
You must replicate the exact sequence by pressing the corresponding buttons in the same order. If the sequence is "red," you press the red button.
4+
The console will then add one more step to the sequence, and you must repeat the longer sequence accurately.
5+
Continue to follow the pattern as it grows longer, always starting from the beginning of the sequence.
6+
If you make a mistake and press the wrong button or get the order wrong, the game will signal the mistake, and the round will end.
7+
The game will display your level, which represents how many steps you correctly completed in the sequence.
8+
Scoring:
9+
The level in the Simon game is typically based on the number of steps in the sequence you complete correctly before making a mistake. The higher your score, the better your memory and concentration skills are.
10+
11+
12+

Simon-Game/sounds/blue.mp3

3.56 KB
Binary file not shown.

Simon-Game/sounds/green.mp3

17.3 KB
Binary file not shown.

Simon-Game/sounds/red.mp3

16.3 KB
Binary file not shown.

Simon-Game/sounds/wrong.mp3

7.74 KB
Binary file not shown.

Simon-Game/sounds/yellow.mp3

10.8 KB
Binary file not shown.

0 commit comments

Comments
 (0)