Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <title>Simon Says Clone</title>
- <style>
- #gamepad {
- display: flex;
- flex-wrap: wrap;
- justify-content: center;
- align-items: center;
- width: 200px;
- height: 200px;
- border: 2px solid black;
- border-radius: 50%;
- margin: 0 auto;
- }
- .button {
- width: 90px;
- height: 90px;
- border-radius: 50%;
- margin: 5px;
- cursor: pointer;
- }
- .red {
- background-color: #ff6347;
- }
- .green {
- background-color: #2ecc71;
- }
- .blue {
- background-color: #3498db;
- }
- .yellow {
- background-color: #f1c40f;
- }
- .active {
- opacity: 0.5;
- }
- </style>
- <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/css/bootstrap.min.css">
- </head>
- <body>
- <div class="container mt-5">
- <h1 class="text-center">Simon Says Clone</h1>
- <div class="row justify-content-center">
- <div class="col-auto">
- <div id="gamepad">
- <div class="button red"></div>
- <div class="button green"></div>
- <div class="button blue"></div>
- <div class="button yellow"></div>
- </div>
- <div class="row mt-3">
- <div class="col text-center">
- <button class="btn btn-primary start-button">Start Game</button>
- </div>
- </div>
- </div>
- </div>
- </div>
- <!-- Add Bootstrap JS and jQuery -->
- <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/js/bootstrap.bundle.min.js"></script>
- <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
- <script>
- var sequence = [];
- var playerSequence = [];
- var level = 0;
- var gameStarted = false;
- var flashing = false;
- // Start the game
- $('.start-button').click(function() {
- if (!gameStarted) {
- level = 0;
- sequence = [];
- playerSequence = [];
- gameStarted = true;
- startRound();
- }
- });
- // Start a new round
- function startRound() {
- level++;
- $('.start-button').text('Level ' + level);
- var buttonColors = ['red', 'green', 'blue', 'yellow'];
- var randomColor = buttonColors[Math.floor(Math.random() * buttonColors.length)];
- sequence.push(randomColor);
- flashSequence();
- }
- // Flash the sequence
- function flashSequence() {
- flashing = true;
- var i = 0;
- var interval = setInterval(function() {
- if (i === sequence.length) {
- clearInterval(interval);
- flashing = false;
- playerSequence = [];
- $('.button').removeClass('active');
- } else {
- flashButton(sequence[i]);
- i++;
- }
- }, 1000);
- }
- // Flash a button
- function flashButton(color) {
- var button = $('.' + color);
- button.addClass('active');
- var audio = new Audio('https://s3.amazonaws.com/freecodecamp/simonSound1.mp3');
- audio.play();
- setTimeout(function() {
- button.removeClass('active');
- }, 500);
- }
- // Handle button clicks
- $('.button').click(function() {
- if (flashing) {
- return;
- }
- var color = $(this).attr('class').split(' ')[1];
- playerSequence.push(color);
- flashButton(color);
- checkSequence();
- });
- // Check the sequence
- function checkSequence() {
- for (var i = 0; i < playerSequence.length; i++) {
- if (playerSequence[i] !== sequence[i]) {
- gameOver();
- return;
- }
- }
- if (playerSequence.length === sequence.length) {
- setTimeout(startRound, 1000);
- }
- }
- // Game over
- function gameOver() {
- alert('Game Over! You made it to level ' + level + '.');
- gameStarted = false;
- sequence = [];
- playerSequence = [];
- level = 0;
- flashing = false;
- $('.start-button').text('Start Game');
- var audio = new Audio('https://s3.amazonaws.com/freecodecamp/simonSound4.mp3');
- audio.play();
- }
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement