Below is an example of a simple Cookie Clicker–style game implemented in HTML, CSS, and JavaScript. You can save the code to an HTML file (for example, index.html) and open it in your browser to play.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Cookie Clicker Game</title>
<style>
body {
text-align: center;
background-color: #f2f2f2;
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
}
h1 {
color: #333;
}
#cookie {
width: 200px;
height: 200px;
cursor: pointer;
transition: transform 0.1s ease-in-out;
}
#cookie:hover {
transform: scale(1.05);
}
#clickCount {
font-size: 2em;
margin: 20px;
}
</style>
</head>
<body>
<h1>Cookie Clicker Game</h1>
<p id="clickCount">Cookies: 0</p>
<!-- You can use any cookie image URL; this example uses a sample cookie image -->
<img id="cookie" src="https://upload.wikimedia.org/wikipedia/commons/1/11/Chocolate_chip_cookie.jpg" alt="Cookie">
<script>
// Initialize cookie count
let count = 0;
// Reference to the HTML elements
const cookie = document.getElementById('cookie');
const clickCount = document.getElementById('clickCount');
// Add event listener to the cookie picture
cookie.addEventListener('click', function() {
count++;
clickCount.textContent = `Cookies: ${count}`;
});
</script>
</body>
</html>
How It Works
-
HTML Structure:
- The
<h1> displays the game title.
- The
<p> with id="clickCount" shows the number of cookies clicked.
- The
<img> with id="cookie" is the clickable cookie image.
-
CSS Styling:
- The CSS centers the content and adds a pleasant background.
- A hover effect is applied on the cookie image to make it interactive.
-
JavaScript Functionality:
- A counter (
count) is initialized to zero.
- An event listener on the cookie image listens for click events. Every click increments the counter and updates the displayed cookie count.
Feel free to enhance this basic version by adding features such as upgrades, animations, or saving the score in local storage. Enjoy coding and have fun clicking cookies!