Calculator Tools Calculator Tools
Create

Jigsaw Puzzle Game

Sep 28, 2023

About this app

A fun and colorful jigsaw puzzle game.

Jigsaw Puzzle Game

Related apps

Put this on your site

Source

                    <html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1">

<title>Jigsaw Puzzle Game</title>
<meta name="description" content="A fun and colorful jigsaw puzzle game.">
<meta name="keywords" content="game, jigsaw, puzzle, fun, colorful">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">

<script type="text/javascript">
try {
// Randomize array function
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]]; // swap elements
}
}

// This will run when the DOM is ready.
document.addEventListener("DOMContentLoaded", function() {
let puzzlePieces = Array.from(document.querySelectorAll('.puzzle-piece'));
shuffleArray(puzzlePieces);
const puzzleArea = document.getElementById('puzzle-area');
puzzlePieces.forEach(piece => puzzleArea.appendChild(piece));

$('.puzzle-piece').draggable({
start: function(event, ui) {
$(this).addClass('moving');
},
stop: function(event, ui) {
$(this).removeClass('moving');
}
});

$('.puzzle-piece').droppable({
accept: '.puzzle-piece',
drop: function(event, ui) {
const moving = document.querySelector('.moving');
this.after(moving);
}
});
});

} catch (error) {
// This will throw the error to the parent window.
throw error;
}
</script>

<style>
/* App CSS Goes Here */
#puzzle-area {
display: flex;
flex-wrap: wrap;
width: 400px;
height: 400px;
margin: 0 auto;
border: 10px solid #fdc82f;
background: #feefc3;
}

.puzzle-piece {
width: 100px;
height: 100px;
border: 2px solid #fdc82f;
background-size: cover;
}

.puzzle-piece.moving {
opacity: 0.5;
transform: scale(1.1);
}
</style>
</head>
<body>
<div id="main-container" class="container">
<!-- App HTML Goes Here -->
<div id="puzzle-area">
<div class="puzzle-piece" style="background-image: url('https://via.placeholder.com/100x100.png?text=1');"></div>
<div class="puzzle-piece" style="background-image: url('https://via.placeholder.com/100x100.png?text=2');"></div>
<div class="puzzle-piece" style="background-image: url('https://via.placeholder.com/100x100.png?text=3');"></div>
<div class="puzzle-piece" style="background-image: url('https://via.placeholder.com/100x100.png?text=4');"></div>
<!-- more puzzle pieces as needed -->
</div>
</div>
</body>
</html>