国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

首頁(yè) web前端 js教程 使用 html css 和 js 的益智遊戲

使用 html css 和 js 的益智遊戲

Oct 17, 2024 pm 04:55 PM

Puzzle game using html css and js

https://www.instagram.com/webstreet_code/

HTML CODE:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Jashan's Puzzle Game</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <!-- div.puzzle-piece#piece-$*9>img[src="./j${1}.png" 
    alt="Piece ${1}"] -->
    <div class="puzzle-wrapper">
        <div class="puzzle-container">
            <div class="puzzle-piece" id="piece-1">
            <img src="./j1.png" alt="Piece 1"></div>
            <div class="puzzle-piece" id="piece-2">
            <img src="./j2.png" alt="Piece 2"></div>
            <div class="puzzle-piece" id="piece-3">
            <img src="./j3.png" alt="Piece 3"></div>
            <div class="puzzle-piece" id="piece-4">
            <img src="./j4.png" alt="Piece 4"></div>
            <div class="puzzle-piece" id="piece-5">
            <img src="./j5.png" alt="Piece 5"></div>
            <div class="puzzle-piece" id="piece-6">
            <img src="./j6.png" alt="Piece 6"></div>
            <div class="puzzle-piece" id="piece-7">
            <img src="./j7.png" alt="Piece 7"></div>
            <div class="puzzle-piece" id="piece-8">
            <img src="./j8.png" alt="Piece 8"></div>
            <div class="puzzle-piece" id="piece-9">
            <img src="./j9.png" alt="Piece 9"></div>
        </div>
        <button id="shuffle-btn">Shuffle</button>
    </div>
    <script src="script.js"></script>
</body>
</html>


CSS CODE:

body {
  font-family: "Poppins", sans-serif;
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #121212;
  color: #ffffff;
  margin: 0;
}

.puzzle-wrapper {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.puzzle-container {
  display: grid;
  grid-template-columns: repeat(3, 100px);
  grid-template-rows: repeat(3, 100px);
  gap: 4px;
  margin-bottom: 20px;
  box-shadow: 0 20px 20px rgba(184, 26, 150, 0.879);
  border-radius: 15px;
  padding: 10px;
  background: #282828;
}

.puzzle-piece {
  position: relative;
  width: 100px;
  height: 100px;
  /* overflow: hidden; */
  border-radius: 10px;
  box-shadow: 0 4px 6px rgba(226, 221, 221, 0.1);
  transition: transform 0.2s, box-shadow 0.2s;
  cursor: pointer;
}

.puzzle-piece img {
  border: 1px solid rgb(86, 10, 84);
  border-radius: 10px;
  width: 100%;
  height: 100%;
  filter: brightness(1) contrast(1);
  /* object-fit: cover; */
}

.puzzle-piece.selected {
  border: 3px solid white;
  transform: scale(1.05);
}

.puzzle-piece:hover {
  box-shadow: 0 6px 12px rgba(255, 255, 255, 0.2);
}

#shuffle-btn {
  padding: 12px 25px;
  background-color: #e71d96;
  color: white;
  border: none;
  border-radius: 5px;
  font-size: 18px;
  cursor: pointer;
  transition: background-color 0.3s, box-shadow 0.3s;
}

#shuffle-btn:hover {
  background-color: #8c0a83;
  box-shadow: 0 4px 8px rgba(0, 255, 204, 0.3);
}


JS CODE

let firstSelectedPiece = null;
let secondSelectedPiece = null;

// Initialize puzzle pieces array
const pieces = Array.from(document.querySelectorAll('.puzzle-piece'));
let shuffled = false;

// Shuffle function
// Shuffle function
function shufflePuzzle() {
    // Create a new array to hold shuffled pieces
    const shuffledPieces = pieces.slice(); // Copy the original pieces array

    // Shuffle the array using Fisher-Yates shuffle algorithm
    for (let i = shuffledPieces.length - 1; i > 0; i--) {
        const j = Math.floor(Math.random() * (i + 1));
        [shuffledPieces[i], shuffledPieces[j]] = [shuffledPieces[j], shuffledPieces[i]]; // Swap elements
    }

    // Re-insert shuffled pieces into the puzzle container
    const puzzleContainer = document.querySelector('.puzzle-container');
    shuffledPieces.forEach(piece => {
        puzzleContainer.appendChild(piece); // Append shuffled pieces to the container
    });

    shuffled = true; // Mark as shuffled
}


