Homegamescode Simon Says Game HTML CSS & Js Source Code By Mr Secret February 08, 2024 Simon Says Game HTML CSS & Js Source Code By Mr Secret TEXT Copy Download <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <style> body { font-family: 'Arial', sans-serif; background-color: #f4f4f4; text-align: center; } .container { max-width: 600px; margin: 50px auto; } h1 { color: #333; } #sequence { font-size: 24px; margin-top: 20px; } .button-container { margin-top: 20px; display: flex; justify-content: space-around; } .simon-button { background-color: #3498db; color: #fff; margin: 20px; padding: 10px 20px; font-size: 16px; border: none; border-radius: 5px; cursor: pointer; } #result { margin-top: 20px; font-size: 18px; } </style> <title>Simon Says Game</title> </head> <body> <div class="container"> <h1>Simon Says</h1> <div id="sequence"></div> <div class="button-container"> <button class="simon-button" onclick="addToSequence('red')">Red</button> <button class="simon-button" onclick="addToSequence('blue')">Blue</button> <button class="simon-button" onclick="addToSequence('green')">Green</button> <button class="simon-button" onclick="addToSequence('yellow')">Yellow</button> </div> <button class="simon-button" onclick="startGame()">Start Game</button> <div id="result"></div> </div> <script> var sequence = []; var playerSequence = []; var round = 1; function startGame() { sequence = []; playerSequence = []; round = 1; generateSequence(); displaySequence(); } function generateSequence() { for (var i = 0; i < round; i++) { var randomColor = getRandomColor(); sequence.push(randomColor); } } function getRandomColor() { var colors = ["red", "blue", "green", "yellow"]; var randomIndex = Math.floor(Math.random() * colors.length); return colors[randomIndex]; } function displaySequence() { var sequenceDisplay = document.getElementById("sequence"); sequenceDisplay.innerText = sequence.join(' '); setTimeout(function () { sequenceDisplay.innerText = ''; }, 1000 * round); } function addToSequence(color) { playerSequence.push(color); if (playerSequence.length === sequence.length) { checkSequence(); } } function checkSequence() { var isCorrect = playerSequence.every((value, index) => value === sequence[index]); if (isCorrect) { document.getElementById("result").innerText = "Correct! Next round."; round++; setTimeout(function () { playerSequence = []; generateSequence(); displaySequence(); }, 1000); } else { document.getElementById("result").innerText = "Incorrect! Game over."; } } </script> </body> </html> Tags: gamescode Facebook Twitter