make 1 html5 script without using any libraries that makes cookie clicker or a game…
- User
make 1 html5 script without using any libraries that makes cookie clicker or a game based like that
- Assistant
Sure! Below is a simple HTML5 game similar to Cookie Clicker. It allows you to click on a cookie image to earn cookies and purchase upgrades that generate cookies automatically. This script doesn't use any external libraries and is self-contained within a single HTML file.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Simple Cookie Clicker</title> <style> body { text-align: center; font-family: Arial, sans-serif; } #cookie { width: 200px; cursor: pointer; } #stats { margin-top: 20px; } #upgrades { margin-top: 30px; } .upgrade { margin: 10px 0; } .upgrade button { margin-left: 10px; } </style> </head> <body> <h1>Simple Cookie Clicker</h1> <img id="cookie" src="https://i.imgur.com/VO6Pl5Q.png" alt="Cookie"> <div id="stats"> <h2>Cookies: <span id="cookies">0</span></h2> <h3>Cookies per second: <span id="cps">0</span></h3> </div> <div id="upgrades"> <h2>Upgrades:</h2> <div class="upgrade"> <span>Cursor (+1 CPS) - Cost: <span id="cursorCost">15</span></span> <button onclick="buyUpgrade('cursor')">Buy</button> </div> <div class="upgrade"> <span>Grandma (+5 CPS) - Cost: <span id="grandmaCost">100</span></span> <button onclick="buyUpgrade('grandma')">Buy</button> </div> <div class="upgrade"> <span>Farm (+10 CPS) - Cost: <span id="farmCost">500</span></span> <button onclick="buyUpgrade('farm')">Buy</button> </div> </div> <script> // Game variables var cookies = 0; var cps = 0; // cookies per second var upgrades = { cursor: {cost: 15, cps: 1, owned: 0}, grandma: {cost: 100, cps: 5, owned: 0}, farm: {cost: 500, cps: 10, owned: 0} }; // DOM elements var cookiesElem = document.getElementById('cookies'); var cpsElem = document.getElementById('cps'); // Cookie click event document.getElementById('cookie').addEventListener('click', function() { cookies++; updateCookies(); }); // Update cookie count display function updateCookies() { cookiesElem.textContent = Math.floor(cookies); } // Update CPS display function updateCPS() { cps = 0; for (var key in upgrades) { cps += upgrades[key].cps * upgrades[key].owned; } cpsElem.textContent = cps; } // Purchase an upgrade function buyUpgrade(upgradeName) { var upgrade = upgrades[upgradeName]; if (cookies >= upgrade.cost) { cookies -= upgrade.cost; upgrade.owned++; upgrade.cost = Math.floor(upgrade.cost * 1.15); // Increase cost by 15% document.getElementById(upgradeName + 'Cost').textContent = upgrade.cost; updateCookies(); updateCPS(); } else { alert('Not enough cookies!'); } } // Generate cookies over time setInterval(function() { cookies += cps / 10; // Update 10 times per second for smoother increment updateCookies(); }, 100); </script> </body> </html>Instructions:
-
Save the code: Copy the code above and paste it into a text editor. Save the file with an
.htmlextension, for example,cookie_clicker.html. -
Open in a browser: Double-click the saved file to open it in your web browser.
-
Play the game: Click on the cookie image to earn cookies. Once you have enough cookies, you can purchase upgrades that generate cookies automatically.
Game Features:
-
Cookie Clicking: Click on the cookie image to increase your cookie count by 1 per click.
-
Upgrades: Purchase upgrades using your cookies:
- Cursor: Costs 15 cookies, generates +1 cookie per second (CPS).
- Grandma: Costs 100 cookies, generates +5 CPS.
- Farm: Costs 500 cookies, generates +10 CPS.
-
Dynamic Costs: The cost of each upgrade increases by 15% after each purchase.
-
Cookies Per Second: Displays the total CPS from all owned upgrades.
Notes:
-
No External Libraries: This game is built using plain HTML, CSS, and JavaScript without any external libraries or frameworks.
-
Cookie Image: The cookie image is loaded from an external URL (
https://i.imgur.com/VO6Pl5Q.png). Ensure you have an internet connection, or you can replace thesrcattribute with a local image path.
Understanding the Code:
-
HTML Structure: The HTML sets up the game interface, including the cookie image, stats display, and upgrade buttons.
-
CSS Styling: Basic styles are applied to center the content and style the elements.
-
JavaScript Logic: The game logic is implemented in JavaScript:
- Variables: Track the number of cookies, cookies per second, and upgrades.
- Event Listeners: Handle clicks on the cookie image and upgrade buttons.
- Functions: Update the display of cookies and CPS, handle purchasing upgrades, and generate cookies over time.
- Intervals: Uses
setIntervalto update the cookie count based on CPS every 100 milliseconds for smooth incrementing.
Feel free to modify and enhance the game by adding more features, upgrades, and styling!
-