// Swap two selected pieces
function swapPieces(piece1, piece2) {
    setTimeout(() => {
        // Swap the images instead of text content
        const img1 = piece1.querySelector('img').src;
        const img2 = piece2.querySelector('img').src;

        piece1.querySelector('img').src = img2; // Set piece1's img to piece2's img
        piece2.querySelector('img').src = img1; // Set piece2's img to piece1's img

        piece1.classList.remove('selected');
        piece2.classList.remove('selected');

        firstSelectedPiece = null;
        secondSelectedPiece = null;

        checkCompletion();
    }, 300);  // Delay of 300ms for a smoother swap animation
}

// Check if puzzle is completed
function checkCompletion() {
    // Get the current order of images by their src attributes
    const currentOrder = Array.from(document.querySelectorAll('img')).map(img => img.src);


    console.log("currentorderis:", currentOrder)

    // Define the correct order of the images
    const correctOrder = [
        'http://127.0.0.1:5500/htmlcss/j1.png',
        'http://127.0.0.1:5500/htmlcss/j2.png',
        'http://127.0.0.1:5500/htmlcss/j3.png',
        'http://127.0.0.1:5500/htmlcss/j4.png',
        'http://127.0.0.1:5500/htmlcss/j5.png',
        'http://127.0.0.1:5500/htmlcss/j6.png',
        'http://127.0.0.1:5500/htmlcss/j7.png',
        'http://127.0.0.1:5500/htmlcss/j8.png',
        'http://127.0.0.1:5500/htmlcss/j9.png'
    ];

    // Compare the current order with the correct order
    if (JSON.stringify(currentOrder) === JSON.stringify(correctOrder)) {
        setTimeout(() => {
            alert("Congratulations! You completed the puzzle!");
        }, 1000);
    }
}

// Add click event listeners for puzzle pieces
pieces.forEach(piece => {
    piece.addEventListener('click', () => {
        if (!firstSelectedPiece) {
            firstSelectedPiece = piece;
            piece.classList.add('selected');
        } else if (!secondSelectedPiece && piece !== firstSelectedPiece) {
            secondSelectedPiece = piece;
            piece.classList.add('selected');
            swapPieces(firstSelectedPiece, secondSelectedPiece);
        }
    });
});

// Deselect pieces when clicking outside the puzzle
document.addEventListener('click', (event) => {
    if (!event.target.classList.contains('puzzle-piece') && !event.target.closest('.puzzle-piece')) {
        if (firstSelectedPiece) {
            firstSelectedPiece.classList.remove('selected');
            firstSelectedPiece = null;
        }
        if (secondSelectedPiece) {
            secondSelectedPiece.classList.remove('selected');
            secondSelectedPiece = null;
        }
    }
});

// Shuffle the puzzle at the beginning
shufflePuzzle();

// Shuffle button
document.getElementById('shuffle-btn').addEventListener('click', shufflePuzzle);

以上是使用 html css 和 js 的益智遊戲的詳細(xì)內(nèi)容。更多資訊請(qǐng)關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

本網(wǎng)站聲明
本文內(nèi)容由網(wǎng)友自願(yuàn)投稿,版權(quán)歸原作者所有。本站不承擔(dān)相應(yīng)的法律責(zé)任。如發(fā)現(xiàn)涉嫌抄襲或侵權(quán)的內(nèi)容,請(qǐng)聯(lián)絡(luò)admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費(fèi)脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅(qū)動(dòng)的應(yīng)用程序,用於創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費(fèi)的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費(fèi)的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強(qiáng)大的PHP整合開(kāi)發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺(jué)化網(wǎng)頁(yè)開(kāi)發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級(jí)程式碼編輯軟體(SublimeText3)

熱門(mén)話題

Java vs. JavaScript:清除混亂 Java vs. JavaScript:清除混亂 Jun 20, 2025 am 12:27 AM

Java和JavaScript是不同的編程語(yǔ)言,各自適用於不同的應(yīng)用場(chǎng)景。 Java用於大型企業(yè)和移動(dòng)應(yīng)用開(kāi)發(fā),而JavaScript主要用於網(wǎng)頁(yè)開(kāi)發(fā)。

如何在JS中與日期和時(shí)間合作? 如何在JS中與日期和時(shí)間合作? Jul 01, 2025 am 01:27 AM

JavaScript中的日期和時(shí)間處理需注意以下幾點(diǎn):1.創(chuàng)建Date對(duì)像有多種方式,推薦使用ISO格式字符串以保證兼容性;2.獲取和設(shè)置時(shí)間信息可用get和set方法,注意月份從0開(kāi)始;3.手動(dòng)格式化日期需拼接字符串,也可使用第三方庫(kù);4.處理時(shí)區(qū)問(wèn)題建議使用支持時(shí)區(qū)的庫(kù),如Luxon。掌握這些要點(diǎn)能有效避免常見(jiàn)錯(cuò)誤。

