Skip to content

Commit 885ed6f

Browse files
committed
simon game implementation
1 parent 945281e commit 885ed6f

File tree

10 files changed

+419
-0
lines changed

10 files changed

+419
-0
lines changed

Simon-GameChallenge/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<center>
2+
<h1 style="font-size: 36px; font-family: 'Roboto', sans-serif; font-weight: bold;">🎮 Simon Game</h1>
3+
</center>
4+
5+
- A web-based interactive **Simon Game**, where users test and improve their memory by repeating an ever-growing sequence of lights and sounds.
6+
7+
### ***Features***
8+
- "🎶 **Colorful Light Patterns**: Buttons light up in a sequence that users must memorize and replicate."
9+
- "📱 **Responsive Design**: The layout is optimized for various screen sizes and devices."
10+
- "🎨 **Animations and Feedback**: Buttons are visually and audibly animated, providing instant feedback on user actions."
11+
- "🔄 **Restart Option**: Users can end the game and restart the game manually in normal mode or automatically restart after a mistake in tricky mode."
12+
13+
### 🔢 **Game Rules**
14+
1. Watch the sequence of buttons lighting up.
15+
2. Repeat the sequence by clicking the buttons or using keyboard keys.
16+
3. With every correct sequence, an additional button is added.
17+
4. The game ends when the user presses the wrong button.
18+
19+
### 🔧 **Technologies Used**
20+
- **HTML**: Provides the structure for the game's interface.
21+
- **CSS**: Adds styles, animations, and ensures responsiveness.
22+
- **JavaScript**: Implements game logic, sequence tracking, and animations.

Simon-GameChallenge/game.js

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
let gamePattern = [];
2+
let userClickedPattern = [];
3+
let buttonColours = ["red", "blue", "green", "yellow"];
4+
let started = false;
5+
let level = 0;
6+
let highScore = 0;
7+
let gameMode = null; // Game mode is null initially
8+
9+
// Display the high score and level
10+
$("#high-score").text(highScore);
11+
$("#current-level").text(level);
12+
13+
// Start Game Button Click Event
14+
$("#start-button").on("click", function () {
15+
if (!gameMode) {
16+
alert("Please select a mode (Normal or Strict) to start the game!");
17+
return;
18+
}
19+
20+
if (!started) {
21+
$("#level-title").text("Happy Gaming! - Simon");
22+
nextSequence();
23+
started = true;
24+
$(this).hide();
25+
}
26+
});
27+
28+
// End Game Button Click Event
29+
$("#end-button").on("click", function () {
30+
if (!started) return; // Do nothing if the game hasn't started
31+
32+
// End the game by resetting all values
33+
$("#level-title").text("Game Ended. Press Start to Restart");
34+
updateHighScore(); // Save high score before resetting
35+
startOver(); // Reset the game state
36+
$("#start-button").show(); // Show the Start button again
37+
});
38+
39+
// Mode Selection (Normal/Strict)
40+
$("input[name='mode']").on("change", function () {
41+
gameMode = $(this).val();
42+
alert(`Game mode set to: ${gameMode.toUpperCase()}`);
43+
});
44+
45+
// User Clicks on a Button
46+
$(".btn").on("click", function () {
47+
if (!started) return;
48+
49+
let userChosenColour = $(this).attr("id");
50+
userClickedPattern.push(userChosenColour);
51+
52+
playSound(userChosenColour);
53+
animatePress(userChosenColour);
54+
55+
checkAnswer(userClickedPattern.length - 1);
56+
});
57+
58+
// Check User's Answer
59+
function checkAnswer(currentLevel) {
60+
if (gamePattern[currentLevel] === userClickedPattern[currentLevel]) {
61+
if (userClickedPattern.length === gamePattern.length) {
62+
setTimeout(() => {
63+
nextSequence();
64+
}, 1000);
65+
}
66+
} else {
67+
playSound("wrong");
68+
$("body").addClass("game-over");
69+
$("#level-title").text("Game Over. Press Start to Retry.");
70+
71+
setTimeout(() => {
72+
$("body").removeClass("game-over");
73+
}, 200);
74+
75+
if (gameMode === "strict") {
76+
updateHighScore();
77+
startOver(); // Strict mode resets the game completely
78+
} else {
79+
userClickedPattern = []; // Normal mode allows retrying the same sequence
80+
}
81+
}
82+
}
83+
84+
// Generate the Next Sequence
85+
function nextSequence() {
86+
userClickedPattern = [];
87+
level++;
88+
$("#current-level").text(level);
89+
90+
let randomNumber = Math.floor(Math.random() * 4);
91+
let randomChosenColour = buttonColours[randomNumber];
92+
gamePattern.push(randomChosenColour);
93+
94+
$(`#${randomChosenColour}`).fadeOut(100).fadeIn(100);
95+
playSound(randomChosenColour);
96+
}
97+
98+
// Play Sound Based on Button Colour
99+
function playSound(name) {
100+
let audio = new Audio(`sounds/${name}.mp3`);
101+
audio.play();
102+
}
103+
104+
// Animate Button Press
105+
function animatePress(currentColour) {
106+
$(`#${currentColour}`).addClass("pressed");
107+
setTimeout(() => {
108+
$(`#${currentColour}`).removeClass("pressed");
109+
}, 100);
110+
}
111+
112+
// Start Over Function (Resets Game State)
113+
function startOver() {
114+
level = 0;
115+
gamePattern = [];
116+
userClickedPattern = [];
117+
started = false;
118+
$("#current-level").text(level);
119+
}
120+
121+
// Update High Score
122+
function updateHighScore() {
123+
if (level > highScore) {
124+
highScore = level - 1; // Subtract 1 because the game increments the level before game over
125+
$("#high-score").text(highScore);
126+
}
127+
}
128+
129+
// Toggle Dark/Light Mode
130+
$("#mode-toggle").on("click", function () {
131+
$("body").toggleClass("dark-mode");
132+
$(this).toggleClass("dark-mode");
133+
});

