How to Build a Fun HTML Game

How to Build a Fun HTML Game

Building a game with HTML, CSS, and JavaScript is an exciting way to learn coding! In this tutorial, we’ll create a simple “Catch the Circle” game where the player clicks on a moving circle to score points. Let’s get started!

1. Set Up Your HTML File

First, create a new HTML file. This file will contain the structure of your game. Here’s the basic code:

HTML File Example
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Catch the Circle Game</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <h1>Catch the Circle Game</h1>
    <div id="game-area">
        <div id="circle"></div>
    </div>
    <p id="score">Score: 0</p>
    <script src="script.js"></script>
</body>
</html>
        

2. Add CSS for Styling

Create a CSS file called styles.css to style the game area and the circle. Here’s the code:

CSS Code Example
body {
    font-family: Arial, sans-serif;
    text-align: center;
    background-color: #f9f9f9;
}
#game-area {
    width: 300px;
    height: 300px;
    margin: 20px auto;
    border: 2px solid #333;
    position: relative;
    background-color: #eef;
}
#circle {
    width: 50px;
    height: 50px;
    background-color: red;
    border-radius: 50%;
    position: absolute;
}
#score {
    font-size: 1.5em;
    margin-top: 20px;
}
        

3. Add JavaScript for Game Logic

Create a JavaScript file called script.js to handle the game logic, including moving the circle and updating the score. Here’s the code:

JavaScript Code Example
const circle = document.getElementById("circle");
const gameArea = document.getElementById("game-area");
const scoreDisplay = document.getElementById("score");

let score = 0;

// Function to move the circle to a random position
function moveCircle() {
    const x = Math.random() * (gameArea.offsetWidth - circle.offsetWidth);
    const y = Math.random() * (gameArea.offsetHeight - circle.offsetHeight);
    circle.style.left = `${x}px`;
    circle.style.top = `${y}px`;
}

// Increase score when the circle is clicked
circle.addEventListener("click", () => {
    score++;
    scoreDisplay.textContent = `Score: ${score}`;
    moveCircle();
});

// Move the circle every 2 seconds
setInterval(moveCircle, 2000);
        

4. How It Works

Game Preview
  • The HTML file sets up the structure of the game, including the game area and the circle.
  • The CSS file styles the game to make it look neat and visually appealing.
  • The JavaScript file makes the circle move randomly and updates the score when the player clicks on the circle.

5. Test Your Game

Open your index.html file in a browser and play your game! Click on the red circle as it moves around to score points. You can customize the game by changing the speed or size of the circle.

6. Bonus Ideas

  • Add a timer to make the game more challenging.
  • Change the color of the circle each time it moves.
  • Keep a high score that updates after each game.

Congratulations! You’ve built a fun and interactive game using HTML, CSS, and JavaScript. Share it with your friends and keep experimenting to make it even better!

© 2024 KidsKod | All Rights Reserved

Next Post Previous Post
1 Comments
  • KidsKod
    KidsKod April 1, 2025 at 1:45 AM

    Good love it

Add Comment
comment url