Write an RPS game in HTML, CSS, and JS. Give it a dark, sleek UI of Material You. Set…
- User
Write an RPS game in HTML, CSS, and JS. Give it a dark, sleek UI of Material You. Set the Rock, Paper, and Scissors as vector SVGs in buttons...
Make an animation of the bot thinking.
Make sure to give it smooth animations/transitions.
- Assistant
Certainly! Below is the implementation of a Rock-Paper-Scissors (RPS) game using HTML, CSS, and JavaScript. The game features a dark, sleek UI inspired by Material You design principles, uses SVG icons for the choices, includes a bot thinking animation, and ensures smooth animations and transitions.
HTML (
index.html):<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Rock Paper Scissors</title> <!-- Link to external CSS --> <link rel="stylesheet" href="styles.css"> <!-- Google Fonts --> <link href="https://fonts.googleapis.com/css2?family=Roboto&display=swap" rel="stylesheet"> </head> <body> <div class="game-container"> <h1>Rock Paper Scissors</h1> <div class="score-board"> <div id="user-label" class="badge">You</div> <div id="comp-label" class="badge">Bot</div> <div class="score"> <span id="user-score">0</span> : <span id="comp-score">0</span> </div> </div> <div class="result"> <p>Make your move!</p> </div> <div class="choices"> <button id="rock" class="choice" aria-label="Rock"> <!-- Rock SVG --> <svg viewBox="0 0 64 64"> <path d="M22,2 L42,2 L60,22 L60,42 L42,62 L22,62 L2,42 L2,22 Z" fill="currentColor"/> </svg> </button> <button id="paper" class="choice" aria-label="Paper"> <!-- Paper SVG --> <svg viewBox="0 0 64 64"> <rect x="12" y="2" width="40" height="60" rx="5" ry="5" fill="currentColor"/> </svg> </button> <button id="scissors" class="choice" aria-label="Scissors"> <!-- Scissors SVG --> <svg viewBox="0 0 64 64"> <circle cx="22" cy="22" r="10" fill="currentColor"/> <circle cx="42" cy="42" r="10" fill="currentColor"/> <line x1="32" y1="32" x2="52" y2="12" stroke="currentColor" stroke-width="4"/> <line x1="12" y1="52" x2="32" y2="32" stroke="currentColor" stroke-width="4"/> </svg> </button> </div> <div class="bot-thinking"> <p>Bot is thinking...</p> <div class="spinner"></div> </div> </div> <!-- Link to external JS --> <script src="script.js"></script> </body> </html>
CSS (
styles.css):/* Material You Dark Theme Colors */ :root { --primary-color: #bb86fc; --on-primary-color: #ffffff; --background-color: #121212; --surface-color: #1e1e1e; --on-surface-color: #ffffff; --accent-color: #03dac6; } body { margin: 0; padding: 0; font-family: 'Roboto', sans-serif; background-color: var(--background-color); color: var(--on-surface-color); } .game-container { text-align: center; padding: 20px; } h1 { margin-bottom: 10px; color: var(--primary-color); font-weight: normal; } .score-board { display: flex; justify-content: center; align-items: center; margin-bottom: 20px; } .badge { background-color: var(--surface-color); color: var(--on-surface-color); padding: 5px 15px; margin: 0 10px; border-radius: 20px; font-weight: bold; } .score { font-size: 1.5rem; color: var(--primary-color); } .result { margin-bottom: 20px; min-height: 40px; } .result p { transition: opacity 0.3s ease; } .choices { display: flex; justify-content: center; gap: 30px; } .choice { background-color: var(--surface-color); border: none; border-radius: 50%; padding: 20px; cursor: pointer; transition: background-color 0.3s ease, transform 0.2s ease; outline: none; } .choice:hover { background-color: var(--accent-color); transform: scale(1.1); } .choice:active { transform: scale(1.05); } .choice svg { width: 64px; height: 64px; fill: var(--on-surface-color); } .bot-thinking { display: none; margin-top: 20px; } .bot-thinking p { margin-bottom: 10px; } .spinner { border: 4px solid var(--surface-color); border-top: 4px solid var(--primary-color); border-radius: 50%; width: 32px; height: 32px; animation: spin 1s linear infinite; margin: 0 auto; } @keyframes spin { to { transform: rotate(360deg); } }
JavaScript (
script.js):// Get elements from the DOM const userScoreSpan = document.getElementById('user-score'); const compScoreSpan = document.getElementById('comp-score'); const resultDiv = document.querySelector('.result > p'); const botThinkingDiv = document.querySelector('.bot-thinking'); const choices = document.querySelectorAll('.choice'); let userScore = 0; let compScore = 0; // Function to get the bot's choice function getComputerChoice() { const options = ['rock', 'paper', 'scissors']; return options[Math.floor(Math.random() * options.length)]; } // Function to convert choice to proper formatting function convertToWord(choice) { return choice.charAt(0).toUpperCase() + choice.slice(1); } // Functions to handle win, lose, and draw function win(userChoice, computerChoice) { userScore++; userScoreSpan.textContent = userScore; resultDiv.textContent = `${convertToWord(userChoice)} beats ${convertToWord(computerChoice)}. You win! 🎉`; flashResult('win'); } function lose(userChoice, computerChoice) { compScore++; compScoreSpan.textContent = compScore; resultDiv.textContent = `${convertToWord(userChoice)} loses to ${convertToWord(computerChoice)}. You lost! 😢`; flashResult('lose'); } function draw(userChoice, computerChoice) { resultDiv.textContent = `${convertToWord(userChoice)} equals ${convertToWord(computerChoice)}. It's a draw! 🤝`; flashResult('draw'); } // Function to flash the result with color function flashResult(result) { const colors = { win: 'green', lose: 'red', draw: 'gray' }; resultDiv.style.opacity = 0; setTimeout(() => { resultDiv.style.opacity = 1; resultDiv.style.color = colors[result]; }, 100); } // Main game function function game(userChoice) { // Display bot thinking animation botThinkingDiv.style.display = 'block'; resultDiv.textContent = ''; // Disable choices during bot thinking choices.forEach(choice => choice.disabled = true); // Simulate bot thinking time setTimeout(() => { const computerChoice = getComputerChoice(); switch (userChoice + computerChoice) { case 'rockscissors': case 'paperrock': case 'scissorspaper': win(userChoice, computerChoice); break; case 'rockpaper': case 'paperscissors': case 'scissorsrock': lose(userChoice, computerChoice); break; default: draw(userChoice, computerChoice); break; } // Hide bot thinking and enable choices botThinkingDiv.style.display = 'none'; choices.forEach(choice => choice.disabled = false); }, 2000); } // Add event listeners to choices choices.forEach(choice => { choice.addEventListener('click', () => game(choice.id)); });
Instructions:
- SVG Icons: The SVG icons for Rock, Paper, and Scissors are included directly in the HTML within the buttons. These SVGs are simple representations and can be customized or replaced with more elaborate SVGs if desired.
- Fonts: The Roboto font is linked from Google Fonts in the
<head>section of the HTML. - Bot Thinking Animation: When you make a choice, the "Bot is thinking..." message along with a spinner animation appears for 2 seconds to simulate the bot's decision-making process.
- Transitions and Animations:
- Buttons scale up slightly on hover and press for a tactile feel.
- The result message fades in smoothly after each round.
- The spinner uses a CSS keyframe animation for smooth rotation.
- Responsive Design: The game layout is simple and should display well on various screen sizes. For full responsiveness, additional media queries may be added as needed.
Customization Tips:
- Colors: You can adjust the color variables in the CSS
:rootto change the theme colors. - Animations: Modify the durations and easing functions in the CSS transitions and keyframes to tweak the animation feel.
- Bot Thinking Time: In
script.js, thesetTimeoutduration of 2000 milliseconds simulates the bot's thinking time. You can adjust this value to make the bot respond faster or slower. - Result Flashes: The
flashResultfunction inscript.jschanges the color of the result text briefly. You can customize the colors or animations as desired.
Preview:
Here's how the game looks and functions:
-
Initial Screen:
- Displays the game title, scoreboards, and prompts the player to make a move.
- The choices are represented by SVG icons for Rock, Paper, and Scissors.
-
Making a Move:
- Click on one of the choices.
- The bot thinking animation appears for 2 seconds.
- After the delay, the result is displayed with a message indicating whether you won, lost, or drew.
- The scores are updated accordingly.
- The result message briefly flashes in color to indicate the outcome (green for win, red for lose, gray for draw).
Files Structure:
Make sure your files are organized as follows:
index.html- The main HTML file.styles.css- The CSS file linked from the HTML.script.js- The JavaScript file linked from the HTML.- (Optional) Images or additional assets can be placed in an
assetsfolder.
Feel free to further enhance the game by adding features such as a reset button, sound effects, or additional rounds. Enjoy your sleek, animated Rock-Paper-Scissors game!