Below is a simple idle game implemented using HTML, CSS, and JavaScript. In this game, the player earns "coins" by clicking a button, and they can purchase upgrades to increase their coin earnings over time. The game also includes an auto-clicker feature as an upgrade.
Features:
- Click to Earn: Players earn coins by clicking a button.
- Upgrades: Players can buy upgrades to increase their earnings per click.
- Auto-Clicker: Players can buy an auto-clicker to earn coins automatically over time.
- Save Progress: The game saves progress using
localStorage.
Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple Idle Game</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
background-color: #f0f0f0;
padding: 20px;
}
#game-container {
background-color: #fff;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
max-width: 400px;
margin: 0 auto;
}
button {
padding: 10px 20px;
font-size: 16px;
margin: 10px;
cursor: pointer;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 5px;
}
button:disabled {
background-color: #cccccc;
cursor: not-allowed;
}
button:hover:enabled {
background-color: #45a049;
}
#coin-display {
font-size: 24px;
margin: 20px 0;
}
.upgrade-section {
margin: 20px 0;
}
</style>
</head>
<body>
<div id="game-container">
<h1>Idle Coin Clicker</h1>
<div id="coin-display">Coins: 0</div>
<button id="click-button">Click to Earn Coins!</button>
<div class="upgrade-section">
<h3>Upgrades</h3>
<p>Click Power: <span id="click-power">1</span> coins/click</p>
<button id="upgrade-click">Upgrade Click Power (Cost: <span id="click-upgrade-cost">10</span>)</button>
</div>
<div class="upgrade-section">
<h3>Auto-Clicker</h3>
<p>Auto Coins: <span id="auto-power">0</span> coins/second</p>
<button id="upgrade-auto">Buy Auto-Clicker (Cost: <span id="auto-upgrade-cost">50</span>)</button>
</div>
</div>
<script>
// Game state
let coins = 0;
let clickPower = 1;
let autoPower = 0;
let clickUpgradeCost = 10;
let autoUpgradeCost = 50;
// DOM elements
const coinDisplay = document.getElementById('coin-display');
const clickButton = document.getElementById('click-button');
const clickPowerDisplay = document.getElementById('click-power');
const autoPowerDisplay = document.getElementById('auto-power');
const clickUpgradeButton = document.getElementById('upgrade-click');
const autoUpgradeButton = document.getElementById('upgrade-auto');
const clickUpgradeCostDisplay = document.getElementById('click-upgrade-cost');
const autoUpgradeCostDisplay = document.getElementById('auto-upgrade-cost');
// Load saved game
function loadGame() {
const savedGame = JSON.parse(localStorage.getItem('idleGame'));
if (savedGame) {
coins = savedGame.coins || 0;
clickPower = savedGame.clickPower || 1;
autoPower = savedGame.autoPower || 0;
clickUpgradeCost = savedGame.clickUpgradeCost || 10;
autoUpgradeCost = savedGame.autoUpgradeCost || 50;
}
updateDisplay();
}
// Save game
function saveGame() {
const gameState = {
coins,
clickPower,
autoPower,
clickUpgradeCost,
autoUpgradeCost
};
localStorage.setItem('idleGame', JSON.stringify(gameState));
}
// Update display
function updateDisplay() {
coinDisplay.textContent = `Coins: ${Math.floor(coins)}`;
clickPowerDisplay.textContent = clickPower;
autoPowerDisplay.textContent = autoPower;
clickUpgradeCostDisplay.textContent = clickUpgradeCost;
autoUpgradeCostDisplay.textContent = autoUpgradeCost;
// Enable/disable upgrade buttons based on coin count
clickUpgradeButton.disabled = coins < clickUpgradeCost;
autoUpgradeButton.disabled = coins < autoUpgradeCost;
}
// Click to earn coins
clickButton.addEventListener('click', () => {
coins += clickPower;
updateDisplay();
saveGame();
});
// Upgrade click power
clickUpgradeButton.addEventListener('click', () => {
if (coins >= clickUpgradeCost) {
coins -= clickUpgradeCost;
clickPower += 1;
clickUpgradeCost = Math.floor(clickUpgradeCost * 1.5); // Increase cost
updateDisplay();
saveGame();
}
});
// Upgrade auto-clicker
autoUpgradeButton.addEventListener('click', () => {
if (coins >= autoUpgradeCost) {
coins -= autoUpgradeCost;
autoPower += 1;
autoUpgradeCost = Math.floor(autoUpgradeCost * 1.5); // Increase cost
updateDisplay();
saveGame();
}
});
// Auto-clicker logic (runs every second)
setInterval(() => {
coins += autoPower;
updateDisplay();
saveGame();
}, 1000);
// Load game on start
loadGame();
</script>
</body>
</html>
How to Play:
- Click to Earn: Click the "Click to Earn Coins!" button to earn coins.
- Upgrade Click Power: Spend coins to increase the number of coins earned per click.
- Buy Auto-Clicker: Spend coins to earn coins automatically every second.
- Progress Saves Automatically: Your progress is saved in the browser's
localStorage, so you can close the game and return later.
Explanation of the Code:
- HTML: Provides the structure of the game, including buttons, text displays, and sections for upgrades.
- CSS: Styles the game to make it visually appealing, with a clean and centered layout.
- JavaScript:
- Manages the game state (coins, click power, auto power, upgrade costs).
- Handles user interactions (clicking buttons to earn coins or buy upgrades).
- Updates the display to reflect the current game state.
- Saves and loads the game state using
localStorage.
- Uses
setInterval to simulate passive income from the auto-clicker.
How to Run:
- Copy the code into a file named
index.html.
- Open the file in a web browser.
- Start playing!
Ideas for Expansion:
- Add more upgrades (e.g., multipliers, temporary boosts).
- Include achievements or milestones.
- Add animations or sound effects for a more engaging experience.
- Implement a reset/prestige system for replayability.
This is a basic idle game, but it provides a solid foundation for further development!