write for me an incremental game using HTML, CSS and JS where "Cubes" spawn randomly…
- User
write for me an incremental game using HTML, CSS and JS where "Cubes" spawn randomly around the screen every 5 seconds, clicking on cubes collects them and then they can be spent on upgrades, first upgrade reduces the waiting time for cubes and second one increases amount of cubes gained on collect. There's also 1/100 chance that cube spawned will be a "golden cube" which can be spent on another upgrades
- Assistant
Sure! Below is a simple incremental game implemented using HTML, CSS, and JavaScript as per your specifications. The game features cubes that spawn randomly on the screen every few seconds. Clicking on the cubes collects them, and you can spend the collected cubes on upgrades to enhance your gameplay.
Game Features:
- Cubes spawning randomly every 5 seconds.
- Click on cubes to collect them.
- First upgrade: Reduces the spawning time of cubes.
- Second upgrade: Increases the number of cubes gained per collection.
- Golden Cubes: There's a 1/100 chance that a spawned cube is a golden cube.
- Golden cubes can be spent on special upgrades.
HTML Code (
index.html):<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Cube Collector Game</title> <link rel="stylesheet" href="styles.css"> </head> <body> <h1>Cube Collector Game</h1> <div id="stats"> <p>Total Cubes: <span id="cubeCount">0</span></p> <p>Golden Cubes: <span id="goldenCubeCount">0</span></p> </div> <div id="gameArea"></div> <div id="upgrades"> <h2>Upgrades</h2> <button id="spawnUpgrade">Reduce Spawn Time (Cost: <span id="spawnUpgradeCost">10</span> Cubes)</button> <button id="cubeUpgrade">Increase Cubes per Click (Cost: <span id="cubeUpgradeCost">15</span> Cubes)</button> <button id="goldenUpgrade">Golden Upgrade (Cost: <span id="goldenUpgradeCost">5</span> Golden Cubes)</button> </div> <script src="script.js"></script> </body> </html>
CSS Code (
styles.css):body { font-family: Arial, sans-serif; text-align: center; } #gameArea { position: relative; width: 800px; height: 600px; margin: 0 auto; border: 1px solid #ccc; background-color: #f9f9f9; } .cube { width: 50px; height: 50px; background-color: #3498db; position: absolute; cursor: pointer; } .goldenCube { background-color: gold; } #upgrades { margin-top: 20px; } button { margin: 5px; padding: 10px 20px; }
JavaScript Code (
script.js):// Game Variables let cubeCount = 0; let goldenCubeCount = 0; let spawnTime = 5000; // Time in milliseconds let cubesPerClick = 1; // Upgrade Costs let spawnUpgradeCost = 10; let cubeUpgradeCost = 15; let goldenUpgradeCost = 5; // DOM Elements const cubeCountElem = document.getElementById('cubeCount'); const goldenCubeCountElem = document.getElementById('goldenCubeCount'); const gameArea = document.getElementById('gameArea'); const spawnUpgradeBtn = document.getElementById('spawnUpgrade'); const cubeUpgradeBtn = document.getElementById('cubeUpgrade'); const goldenUpgradeBtn = document.getElementById('goldenUpgrade'); const spawnUpgradeCostElem = document.getElementById('spawnUpgradeCost'); const cubeUpgradeCostElem = document.getElementById('cubeUpgradeCost'); const goldenUpgradeCostElem = document.getElementById('goldenUpgradeCost'); // Functions function spawnCube() { // Create a new cube element const cube = document.createElement('div'); let isGolden = Math.random() < 0.01; // 1/100 chance cube.classList.add('cube'); if (isGolden) { cube.classList.add('goldenCube'); } // Random position within game area const x = Math.random() * (gameArea.clientWidth - 50); const y = Math.random() * (gameArea.clientHeight - 50); cube.style.left = x + 'px'; cube.style.top = y + 'px'; // Add click event to cube cube.addEventListener('click', function () { if (isGolden) { goldenCubeCount += 1; goldenCubeCountElem.textContent = goldenCubeCount; } else { cubeCount += cubesPerClick; cubeCountElem.textContent = cubeCount; } gameArea.removeChild(cube); }); // Add cube to game area gameArea.appendChild(cube); } function startSpawningCubes() { spawnCube(); setTimeout(startSpawningCubes, spawnTime); } // Upgrade Functions spawnUpgradeBtn.addEventListener('click', function () { if (cubeCount >= spawnUpgradeCost) { cubeCount -= spawnUpgradeCost; cubeCountElem.textContent = cubeCount; spawnTime = Math.max(1000, spawnTime - 500); // Reduce spawn time by 0.5s, minimum 1s spawnUpgradeCost = Math.ceil(spawnUpgradeCost * 1.5); spawnUpgradeCostElem.textContent = spawnUpgradeCost; } else { alert('Not enough cubes!'); } }); cubeUpgradeBtn.addEventListener('click', function () { if (cubeCount >= cubeUpgradeCost) { cubeCount -= cubeUpgradeCost; cubeCountElem.textContent = cubeCount; cubesPerClick += 1; cubeUpgradeCost = Math.ceil(cubeUpgradeCost * 1.7); cubeUpgradeCostElem.textContent = cubeUpgradeCost; } else { alert('Not enough cubes!'); } }); goldenUpgradeBtn.addEventListener('click', function () { if (goldenCubeCount >= goldenUpgradeCost) { goldenCubeCount -= goldenUpgradeCost; goldenCubeCountElem.textContent = goldenCubeCount; // Implement your golden cube upgrade effect here alert('Golden Upgrade Purchased! (Effect not implemented)'); goldenUpgradeCost = Math.ceil(goldenUpgradeCost * 2); goldenUpgradeCostElem.textContent = goldenUpgradeCost; } else { alert('Not enough golden cubes!'); } }); // Start the game startSpawningCubes();
Instructions to Run the Game:
-
Create three files in the same directory:
index.html(HTML code)styles.css(CSS code)script.js(JavaScript code)
-
Copy and paste the respective code snippets into these files.
-
Open
index.htmlin a web browser to start the game.
How the Game Works:
-
Cubes Spawn: Every few seconds, a cube will appear at a random position within the game area.
-
Collecting Cubes: Click on the cubes to collect them. The number of cubes you have is displayed at the top.
-
Upgrades:
- Reduce Spawn Time: Lowers the time between cube spawns, allowing cubes to appear more frequently.
- Increase Cubes per Click: Increases the number of cubes you get every time you click a cube.
- Golden Upgrade: A special upgrade that can be purchased with golden cubes.
-
Golden Cubes:
- Occasionally, a golden cube will spawn (1% chance).
- Golden cubes look different and are used to purchase special upgrades.
Customizing the Game:
- Adjust Spawn Rates: Modify the
spawnTimevariable or the spawn time reduction in the upgrade function. - Change Upgrade Costs: Tweak the
spawnUpgradeCost,cubeUpgradeCost, and their growth rates to balance the game. - Implement Golden Upgrades: In the
goldenUpgradeBtnclick event, you can add effects such as doubling the cube per click or increasing the chance of golden cubes spawning.
Additional Features to Consider:
- Cube Despawn: Make cubes disappear if not collected within a certain time.
- Multiple Levels of Upgrades: Add more upgrades or levels to existing ones.
- Audio Feedback: Play sounds when cubes are collected or upgrades are purchased.
- Save and Load Game: Implement saving the game state to local storage so progress isn't lost on page refresh.
Enjoy your new incremental game! If you have any questions or need further customization, feel free to ask.