Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's 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 it's URL and the proper URL extention.

+ 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

Save Automatically?

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

              
                <div class="wrapper">
  <div id="main_screen">
    <h1 id="game-title" class="h1Title">DETENTION</h1>
    <div id="menu" class="start_menu_list">
      <button onclick="startGame()" class="btn_menu">START</button>

    </div>
  </div>
  <div id="game_screen">
    <div class="wordScr">
      <p id="word"></p>
    </div>
    <div class="gameplayWindow">

    </div>
    <div id="status_bar" class="statusbar">
      <p id="lives">LIVES : 3</p>
      <p id="score">SCORE : 0000</p>
    </div>

  </div>
  <div id="winScr">

    <h1 id="finalScore"></h1>
    <h1 id="bestScore">
      <h1>

        <button onclick="startGame()" class="btn_menu">&#8634;</button>

  </div>

</div>

<script defer src="gameplay.js"></script>
              
            
!

CSS

              
                
@import url("https://fonts.googleapis.com/css2?family=Fredericka+the+Great&display=swap");
html {
  box-sizing: border-box;
  font-size: 62.5%; /* 10px = 1 rem*/
  user-select:none;
}
.wrapper {
  position: relative;
  background-image: url("https://i.postimg.cc/SNBYz5sB/iiiii.png");
  background-repeat: no-repeat;
  background-position: center;
  background-size: cover;
  width: 450px;
  height: 600px;
  margin: 10rem auto;
  border: 2px solid black;

  text-align: center;
}
.wrapper h1.h1Title {
  display: block;
  font-family: "Fredericka the Great", cursive;
  color: white;
  letter-spacing: 5px;
  font-size: 4.8rem;
}
.start_menu_list {
  text-align: center;
  margin-top: 10rem;
}

.btn_menu {
  display: block;
  width: 240px;
  height: 80px;
  color:white;
  font-size: 5rem;
  margin: 0 auto 0 auto;
  border:none;
  background: none;
   font-family: "Fredericka the Great", cursive;
}
.btn_menu:not(:first-of-type) {
  margin-top: 1rem;
}
.btn_menu:hover {
  cursor: pointer;
}
/*Game status bar*/
.statusbar,
.wordScr {
  display: block;
  padding: 0;
  width: 100%;
  height: 40px;
}
.statusbar {
  width: 100%;
  text-align: left;
  background-image: url("https://i.postimg.cc/gj1Gp5kH/ssss.png");
  background-repeat: no-repeat;
  background-position: center;
  background-size: cover;
  padding: 0;
  margin: 0;
}
.wordScr {
  text-align: center;
}
.statusbar p {
  display: inline-block;
  margin: 1rem;
  font-size: 2rem;
  font-weight:1.5;
  color: black;
}
/*GAMEPLAY SCREEN*/

.gameplayWindow {
  width: 450px;
  height: 520px;
  overflow: hidden;
}
.wordScr p {
  display: block;
  margin: auto;
  padding-top: 1.5rem;
  font-size: 2rem;
  font-family: "Fredericka the Great", cursive;
  letter-spacing: 0.5rem;
  color: white;
}

#main_screen {
  display: block;
}
#game_screen,
#winScr {
  display: none;
}
#winScr {
  padding-top: 2rem;
  color:white;
}

.letters {
  display: block;
  position: absolute;
  font-size: 7rem;
  font-family: "Fredericka the Great", cursive;
  color: white;
  margin: 2rem 0 6rem 0;
}
.letters:hover {
  cursor: pointer;
}
.correct {
  transform: scale(1.2);
}
.incorrect {
  transform: scale(1.9);
  transition: transform 500ms ease-in-out;
  color: red;
}

.fall {
  animation-delay: 1s;
  animation: falling 2s ease forwards;
  animation-timing-function: linear;
  animation-iteration-count: 1;
}
@keyframes falling {
  100% {
    transform: translateY(400px);
  }
}
.correctletter {
  color: red;
  text-transform: uppercase;
}
.livesicons {
  display: inline;
  list-style: none;
  margin: 0;
  padding: 0;
}
.livesicons li {
  margin: 0 0.5rem 0 0;
  float: left;
}

              
            
!

JS

              
                var lives = 1;
var score = 0;
var bestScore = 0;
var difficulty = 1;
var counter = 0;
var mainWord;
var gameState = true;
function checkScore() {
  if (score > bestScore) {
    bestScore = score;
  }
}
function nextWord() {
  removeAllLetters();
  gameState = true;
  difficulty = 1;
  lives++;
  counter = 0;
  printLives();
  let word = GetWord();
  mainWord = word;
  PrintWord();
  SpawnLetters(word, 5);
}
function checkIfCorrect(num1) {
  if (num1 == counter) {
    gameState = false;

    setTimeout(nextWord, 2000);
  }
}

