所以我想做一个新年音乐宾果游戏,我已经做了,所以歌曲得到随机,你可以 Select 你得到的歌曲.但是当你连续四次的时候什么都不会发生.我试着添加一个sumbit按钮,但没有任何react .我的理想的事情将是,如果宾果弹出时, Select ,而不是当提交按下.

// Define the list of songs with artist names
const songs = [
  "Auld Lang Syne - Traditional",
  "Happy New Year - ABBA",
  "New Year's Day - U2",
  "What Are You Doing New Year's Eve? - Ella Fitzgerald",
  "It's Just Another New Year's Eve - Barry Manilow",
  "New Year's Eve - Snoop Dogg ft. Marty James",
  "New Year's Day - Taylor Swift",
  "Bringing in a Brand New Year - B.B. King",
  "New Year's Resolution - Otis Redding & Carla Thomas",
  "This Will Be (An Everlasting Love) - Natalie Cole",
  "New Year's Kiss - Gwen Stefani",
  "Year's End - The Black Eyed Peas",
  "Ring in the New - Imogen Heap",
  "Another Year Has Gone By - Celine Dion",
  "New Year's Eve 1999 - Alabama",
  "New Year's Eve - Tom Waits"
];

let shuffledSongs = [];

// Function to randomize songs and update the Bingo card
function randomizeSongs() {
  const bingoCard = document.getElementById("bingo-card");
  bingoCard.innerHTML = "";
  shuffledSongs = shuffleArray(songs);

  // Create Bingo card cells and add them to the card
  for (let i = 0; i < 16; i++) {
    const cell = document.createElement("div");
    cell.classList.add("cell");
    cell.textContent = shuffledSongs[i];

    // Add a click event listener to mark the cell
    cell.addEventListener("click", function() {
      this.classList.toggle("selected");
      checkBingo();
    });

    bingoCard.appendChild(cell);
  }

  // Reset the Bingo message
  const bingoMessage = document.getElementById("bingo-message");
  bingoMessage.style.display = "none";
}

// Function to check for a bingo
function checkBingo() {
  const rows = document.querySelectorAll(".cell");
  const selectedCells = Array.from(rows).filter(cell => cell.classList.contains("selected"));

  // Check for a horizontal bingo
  for (let i = 0; i < 4; i++) {
    const row = selectedCells.filter(cell => cell.textContent === shuffledSongs[i * 4]);
    if (row.length === 4) {
      showBingoMessage();
      resetGame();
      return;
    }
  }

  // Check for a vertical bingo
  for (let i = 0; i < 4; i++) {
    const column = selectedCells.filter(cell => cell.textContent === shuffledSongs[i]);
    if (column.length === 4) {
      showBingoMessage();
      resetGame();
      return;
    }
  }
}

// Function to show the Bingo message
function showBingoMessage() {
  const bingoMessage = document.getElementById("bingo-message");
  bingoMessage.style.display = "block";
}

// Function to reset the game
function resetGame() {
  const selectedCells = document.querySelectorAll(".selected");
  selectedCells.forEach(cell => cell.classList.remove("selected"));
}

// Function to shuffle an array using Fisher-Yates algorithm
function shuffleArray(array) {
  for (let i = array.length - 1; i > 0; i--) {
    const j = Math.floor(Math.random() * (i + 1));
    [array[i], array[j]] = [array[j], array[i]];
  }
  return array;
}

// Function to create fireworks
function createFireworks() {
  const fireworksContainer = document.getElementById("fireworks-container");

  for (let i = 0; i < 50; i++) {
    const firework = document.createElement("img");
    firework.src = "https://i.gifer.com/VZvx.gif"; // Replace with the URL of your fireworks GIF
    firework.classList.add("firework");
    firework.style.left = `${Math.random() * 100}vw`;
    firework.style.top = `${Math.random() * 100}vh`;
    firework.style.animationDuration = `${Math.random() * 2 + 1}s`;

    fireworksContainer.appendChild(firework);
  }
}

// Initial randomization on page load
randomizeSongs();

// Initial fireworks creation on page load
createFireworks();

// Function to submit bingo
function submitBingo() {
  checkBingo();
}
body {
  background: linear-gradient(to bottom, #0d0d0d, #0d0d0d 50%, #141414 50%, #141414);
  color: #fff;
  font-family: Arial, sans-serif;
  margin: 0;
  padding: 0;
  /* Remove overflow: hidden; to enable scrolling */
  min-height: 100vh;
  /* Set a minimum height to allow content to fill */
}

#fireworks-container {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 0;
  /* Set the z-index to a lower value */
}

.firework {
  position: absolute;
  width: 50px;
  height: 50px;
  animation: explode 2s ease-out infinite;
}

@keyframes explode {
  0% {
    transform: scale(1);
    opacity: 1;
  }
  100% {
    transform: scale(2);
    opacity: 0;
  }
}

