make a tiny game where i can cook a burger on a stove in js
- User
make a tiny game where i can cook a burger on a stove in js
- Assistant
Creating a simple JavaScript game where you can cook a burger on a stove can be a fun little project. Below is a basic example using HTML, CSS, and JavaScript to accomplish this. This won't involve sophisticated graphics, but it should give you a starting point that you can expand upon:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Cook a Burger Game</title> <style> #stove { width: 200px; height: 200px; background: #333; position: relative; margin: 50px auto; border-radius: 10px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.5); } #burger { width: 100px; height: 100px; background: brown; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); border-radius: 50%; } #message { text-align: center; font-size: 18px; margin-top: 20px; } button { display: block; margin: 20px auto; padding: 10px 20px; font-size: 16px; } </style> </head> <body> <div id="stove"> <div id="burger"></div> </div> <div id="message">Press "Cook" to start cooking!</div> <button id="cookButton">Cook</button> <script> const burger = document.getElementById('burger'); const message = document.getElementById('message'); const cookButton = document.getElementById('cookButton'); let cooking = false; let cookTime = 0; cookButton.addEventListener('click', () => { if (!cooking) { cooking = true; cookButton.disabled = true; message.textContent = "Cooking in progress..."; const cookInterval = setInterval(() => { cookTime++; if (cookTime === 5) { burger.style.backgroundColor = 'darkbrown'; message.textContent = "Burger is perfectly cooked!"; clearInterval(cookInterval); cookButton.disabled = false; cooking = false; cookTime = 0; } else if (cookTime > 5) { burger.style.backgroundColor = 'black'; message.textContent = "Oh no! The burger is burnt!"; clearInterval(cookInterval); cookButton.disabled = false; cooking = false; cookTime = 0; } }, 1000); } }); </script> </body> </html>How it Works:
-
HTML Structure: This consists of a
divrepresenting the stove and anotherdivfor the burger. There's also a message area and a button to start cooking. -
CSS Styling: Basic styles are provided for the stove, burger, and the UI elements to give them a simple, playable appearance.
-
JavaScript Logic:
- An event listener is set on the "Cook" button.
- When the button is clicked, the cooking process starts.
- The
setIntervalfunction simulates the cooking process, changing the color of the burger as it "cooks." - After 5 seconds, the burger is perfectly cooked, and after that, it turns black to signify it is burnt.
This is a tiny game, but you can expand upon it by adding more interactivity, graphics, or challenges like customizable cooking times or additional ingredients.
-
- User
make it so after the burger is cooked, a link appears to a portfolio site
- Assistant
Certainly! You can modify the script to include a link to a portfolio site that appears once the burger is perfectly cooked. Below is the revised code with this feature added:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Cook a Burger Game</title> <style> #stove { width: 200px; height: 200px; background: #333; position: relative; margin: 50px auto; border-radius: 10px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.5); } #burger { width: 100px; height: 100px; background: brown; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); border-radius: 50%; } #message { text-align: center; font-size: 18px; margin-top: 20px; } button { display: block; margin: 20px auto; padding: 10px 20px; font-size: 16px; } </style> </head> <body> <div id="stove"> <div id="burger"></div> </div> <div id="message">Press "Cook" to start cooking!</div> <button id="cookButton">Cook</button> <div id="portfolioLink" style="text-align: center; margin-top: 20px;"></div> <script> const burger = document.getElementById('burger'); const message = document.getElementById('message'); const cookButton = document.getElementById('cookButton'); const portfolioLink = document.getElementById('portfolioLink'); let cooking = false; let cookTime = 0; cookButton.addEventListener('click', () => { if (!cooking) { cooking = true; cookButton.disabled = true; message.textContent = "Cooking in progress..."; portfolioLink.innerHTML = ''; // Clear any previous links const cookInterval = setInterval(() => { cookTime++; if (cookTime === 5) { burger.style.backgroundColor = 'darkbrown'; message.textContent = "Burger is perfectly cooked!"; clearInterval(cookInterval); cookButton.disabled = false; cooking = false; cookTime = 0; // Display the link once the burger is perfectly cooked portfolioLink.innerHTML = `<a href="https://your-portfolio.com" target="_blank">Visit my portfolio</a>`; } else if (cookTime > 5) { burger.style.backgroundColor = 'black'; message.textContent = "Oh no! The burger is burnt!"; clearInterval(cookInterval); cookButton.disabled = false; cooking = false; cookTime = 0; } }, 1000); } }); </script> </body> </html>Key Changes:
- Added a new
divelement with the IDportfolioLinkto hold the link. - In the JavaScript, once the burger is perfectly cooked, a link to the portfolio (
https://your-portfolio.com) is displayed by setting the innerHTML of theportfolioLinkelement. - Make sure to replace
https://your-portfolio.comwith your actual portfolio URL.
This enhancement encourages players to interact with the game and gives them a call to action by visiting your portfolio once they've successfully cooked the burger.
- Added a new