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

              
                <!-- Game screen container -->
<div class="game-screen">
  
  <!-- Number pad section -->
  <div class="number-pad">
    <div class="overlay"></div>
    <!-- Number buttons -->
    <div class="number">1</div>
    <div class="number">2</div>
    <div class="number">3</div>
    <div class="number">4</div>
    <div class="number">5</div>
    <div class="number">6</div>
    <div class="number">7</div>
    <div class="number">8</div>
    <div class="number">9</div>
    <div class="number">0</div>
    <!-- More number buttons... -->
    
    <!-- Join button -->
    <div class="join-btn">JOIN</div>
  </div>
  
  <!-- Game container section -->
  <div class="game-container">
    
    <!-- Result display section -->
    <div class="result">
      <!-- Display "Winner" text -->
      Winner
      <!-- Display the winning number (initially set to "-") -->
      <div class="winner-number">-</div>
    </div>
    
    <!-- Game status section -->
    <div class="game-status">
      
      <!-- Game prize info -->
      <div class="game-prize">
        <!-- Display prize information -->
        You can win
        <!-- Display the prize value (initially set to 0 points) -->
        <div class="game-prize-value">0 points</div>
      </div>
      
      <!-- Game point info -->
      <div class="game-point">
        <!-- Display points spent information -->
        Points spent
        <!-- Display the points spent value (initially set to 0 points) -->
        <div class="game-point-value">0 points</div>
      </div>
      
      <!-- Selected number info -->
      <div class="game-key">
        <!-- Display selected number information -->
        Selected Number
        <!-- Display the selected number value (initially set to "--") -->
        <div class="game-key-value">--</div>
      </div>
      
    </div> <!-- End of game-status -->
    
  </div> <!-- End of game-container -->
  
</div> <!-- End of game-screen -->

              
            
!

CSS

              
                /* Apply transition effects to all elements */
* {
  transition: all 0.15s ease-in-out;
}

/* Set background color for the body */
body {
  background: #000;
}

