About
Explore
Events & Classes
Support Us
Join
Sign In
speedcoder99
View Project Page
Run
Fullscreen
Pop the Balloons...CAN YOU POP 52?
HTML
CSS
JS
<!DOCTYPE HTML> <html> <head> <title>Page Title</title> <meta charset="utf-8"> <meta name="viewport" content="initial-scale=1.0,shrink-to-fit=no,width=device-width"> </head> <body> <h1>pop the balloons</h1> <h3>How many balloons can you pop in twenty seconds? Will it be all fifty-two?</h3> <div class="balloons"> <h1 class="game-over">game over</h1> </div> <div class="info"> <p>Time left: <span id="time-left">20</span> seconds</p> <p>Balloons popped: <span id="number-popped">0</span>/52</p> </div> </body> </html>
@import url('https://fonts.googleapis.com/css2?family=Bebas+Neue&display=swap'); @import url('https://fonts.googleapis.com/css2?family=Rampart+One&display=swap'); body { font-family: Rampart One; color: white; background-color: #ffb22e; text-align: center; } h1 { font-family: Bebas Neue; text-transform: uppercase; font-size: 40px; } p { font-weight: 700; } .balloons { background-color: white; border: 4px solid gray; width: 95%; height: auto; padding: 8px; position: relative; left: 50%; align-items: center; justify-content: center; transform: translateX(-50%); display: flex; flex-wrap: wrap; border-radius: 10px; color: black; } .balloons h1 { display: none; } .balloon { border-radius: 50%; width: 40px; height: 50px; position: relative; margin-right: 10px; margin-bottom: 31px; cursor: pointer; } .balloon::before { content: ''; background-color: silver; border-radius: 50%; width: 10px; height: 10px; position: absolute; top: 5px; left: 23px; opacity: .3; } .balloon::after { content: ''; background-color: silver; width: 1px; height: 31px; position: absolute; top: 42px; z-index: -1; }
// define variables var timer, score, popped, popsound, gameoversound, congrats; timer = 20; score = popped = 0; const balloon_colors = ["purple","blue","black","green"]; popsound = new Audio("https://justcodaborate.org/project_assets/popsound.wav"); gameoversound = new Audio("https://justcodaborate.org/project_assets/gameover.wav"); congrats = new Audio("https://justcodaborate.org/project_assets/congrats.wav"); // select elements var balloons = document.querySelector(".balloons"); var game_over = document.querySelector(".game-over"); var time_left = document.getElementById("time-left"); var number_popped = document.getElementById("number-popped"); // generate balloons for(var i = 0; i < 13; i++) { createPurpleBalloons(); createBlueBalloons(); createBlackBalloons(); createGreenBalloons(); } // end game and run timer var downloadTimer = setInterval(function(){ if(timer <= 0) { clearInterval(downloadTimer); game_over.style.display = "block"; time_left.innerHTML = "0"; if(popped==52) { game_over.innerHTML = "congrats! you popped all the balloons!"; congrats.play(); } else if(popped==0) { game_over.innerHTML = "game over <br> you popped no balloons!"; gameoversound.play(); hideBalloons(); } else if(popped==51) { game_over.innerHTML = "well done! you nearly popped all the balloons!"; congrats.play(); hideBalloons(); } else { game_over.innerHTML = "game over <br> you popped "+popped+"/52"; gameoversound.play(); hideBalloons(); } } else { time_left.innerHTML = timer; } timer -= 1; }, 1000); // create balloon functions function createBlackBalloons() { var balloon_div = document.createElement("div"); balloon_div.setAttribute("class","balloon"); balloon_div.setAttribute("onclick","popBalloon(this)"); balloon_div.style.background = balloon_colors[0]; balloons.appendChild(balloon_div); } function createBlueBalloons() { var balloon_div = document.createElement("div"); balloon_div.setAttribute("class","balloon"); balloon_div.setAttribute("onclick","popBalloon(this)"); balloon_div.style.background = balloon_colors[1]; balloons.appendChild(balloon_div); } function createPurpleBalloons() { var balloon_div = document.createElement("div"); balloon_div.setAttribute("class","balloon"); balloon_div.setAttribute("onclick","popBalloon(this)"); balloon_div.style.background = balloon_colors[2]; balloons.appendChild(balloon_div); } function createGreenBalloons() { var balloon_div = document.createElement("div"); balloon_div.setAttribute("class","balloon"); balloon_div.setAttribute("onclick","popBalloon(this)"); balloon_div.style.background = balloon_colors[3]; balloons.appendChild(balloon_div); } // pop balloon function function popBalloon(elem) { elem.style.display = "none"; popped++; number_popped.innerText = popped; popsound.play(); if(timer != 0 && popped==52) { timer = 0; } } // hide remaining balloons function function hideBalloons() { var balloons_remaining = document.querySelectorAll(".balloon"); for(var j = 0; j < balloons_remaining.length; j++) { balloons_remaining[j].style.display = "none"; } }