function countLetters(word, letter) {
  let count = 0;
  for (let i = 0; i <= word.length; i++) {
    if (word[i] === letter && word[i].length === 1) {
      count++;
    }
  }
  if (word.length - counter < 4) {
    difficulty += 0.5;
  }
  if (word.length - counter < 2) {
    difficulty += 1;
  }
  return count;
}
function changeMainWord(word, letter) {
  let temp =
    '<span class = "correctletter">' + letter.toLowerCase() + "</span>";
  counter = counter + countLetters(word, letter);
  mainWord = mainWord.replace(new RegExp(letter, "g"), temp);
  checkIfCorrect(word.length);

  PrintWord();
}
function getRandomNumber(min, max) {
  let newNum = Math.floor(Math.random() * (max - min + 1)) + min;
  return newNum;
}
function removeAllLetters() {
  let element = document.getElementsByClassName("gameplayWindow")[0];
  while (element.firstChild) {
    element.firstChild.remove();
  }
}
function checkLives(lives) {
  if (lives <= 0) {
    gameState = false;
    removeAllLetters();
    checkScore();
    let finscr = document.getElementById("finalScore");
    let bestscr = document.getElementById("bestScore");
    let winScr = document.getElementById("winScr");
    let gameScr = document.getElementById("game_screen");
    gameScr.style.display = "none";
    winScr.style.display = "block";
    finscr.textContent = "Your score : " + score;
    bestscr.textContent = "Best score : " + bestScore;
  }
}
function printLives() {
  let x = document.getElementById("lives");
  x.innerHTML = "LIVES : " + lives;
}
function printScore() {
  let x = document.getElementById("score");
  x.innerHTML = "SCORE : " + score;
}
function GetWord() {
  let words = [
    "ELEPHANT",
    "CAR",
    "HOUSE",
    "HORSE",
    "APPLE",
    "COMPUTER",
    "PARROT",
    "DOG",
    "FRANCE",
    "AMERICA",
    "EXAMPLE"
  ];

  return words[Math.floor(Math.random() * words.length)];
} /*Printing word in the screen*/
function PrintWord() {
  let word = document.getElementById("word");
  word.innerHTML = mainWord;
}
/*Choosing where letter will spawn*/
function GetLocation() {
  let x = ["50", "200", "350"];
  return x[Math.floor(Math.random() * x.length)];
}
function removeLetter(letter, word) {
  letter.remove();

  SpawnLetters(word, 1);
}
function hideLetter(letter) {
  letter.style.visibility = "hidden";
}
function letterExist(word, letter) {
  for (let i = 0; i <= word.length; i++) {
    if (word[i] === letter) {
      return true;
    }
  }

  /*if (word.indexOf(letter) > -1) {
    return true;
  } else {
    return false;
  }*/
}
/*Spawns letter on the screen*/
function SpawnLetter(letter, word) {
  score += getRandomNumber(5, 10);
  printScore();
  let para = document.createElement("p");
  let node = document.createTextNode(letter);
  para.appendChild(node);
  para.classList.add("letters", "fall");
  para.onclick = function checkIfCorrect() {
    if (letterExist(mainWord, this.textContent)) {
      this.classList.add("correct");
      let ranNum = getRandomNumber(30, 50);
      score += ranNum;
      printScore();
      changeMainWord(word, this.textContent);
      this.textContent = "+" + ranNum;

      setTimeout(hideLetter, 500, this, word);
    } else {
      lives--;
      checkLives(lives);
      printLives();
      setTimeout(hideLetter, 300, this, word);
      this.classList.add("incorrect");
    }
  };
  para.style.left = GetLocation() + "px";
  para.addEventListener("webkitAnimationEnd", function () {
    if (letterExist(mainWord, this.textContent)) {
      lives--;
      checkLives(lives);
      printLives();
    }
    removeLetter(this, word);
  });
  let element = document.getElementsByClassName("gameplayWindow")[0];
  element.appendChild(para);
}
/*Loop which will continue till player
win or lose*/
function SpawnLetters(word, holder) {
  let letters = [
    "A",
    "B",
    "C",
    "D",
    "E",
    "F",
    "G",
    "H",
    "I",
    "J",
    "K",
    "L",
    "M",
    "N",
    "O",
    "P",
    "Q",
    "R",
    "S",
    "T",
    "U",
    "V",
    "W",
    "X",
    "Y",
    "Z"
  ];

  for (let i = 1; i <= holder; i++) {
    if (gameState) {
      setTimeout(
        SpawnLetter,
        i * (500 / difficulty),
        letters[Math.floor(Math.random() * letters.length)],
        word
      );
    } else {
      return 0;
    }
  }
}
function startGame() {
  removeAllLetters();
  let mainScr = document.getElementById("main_screen");
  let winScr = document.getElementById("winScr");
  let gameScr = document.getElementById("game_screen");
  winScr.style.display = "none";
  mainScr.style.display = "none";
  gameScr.style.display = "block";
  gameState = true;
  lives = 3;
  score = 0;
  counter = 0;
  difficulty = 1;
  printLives();
  let word = GetWord();
  mainWord = word;
  PrintWord();
  SpawnLetters(word, 5);
}

              
            
!
999px

Console