/* Styling for the game screen container */
.game-screen {
  border: 2px solid black;
  display: grid;
  grid-template-columns: 30% 70%;
  background: linear-gradient(#36f, blue); /* Gradient background */
  color: #fff; /* Text color */
}

/* Styling for the number pad section */
.number-pad {
  background: #0d0; /* Green background */
}

/* Styling for the result display */
.result {
  height: 200px; /* Set height */
  border: 2px solid black; /* Border */
}

/* Styling for the game status section */
.game-status {
  display: flex;
  justify-content: center; /* Center align items */
}

/* Styling for each section within the game status */
.game-status > div {
  border: 2px solid black; /* Border */
  flex-grow: 1; /* Allow items to grow */
  padding: 20px; /* Padding */
  text-align: center; /* Center align text */
  font-size: 20px; /* Font size */
  font-weight: bold; /* Bold text */
  font-family: arial; /* Font family */
}

/* Styling for the number pad section */
.number-pad {
  display: grid;
  grid-template-columns: repeat(3, 1fr); /* 3 columns with equal width */
  gap: 10px; /* Gap between grid items */
  padding: 10px; /* Padding */
  position: relative; /* Relative positioning */
  overflow: hidden; /* Hide overflow */
}

/* Styling for each number button */
.number-pad > div {
  display: grid;
  place-content: center; /* Center align content */
  border-radius: 20px; /* Rounded corners */
  box-shadow: 0px 5px 10px rgba(0, 0, 0, 0.3), inset 0px 5px 10px rgba(255, 255, 255, 0.6); /* Box shadow */
  user-select: none; /* Disable selection */
  font-size: 20px; /* Font size */
  font-family: arial; /* Font family */
  font-weight: bold; /* Bold text */
}

/* Styling for active state of number buttons */
.number-pad > div:active {
  box-shadow: 0px 5px 10px rgba(0, 0, 0, 0), inset 0px 5px 10px rgba(255, 255, 255, 0), inset 0px 5px 10px rgba(0, 0, 0, 0.3), 0px 5px 10px rgba(255, 255, 255, 0.6); /* Box shadow */
}

/* Styling for the JOIN button */
.join-btn {
  grid-column: 2/4; /* Span columns */
  background: black; /* Black background */
  z-index: 1; /* Set z-index */
}

/* Styling for the overlay in the number pad */
.number-pad .overlay {
  position: absolute; /* Absolute positioning */
  width: 100%; /* Full width */
  height: 100%; /* Full height */
  background: red; /* Red background */
  border-radius: 0px; /* No border radius */
  opacity: 0; /* Opacity */
  box-shadow: none; /* No box shadow */
  z-index: 0; /* Set z-index */
}

/* Styling for the result text */
.result {
  font-size: 50px; /* Font size */
  font-family: arial; /* Font family */
  font-weight: 900; /* Bold text */
  text-align: center; /* Center align text */
}

/* Styling for the winning number */
.winner-number {
  font-size: 100px; /* Font size */
}

              
            
!

JS

              
                // Initialize variables for game state
let isGameStarted = false; // Flag to track if the game is started
let gamepoint = 0; // Points spent by the player
let gamePrize = 0; // Prize the player can win
let gamePrizePercentage = 300; // Percentage of points to be added as prize
let gameKey = ""; // Selected number by the player
let winnerNumber = -1; // Winning number generated randomly

// Select DOM elements
let numberpadOverlay = document.querySelector(".number-pad .overlay");
let numbers = document.querySelectorAll(".number");
let joinBtn = document.querySelector(".join-btn");
let gamepointValue = document.querySelector(".game-point-value");
let gamePrizeValue = document.querySelector(".game-prize-value");
let gameKeyValue = document.querySelector(".game-key-value");
let winnerNumberElement = document.querySelector(".winner-number");

// Event listener for the "JOIN" button to start the game
joinBtn.addEventListener("click", startGame);

// Event listener for each number button
for (let i = 0; i < numbers.length; i++) {
  numbers[i].addEventListener("click", () => {
    // Set the selected number
    gameKey = numbers[i].innerText;
    gameKeyValue.innerText = gameKey;
    
    // Enable pointer events on the overlay
    numberpadOverlay.style.pointerEvents = "all";
    
    // Wait for a delay to simulate the game process
    setTimeout(function() {
      // Display the winning number
      winnerNumberElement.innerText = winnerNumber;
      
      // Check if the selected number matches the winning number
      if (parseInt(winnerNumber) == parseInt(gameKey)) {
        // If matched, player wins
        setTimeout(function() {
          alert("Winner!");
          isGameStarted = false;
          confirmToRestart();
        }, 1000);
      } else {
        // If not matched, player loses
        setTimeout(function() {
          alert("You lose!");
          isGameStarted = false;
          confirmToRestart();
        }, 1000);
      }
    }, 5000); // Wait for 5 seconds
  });
}

// Function to confirm if the player wants to restart the game
function confirmToRestart() {
  let shouldRestartGame = confirm("Do you want to restart the game?");
  
  if (shouldRestartGame) {
    // If player wants to restart, start a new game
    startGame();
  } else {
    // If not, hide the "JOIN" button
    joinBtn.style.zIndex = 0;
  }
}

// Function to start the game
function startGame() {
  // Initialize game state variables
  isGameStarted = true;
  gamepoint = 0;
  gamePrize = 0;
  gamePrizePercentage = 500;
  
  // Generate a random winning number
  winnerNumber = parseInt(Math.random() * 10);
  console.log("w:" + winnerNumber);
  
  // Prompt the player to enter points they want to spend
  gamepoint = parseInt(prompt("Enter how points you want to spend?"));
  // Calculate the prize based on the points spent
  gamePrize = parseInt(gamepoint * gamePrizePercentage / 100) + gamepoint;
  
  // Update the game status display with points and prize information
  gamepointValue.innerText = gamepoint + " points";
  gamePrizeValue.innerText = gamePrize + " points";
  
  // Wait for a delay to start the game after displaying information
  setTimeout(function() {
    alert("Now Select a number from left side.");
    joinBtn.style.zIndex = -1; // Hide the "JOIN" button
    numberpadOverlay.style.pointerEvents = "none"; // Disable pointer events on the overlay
  }, 1000);
}

              
            
!
999px

Console