#bingo-card {
  position: relative;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
  max-width: 400px;
  margin: 20px auto;
  z-index: 1;
  /* Set the z-index to a higher value */
}

.cell {
  position: relative;
  border: 1px solid #fff;
  padding: 10px;
  text-align: center;
  font-size: 14px;
  cursor: pointer;
  background-color: #1e1e1e;
}

.selected {
  background-color: #a0a0a0;
}

.bingo-text {
  position: absolute;
  bottom: -30px;
  left: 50%;
  transform: translateX(-50%);
  font-size: 18px;
  color: gold;
  display: none;
}

button {
  display: block;
  margin: 20px auto;
  padding: 10px 20px;
  font-size: 16px;
  background-color: gold;
  color: #000;
  border: none;
  cursor: pointer;
  z-index: 1;
  /* Set the z-index to a higher value */
}


/* Responsive styling for smaller screens */

@media screen and (max-width: 480px) {
  button {
    width: 100%;
    /* Make the button full width on smaller screens */
  }
}
<div id="fireworks-container"></div>

<h1>New Year's Eve Bingo</h1>

<div id="bingo-card"></div>
<div class="bingo-text" id="bingo-message">BINGO!</div>

<button onclick="randomizeSongs()">Randomize Songs</button>
<button onclick="submitBingo()">Submit Bingo</button>

推荐答案

您的代码有各种小问题,但最重要的问题是:

  • 宾果游戏板覆盖了#bingo-message
  • 你的宾果判断算法很奇怪

为了达到预期的效果,我对您的代码进行了一些修改.看看你是否明白我做了什么:

#bingo-message {
  color: black;
  background: rgba(255, 255, 255, 0.5);
  width: 100%;
  text-align: center;
  padding: 30px 0;
  z-index: 10;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>New Year's Eve Bingo</title>
  <style>
    body {
      background: linear-gradient(to bottom, #0d0d0d, #0d0d0d 50%, #141414 50%, #141414);
      color: #fff;
      font-family: Arial, sans-serif;
      margin: 0;
      padding: 0;
      /* Remove overflow: hidden; to enable scrolling */
      min-height: 100vh;
      /* Set a minimum height to allow content to fill */
    }
    
    #fireworks-container {
      position: fixed;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      pointer-events: none;
      z-index: 0;
      /* Set the z-index to a lower value */
    }
    
    .firework {
      position: absolute;
      width: 50px;
      height: 50px;
      animation: explode 2s ease-out infinite;
    }
    
    @keyframes explode {
      0% {
        transform: scale(1);
        opacity: 1;
      }
      100% {
        transform: scale(2);
        opacity: 0;
      }
    }
    
    #bingo-card {
      position: relative;
      display: grid;
      grid-template-columns: repeat(4, 1fr);
      gap: 10px;
      max-width: 400px;
      margin: 20px auto;
      z-index: 1;
      /* Set the z-index to a higher value */
    }
    
    .cell {
      position: relative;
      border: 1px solid #fff;
      padding: 10px;
      text-align: center;
      font-size: 14px;
      cursor: pointer;
      background-color: #1e1e1e;
    }
    
    .selected {
      background-color: #a0a0a0;
    }
    
    .bingo-text {
      position: absolute;
      bottom: -30px;
      left: 50%;
      transform: translateX(-50%);
      font-size: 18px;
      color: gold;
      display: none;
    }
    
    button {
      display: block;
      margin: 20px auto;
      padding: 10px 20px;
      font-size: 16px;
      background-color: gold;
      color: #000;
      border: none;
      cursor: pointer;
      z-index: 1;
      /* Set the z-index to a higher value */
    }
    /* Responsive styling for smaller screens */
    
    @media screen and (max-width: 480px) {
      button {
        width: 100%;
        /* Make the button full width on smaller screens */
      }
    }
  </style>

  Copy
  <div id="fireworks-container"></div>

  <h1>New Year's Eve Bingo</h1>

  <div id="bingo-card"></div>
  <h1 class="bingo-text" id="bingo-message">BINGO!</h1>

  <button onclick="randomizeSongs()">Randomize Songs</button>

  <script>
    // Define the list of songs with artist names
    const songs = [
      "Auld Lang Syne - Traditional",
      "Happy New Year - ABBA",
      "New Year's Day - U2",
      "What Are You Doing New Year's Eve? - Ella Fitzgerald",
      "It's Just Another New Year's Eve - Barry Manilow",
      "New Year's Eve - Snoop Dogg ft. Marty James",
      "New Year's Day - Taylor Swift",
      "Bringing in a Brand New Year - B.B. King",
      "New Year's Resolution - Otis Redding & Carla Thomas",
      "This Will Be (An Everlasting Love) - Natalie Cole",
      "New Year's Kiss - Gwen Stefani",
      "Year's End - The Black Eyed Peas",
      "Ring in the New - Imogen Heap",
      "Another Year Has Gone By - Celine Dion",
      "New Year's Eve 1999 - Alabama",
      "New Year's Eve - Tom Waits"
    ];

    let shuffledSongs = [];

    // Function to randomize songs and update the Bingo card
    function randomizeSongs() {
      const bingoCard = document.getElementById("bingo-card");
      bingoCard.innerHTML = "";
      shuffledSongs = shuffleArray(songs);

      // Create Bingo card cells and add them to the card
      for (let i = 0; i < 16; i++) {
        const cell = document.createElement("div");
        cell.classList.add("cell");
        cell.textContent = shuffledSongs[i];

        // Add a click event listener to mark the cell
        cell.addEventListener("click", function() {
          this.classList.toggle("selected");
          checkBingo();
        });

        bingoCard.appendChild(cell);
      }

      // Reset the Bingo message
      const bingoMessage = document.getElementById("bingo-message");
      bingoMessage.style.display = "none";
    }

    // Function to check for a bingo
    function checkBingo() {
      const rows = [...document.querySelectorAll(".cell")];

      // Check for a horizontal bingo
      for (let i = 0; i < 4; i++) {
        if (rows.slice(i * 4, i * 4 + 4).every(element => element.classList.contains("selected"))) {
          showBingoMessage();
          return;
        }
      }

      // Check for a vertical bingo
      for (let i = 0; i < 4; i++) {
        const column = [rows[i], rows[i + 4], rows[i + 8], rows[i + 12]];
        if (column.every(element => element.classList.contains("selected"))) {
          showBingoMessage();
          return;
        }
      }


    }

    // Function to show the Bingo message
    function showBingoMessage() {
      const bingoMessage = document.getElementById("bingo-message");
      bingoMessage.style.display = "block";
    }

    // Function to reset the game
    function resetGame() {
      const selectedCells = document.querySelectorAll(".selected");
      selectedCells.forEach(cell => cell.classList.remove("selected"));
    }

    // Function to shuffle an array using Fisher-Yates algorithm
    function shuffleArray(array) {
      for (let i = array.length - 1; i > 0; i--) {
        const j = Math.floor(Math.random() * (i + 1));
        [array[i], array[j]] = [array[j], array[i]];
      }
      return array;
    }

    // Function to create fireworks
    function createFireworks() {
      const fireworksContainer = document.getElementById("fireworks-container");

      for (let i = 0; i < 50; i++) {
        const firework = document.createElement("img");
        firework.src = "https://i.gifer.com/VZvx.gif"; // Replace with the URL of your fireworks GIF
        firework.classList.add("firework");
        firework.style.left = `${Math.random() * 100}vw`;
        firework.style.top = `${Math.random() * 100}vh`;
        firework.style.animationDuration = `${Math.random() * 2 + 1}s`;

        fireworksContainer.appendChild(firework);
      }
    }

    // Initial randomization on page load
    randomizeSongs();

    // Initial fireworks creation on page load
    createFireworks();

    // Function to submit bingo
  </script>

