Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!-- Play Game button -->
<button class="play-game-btn">Play Game</button>

<!-- Main game container -->
<div class="game-container">
  <!-- Game status display -->
  <div class="status">Rest</div>
  
  <!-- Helicopter container -->
  <div class="helicopter">
    <!-- Helicopter image -->
    <img src="https://images.vexels.com/content/153505/preview/red-helicopter-icon-1fd9bb.png" alt="Helicopter">
    <!-- Blast animation image (hidden by default) -->
    <img class="blast-animation" src="https://i.gifer.com/origin/d4/d43612896ed91c111a46c2965c9e7f25_w200.gif" alt="Explosion">
  </div>
</div>
              
            
!

CSS

              
                /* Game container styles */
.game-container {
  background: black;
  height: 400px;
  position: relative;
  overflow: hidden;
}

/* Helicopter styles */
.helicopter {
  border: 5px solid black;
  width: 100px;
  height: 30px;
  position: absolute;
  bottom: 0px;
  left: 10px;
  transition: all 1s;
}

/* Helicopter image styles */
.helicopter img {
  width: 100%;
  margin-top: -35px;
}

/* Game status text styles */
.status {
  color: #fff;
  font-size: 30px;
  font-weight: bold;
  font-family: arial;
  text-align: center;
  padding: 10px;
}

/* Blast animation styles */
.blast-animation {
  position: absolute;
  top: 0px;
  left: 0px;
  opacity: 0;
}
              
            
!

JS

              
                // Select DOM elements
let helicopter = document.querySelector(".helicopter");
let gameStatus = document.querySelector(".status");
let blastAnimation = document.querySelector(".blast-animation");
let playGameBtn = document.querySelector(".play-game-btn");

// Add click event listener to the Play Game button
playGameBtn.addEventListener("click", () => {
  playGameBtn.style.display = "none";
  playGame();
});

// Main game function
function playGame() {
  let count = 0;
  let helicopterLeft = 10;
  let helicopterBottom = 0;
  let maxCount = parseInt(Math.random() * 31);

  // Initialize helicopter position
  helicopter.style.opacity = "1";
  helicopter.style.bottom = helicopterBottom + "px";
  helicopter.style.left = helicopterLeft + "px";

  console.log("Current position of the helicopter is ");
  console.log(`left: ${helicopterLeft} bottom: ${helicopterBottom}`);

  // Update game status and helicopter position
  gameStatus.innerText = "Flying";
  helicopter.style.bottom = "20px";
  helicopter.style.transform = "rotate(-10deg)";
  helicopter.style.transition = "all 0.5s linear";

  // Start game loop
  let gameTimer = setInterval(() => {
    count++;
    helicopterLeft = helicopterLeft + 20;
    helicopterBottom = helicopterBottom + parseInt(Math.random() * 20);
    helicopter.style.left = helicopterLeft + "px";
    helicopter.style.bottom = helicopterBottom + "px";

    // Check if game should end
    if (count >= maxCount) {
      // Show explosion animation
      helicopter.style.opacity = "0";
      blastAnimation.style.opacity = "1";
      gameStatus.innerText = "Game Over";

      // Reset game after a delay
      setTimeout(() => {
        blastAnimation.style.opacity = "0";
        clearInterval(gameTimer);

        setTimeout(() => {
          // Reset game state for next round
          playGameBtn.style.display = "inline-block";
          helicopterLeft = 10;
          helicopterBottom = 0;
          helicopter.style.bottom = helicopterBottom + "px";
          helicopter.style.left = helicopterLeft + "px";
        }, 200);
      }, 200);
    }
  }, 500);
}
              
            
!
999px

Console