為什麼要將標(biāo)籤放在的底部? 為什麼要將標(biāo)籤放在的底部? Jul 02, 2025 am 01:22 AM

PlacingtagsatthebottomofablogpostorwebpageservespracticalpurposesforSEO,userexperience,anddesign.1.IthelpswithSEObyallowingsearchenginestoaccesskeyword-relevanttagswithoutclutteringthemaincontent.2.Itimprovesuserexperiencebykeepingthefocusonthearticl

JavaScript與Java:開(kāi)發(fā)人員的全面比較 JavaScript與Java:開(kāi)發(fā)人員的全面比較 Jun 20, 2025 am 12:21 AM

JavaScriptIspreferredforredforwebdevelverment,而Javaisbetterforlarge-ScalebackendsystystemsandSandAndRoidApps.1)JavascriptexcelcelsincreatingInteractiveWebexperienceswebexperienceswithitswithitsdynamicnnamicnnamicnnamicnnamicnemicnemicnemicnemicnemicnemicnemicnemicnddommanipulation.2)

什麼是在DOM中冒泡和捕獲的事件? 什麼是在DOM中冒泡和捕獲的事件? Jul 02, 2025 am 01:19 AM

事件捕獲和冒泡是DOM中事件傳播的兩個(gè)階段,捕獲是從頂層向下到目標(biāo)元素,冒泡是從目標(biāo)元素向上傳播到頂層。 1.事件捕獲通過(guò)addEventListener的useCapture參數(shù)設(shè)為true實(shí)現(xiàn);2.事件冒泡是默認(rèn)行為,useCapture設(shè)為false或省略;3.可使用event.stopPropagation()阻止事件傳播;4.冒泡支持事件委託,提高動(dòng)態(tài)內(nèi)容處理效率;5.捕獲可用於提前攔截事件,如日誌記錄或錯(cuò)誤處理。了解這兩個(gè)階段有助於精確控制JavaScript響應(yīng)用戶操作的時(shí)機(jī)和方式。

JavaScript:探索用於高效編碼的數(shù)據(jù)類型 JavaScript:探索用於高效編碼的數(shù)據(jù)類型 Jun 20, 2025 am 12:46 AM

javascripthassevenfundaMentalDatatypes:數(shù)字,弦,布爾值,未定義,null,object和symbol.1)numberSeadUble-eaduble-ecisionFormat,forwidevaluerangesbutbecautious.2)

如何減少JavaScript應(yīng)用程序的有效載荷大??? 如何減少JavaScript應(yīng)用程序的有效載荷大小? Jun 26, 2025 am 12:54 AM

如果JavaScript應(yīng)用加載慢、性能差,問(wèn)題往往出在payload太大,解決方法包括:1.使用代碼拆分(CodeSplitting),通過(guò)React.lazy()或構(gòu)建工具將大bundle拆分為多個(gè)小文件,按需加載以減少首次下載量;2.移除未使用的代碼(TreeShaking),利用ES6模塊機(jī)制清除“死代碼”,確保引入的庫(kù)支持該特性;3.壓縮和合併資源文件,啟用Gzip/Brotli和Terser壓縮JS,合理合併文件並優(yōu)化靜態(tài)資源;4.替換重型依賴,選用輕量級(jí)庫(kù)如day.js、fetch

JavaScript模塊上的確定JS綜述:ES模塊與COMPORJS JavaScript模塊上的確定JS綜述:ES模塊與COMPORJS Jul 02, 2025 am 01:28 AM

ES模塊和CommonJS的主要區(qū)別在於加載方式和使用場(chǎng)景。 1.CommonJS是同步加載,適用於Node.js服務(wù)器端環(huán)境;2.ES模塊是異步加載,適用於瀏覽器等網(wǎng)絡(luò)環(huán)境;3.語(yǔ)法上,ES模塊使用import/export,且必須位於頂層作用域,而CommonJS使用require/module.exports,可在運(yùn)行時(shí)動(dòng)態(tài)調(diào)用;4.CommonJS廣泛用於舊版Node.js及依賴它的庫(kù)如Express,ES模塊則適用於現(xiàn)代前端框架和Node.jsv14 ;5.雖然可混合使用,但容易引發(fā)問(wèn)題

See all articles