Simon-GameChallenge/game.png

25.9 KB
Loading

Simon-GameChallenge/index.html

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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 rel="icon" type="image/jpg" href="./game.png" />
8+
<link href="https://fonts.googleapis.com/css?family=Press+Start+2P" rel="stylesheet">
9+
</head>
10+
<body>
11+
<h1 id="level-title">Press Start to Begin</h1>
12+
<div class="main-container">
13+
<!-- Game Layout -->
14+
<div class="container">
15+
<div class="row">
16+
<div type="button" id="green" class="btn green"></div>
17+
<div type="button" id="blue" class="btn blue"></div>
18+
</div>
19+
<div class="row">
20+
<div type="button" id="red" class="btn red"></div>
21+
<div type="button" id="yellow" class="btn yellow"></div>
22+
</div>
23+
</div>
24+
25+
<!-- Info Section -->
26+
<div class="info-panel">
27+
<p>Level: <span id="current-level">0</span></p>
28+
<p>High Score: <span id="high-score">0</span></p>
29+
30+
<!-- Mode Selection -->
31+
<div class="mode-selection">
32+
<label>
33+
<input type="radio" name="mode" value="normal">
34+
Normal
35+
</label>
36+
<label>
37+
<input type="radio" name="mode" value="strict">
38+
Tricky
39+
</label>
40+
</div>
41+
42+
<button id="start-button">Start Game</button>
43+
<button id="end-button">End Game</button> <!-- New End Game Button -->
44+
</div>
45+
</div>
46+
47+
<!-- Robot Icon Link -->
48+
<a href="https://github.com/Saiharitha3/Simon-GameChallenge" target="_blank" id="github-link">
49+
<span id="robot-icon">🤖</span>
50+
</a>
51+
52+
<!-- Dark/Light Mode Button -->
53+
<button id="mode-toggle" class="mode-button">🌙</button>
54+
55+
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
56+
<script src="./game.js" rel="javascript"></script>
57+
</body>
58+
</html>

Simon-GameChallenge/sounds/blue.mp3

3.56 KB
Binary file not shown.

Simon-GameChallenge/sounds/green.mp3

17.3 KB
Binary file not shown.

Simon-GameChallenge/sounds/red.mp3

16.3 KB
Binary file not shown.

Simon-GameChallenge/sounds/wrong.mp3

7.74 KB
Binary file not shown.

Simon-GameChallenge/sounds/yellow.mp3

10.8 KB
Binary file not shown.

0 commit comments

Comments
 (0)