第一个问题可以通过将h1‘S z-index设置为高于宾果板的值来解决.我们在做的时候加了一些造型,然后就都做好了.

至于宾果判断算法:对于水平宾果判断,我们使用splice来获取一行中的所有元素,然后使用Array.every来判断每个元素是否满足一个条件;在我们的例子中,条件是classList是否包含.selected.对于垂直判断,我的解决方案并不理想(我对4x4网格的索引进行了硬编码),但它已经足够了:).

祝您节日愉快.🎄👨‍👩‍👧‍👦🎇快乐编码!

Javascript相关问答推荐

通过使用100%间隔时间来代表我们还剩多少时间来倒计时

如何添加绘图条形图图例单击角形事件

如何从URL获取令牌?

JS,当你点击卡片下方的绿色空间,而它是在它的背后转动时,

如何使onPaste事件与可拖动的HTML元素一起工作?

如何在ASP.NET JavaScript中使用Google Charts API仅对绘制为负方向的条形图移动堆叠条形图标签位置

使用POST请求时,Req.Body为空

在HTML语言中调用外部JavaScript文件中的函数

确保函数签名中的类型安全:匹配值

向数组中的对象添加键而不改变原始变量

如何在FastAPI中为通过file:/URL加载的本地HTML文件启用CORS?

如何确保预订系统跨不同时区的日期时间处理一致?

Node.js API-queryAll()中的MarkLogic数据移动

我正在试着做一个TicTacToe Ai来和我玩.但是,我试着在第一个方块被点击时出现一个X,然后在第二个方块之后出现一个O

我为什么要使用回调而不是等待?

如何正确地在ComponentWillUnmount中卸载状态以避免内存泄漏?

对不同目录中的Angular material 表列进行排序

ReactJS Sweep Line:优化SciChartJS性能,重用wasmContext进行多图表渲染

JS/css:将数字输入的S函数和可调整大小的元素S函数绑定在一起

使用JAVASCRIPT-使用If和Else If多次判断条件-使用JAVASRIPT对象及其属性