Write a BOM javascript application that floats 6 colored balloons around the web page.
User must click on each balloon to pop it.
Program keeps track of time and reports to user their time to win. Balloons move faster as each is popped.
Here's a fun and interactive JavaScript Balloon Popping Game using the BOM (Browser Object Model). The game floats 6 colored balloons around the webpage, and users must click on each balloon to pop them. The program tracks the time, and as each balloon is popped, the remaining balloons move faster.
Once all balloons are popped, the user's time to win is displayed.
Key Features:
Balloon movement: The balloons float randomly around the screen using JavaScript.
Balloon popping: The user clicks on each balloon to pop it.
Increased speed: The remaining balloons move faster after each balloon is popped.
Timer: The game keeps track of the time it takes for the user to pop all balloons.
There are 6 balloon <div> elements with different colors. Each balloon has the class balloon, and each one is assigned a unique id (balloon1 to balloon6).
A div with id="gameOverMessage" displays the time taken once all balloons are popped.
CSS Styling:
The balloon class gives each balloon a size, color, and makes it round with the border-radius property.
The game over message is initially hidden and displayed in the center of the page when the game ends.
JavaScript Functionality:
Starting the Game: When the page loads, the startGame() function initializes the game by placing the balloons at random positions and starting their movement.
Moving Balloons: Each balloon moves randomly around the screen using requestAnimationFrame() to create smooth animations. The speed increases as balloons are popped.
Popping Balloons: Each balloon has an event listener that triggers when clicked, hiding the balloon and increasing the speed of the remaining balloons.
Game Over: Once all balloons are popped, the total time taken is calculated and displayed on the screen.
Balloon Movement:
Balloons move continuously and bounce back when they hit the edges of the browser window.
The speed of the balloons increases as each balloon is popped by modifying the speedMultiplier.
Event Handling:
The click event is used to pop the balloons. Once clicked, the balloon is hidden using balloon.style.display = 'none', and the game checks if all balloons have been popped.
Timer:
The game tracks the time using Date.now() at the start and end of the game. When the last balloon is popped, the game calculates the total time taken and displays it.
How to Play:
The balloons will start floating around the page when the game starts.
The player must click on each balloon to pop it.
With each balloon popped, the remaining balloons move faster.
Once all balloons are popped, the game will display how long it took to pop them all.
Enhancements:
You could extend this game by:
Adding sounds when balloons are popped.
Increasing the number of balloons.
Keeping track of high scores and allowing players to restart the game.
This interactive project demonstrates the use of the BOM for handling window sizes, animations, and user interactions through events in a fun and educational way.
Want to print your doc? This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (