Here's a simple HTML, CSS, and JavaScript implementation of a Cookie Clicker-style game:
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Cookie Clicker</title>
<style>
body {
font-family: 'Arial', sans-serif;
text-align: center;
background-color: #f5f5dc;
margin: 0;
padding: 20px;
}
#game-container {
max-width: 800px;
margin: 0 auto;
background-color: #fff;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
#cookie {
width: 200px;
height: 200px;
cursor: pointer;
transition: transform 0.1s;
margin: 20px auto;
background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><circle cx="50" cy="50" r="45" fill="%23D2691E"/><circle cx="30" cy="30" r="5" fill="%238B4513"/><circle cx="70" cy="30" r="5" fill="%238B4513"/><circle cx="50" cy="60" r="8" fill="%238B4513"/><circle cx="20" cy="70" r="4" fill="%238B4513"/><circle cx="80" cy="50" r="3" fill="%238B4513"/></svg>');
background-size: contain;
}
#cookie:active {
transform: scale(0.95);
}
#counters {
display: flex;
justify-content: space-around;
margin: 20px 0;
}
.counter {
font-size: 24px;
font-weight: bold;
}
#upgrades {
display: flex;
flex-wrap: wrap;
justify-content: center;
gap: 10px;
margin-top: 20px;
}
.upgrade {
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 15px;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s;
}
.upgrade:hover {
background-color: #45a049;
}
.upgrade:disabled {
background-color: #cccccc;
cursor: not-allowed;
}
#achievements {
margin-top: 30px;
text-align: left;
}
.achievement {
padding: 5px;
margin: 5px 0;
background-color: #f0f0f0;
border-radius: 3px;
}
.achievement.unlocked {
background-color: #ffd700;
font-weight: bold;
}
</style>
</head>
<body>
<div id="game-container">
<h1>Cookie Clicker</h1>
<div id="counters">
<div class="counter">
Cookies: <span id="cookies">0</span>
</div>
<div class="counter">
Per Second: <span id="cps">0</span>
</div>
</div>
<div id="cookie"></div>
<div id="upgrades">
<button class="upgrade" onclick="buyUpgrade('cursor')" id="cursor-btn">
Cursor (10 cookies)<br>
+0.1 cookies per second<br>
Owned: <span id="cursor-count">0</span>
</button>
<button class="upgrade" onclick="buyUpgrade('grandma')" id="grandma-btn">
Grandma (50 cookies)<br>
+0.5 cookies per second<br>
Owned: <span id="grandma-count">0</span>
</button>
<button class="upgrade" onclick="buyUpgrade('farm')" id="farm-btn">
Farm (200 cookies)<br>
+2 cookies per second<br>
Owned: <span id="farm-count">0</span>
</button>
<button class="upgrade" onclick="buyUpgrade('mine')" id="mine-btn">
Mine (500 cookies)<br>
+5 cookies per second<br>
Owned: <span id="mine-count">0</span>
</button>
</div>
<div id="achievements">
<h2>Achievements</h2>
<div class="achievement" id="achievement-10">Bake 10 cookies</div>
<div class="achievement" id="achievement-100">Bake 100 cookies</div>
<div class="achievement" id="achievement-1000">Bake 1,000 cookies</div>
<div class="achievement" id="achievement-10000">Bake 10,000 cookies</div>
</div>
</div>
<script>
// Game state
let cookies = 0;
let cookiesPerSecond = 0;
let upgrades = {
cursor: { cost: 10, cps: 0.1, owned: 0 },
grandma: { cost: 50, cps: 0.5, owned: 0 },
farm: { cost: 200, cps: 2, owned: 0 },
mine: { cost: 500, cps: 5, owned: 0 }
};
// DOM elements
const cookieElement = document.getElementById('cookie');
const cookiesElement = document.getElementById('cookies');
const cpsElement = document.getElementById('cps');
// Initialize the game
function init() {
updateDisplay();
// Set up cookie click event
cookieElement.addEventListener('click', () => {
cookies += 1;
updateDisplay();
checkAchievements();
});
// Set up passive income
setInterval(() => {
cookies += cookiesPerSecond / 10;
updateDisplay();
checkAchievements();
}, 100);
}
// Update all displayed values
function updateDisplay() {
cookiesElement.textContent = Math.floor(cookies);
cpsElement.textContent = cookiesPerSecond.toFixed(1);
// Update upgrade buttons
for (const [name, data] of Object.entries(upgrades)) {
const btn = document.getElementById(`${name}-btn`);
const countElement = document.getElementById(`${name}-count`);
countElement.textContent = data.owned;
btn.disabled = cookies < data.cost;
btn.innerHTML = `${name.charAt(0).toUpperCase() + name.slice(1)} (${data.cost} cookies)<br>+${data.cps} cookies per second<br>Owned: <span id="${name}-count">${data.owned}</span>`;
}
}
// Buy an upgrade
function buyUpgrade(name) {
const upgrade = upgrades[name];
if (cookies >= upgrade.cost) {
cookies -= upgrade.cost;
upgrade.owned += 1;
cookiesPerSecond += upgrade.cps;
// Increase cost for next purchase (optional)
upgrade.cost = Math.floor(upgrade.cost * 1.15);
updateDisplay();
}
}
// Check for achievements
function checkAchievements() {
const total = Math.floor(cookies);
if (total >= 10) {
document.getElementById('achievement-10').classList.add('unlocked');
}
if (total >= 100) {
document.getElementById('achievement-100').classList.add('unlocked');
}
if (total >= 1000) {
document.getElementById('achievement-1000').classList.add('unlocked');
}
if (total >= 10000) {
document.getElementById('achievement-10000').classList.add('unlocked');
}
}
// Start the game
init();
</script>
</body>
</html>