使用 html css 和 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 的益智遊戲的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Python更適合初學者,學習曲線平緩,語法簡潔;JavaScript適合前端開發,學習曲線較陡,語法靈活。 1.Python語法直觀,適用於數據科學和後端開發。 2.JavaScript靈活,廣泛用於前端和服務器端編程。

從C/C 轉向JavaScript需要適應動態類型、垃圾回收和異步編程等特點。 1)C/C 是靜態類型語言,需手動管理內存,而JavaScript是動態類型,垃圾回收自動處理。 2)C/C 需編譯成機器碼,JavaScript則為解釋型語言。 3)JavaScript引入閉包、原型鍊和Promise等概念,增強了靈活性和異步編程能力。

JavaScript在Web開發中的主要用途包括客戶端交互、表單驗證和異步通信。 1)通過DOM操作實現動態內容更新和用戶交互;2)在用戶提交數據前進行客戶端驗證,提高用戶體驗;3)通過AJAX技術實現與服務器的無刷新通信。

JavaScript在現實世界中的應用包括前端和後端開發。 1)通過構建TODO列表應用展示前端應用,涉及DOM操作和事件處理。 2)通過Node.js和Express構建RESTfulAPI展示後端應用。

理解JavaScript引擎內部工作原理對開發者重要,因為它能幫助編寫更高效的代碼並理解性能瓶頸和優化策略。 1)引擎的工作流程包括解析、編譯和執行三個階段;2)執行過程中,引擎會進行動態優化,如內聯緩存和隱藏類;3)最佳實踐包括避免全局變量、優化循環、使用const和let,以及避免過度使用閉包。

Python和JavaScript在社區、庫和資源方面的對比各有優劣。 1)Python社區友好,適合初學者,但前端開發資源不如JavaScript豐富。 2)Python在數據科學和機器學習庫方面強大,JavaScript則在前端開發庫和框架上更勝一籌。 3)兩者的學習資源都豐富,但Python適合從官方文檔開始,JavaScript則以MDNWebDocs為佳。選擇應基於項目需求和個人興趣。

Python和JavaScript在開發環境上的選擇都很重要。 1)Python的開發環境包括PyCharm、JupyterNotebook和Anaconda,適合數據科學和快速原型開發。 2)JavaScript的開發環境包括Node.js、VSCode和Webpack,適用於前端和後端開發。根據項目需求選擇合適的工具可以提高開發效率和項目成功率。

C和C 在JavaScript引擎中扮演了至关重要的角色,主要用于实现解释器和JIT编译器。1)C 用于解析JavaScript源码并生成抽象语法树。2)C 负责生成和执行字节码。3)C 实现JIT编译器,在运行时优化和编译热点代码,显著提高JavaScript的执行效率。
