Skip to content

Commit 2abea9e

Browse files
authored
Added files
1 parent 4985b22 commit 2abea9e

File tree

3 files changed

+175
-0
lines changed

3 files changed

+175
-0
lines changed

Simon_Game/game.js

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

Simon_Game/index.html

Lines changed: 43 additions & 0 deletions
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+
</head>
10+
11+
<body>
12+
<h1 id="level-title">Press A Key to Start</h1>
13+
<div class="container">
14+
<div lass="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" charset="utf-8"></script>
40+
41+
</body>
42+
43+
</html>

Simon_Game/style.css

Lines changed: 53 additions & 0 deletions
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)