/**** * Classes ****/ //<Assets used in the game will automatically appear here> // BeeIcon class to represent each bee icon in the puzzle var BeeIcon = Container.expand(function () { var self = Container.call(this); var beeGraphics = self.attachAsset('beeIcon', { anchorX: 0.5, anchorY: 0.5 }); self.correctPosition = { x: 0, y: 0 }; // Store the correct position for the bee icon self.setPosition = function (x, y) { self.x = x; self.y = y; }; self.isInCorrectPosition = function () { return self.x === self.correctPosition.x && self.y === self.correctPosition.y; }; self.containsPoint = function (point) { return point.x >= self.x && point.x <= self.x + pieceSize && point.y >= self.y && point.y <= self.y + pieceSize; }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ // Initialize puzzle pieces var puzzlePieces = []; var pieceSize = 200; // Size of each puzzle piece var gridSize = 3; // 3x3 grid for the puzzle var offsetX = (2048 - gridSize * pieceSize) / 2; // Center the puzzle horizontally var offsetY = (2732 - gridSize * pieceSize) / 2; // Center the puzzle vertically // Create bee icons and set their initial positions for (var i = 0; i < gridSize; i++) { for (var j = 0; j < gridSize; j++) { var bee = new BeeIcon(); bee.correctPosition.x = offsetX + j * pieceSize; bee.correctPosition.y = offsetY + i * pieceSize; bee.setPosition(bee.correctPosition.x, bee.correctPosition.y); puzzlePieces.push(bee); game.addChild(bee); } } // Shuffle the puzzle pieces function shufflePieces() { for (var i = puzzlePieces.length - 1; i > 0; i--) { var j = Math.floor(Math.random() * (i + 1)); var temp = puzzlePieces[i]; puzzlePieces[i] = puzzlePieces[j]; puzzlePieces[j] = temp; } // Set shuffled positions for (var k = 0; k < puzzlePieces.length; k++) { var x = offsetX + k % gridSize * pieceSize; var y = offsetY + Math.floor(k / gridSize) * pieceSize; puzzlePieces[k].setPosition(x, y); } } // Check if the puzzle is solved function checkPuzzleSolved() { for (var i = 0; i < puzzlePieces.length; i++) { if (!puzzlePieces[i].isInCorrectPosition()) { return false; } } return true; } // Handle piece dragging var selectedPiece = null; game.down = function (x, y, obj) { for (var i = 0; i < puzzlePieces.length; i++) { if (puzzlePieces[i].containsPoint({ x: x, y: y })) { selectedPiece = puzzlePieces[i]; break; } } }; game.move = function (x, y, obj) { if (selectedPiece) { selectedPiece.setPosition(x, y); } }; game.up = function (x, y, obj) { if (selectedPiece) { // Snap to grid var snappedX = Math.round((selectedPiece.x - offsetX) / pieceSize) * pieceSize + offsetX; var snappedY = Math.round((selectedPiece.y - offsetY) / pieceSize) * pieceSize + offsetY; selectedPiece.setPosition(snappedX, snappedY); // Check if the puzzle is solved if (checkPuzzleSolved()) { console.log("Puzzle Solved!"); // Optionally, show a message or trigger a game over screen } selectedPiece = null; } }; // Shuffle pieces at the start shufflePieces();
===================================================================
--- original.js
+++ change.js
@@ -1,19 +1,19 @@
/****
* Classes
****/
//<Assets used in the game will automatically appear here>
-// PuzzlePiece class to represent each piece of the puzzle
-var PuzzlePiece = Container.expand(function () {
+// BeeIcon class to represent each bee icon in the puzzle
+var BeeIcon = Container.expand(function () {
var self = Container.call(this);
- var pieceGraphics = self.attachAsset('puzzlePiece', {
+ var beeGraphics = self.attachAsset('beeIcon', {
anchorX: 0.5,
anchorY: 0.5
});
self.correctPosition = {
x: 0,
y: 0
- }; // Store the correct position for the piece
+ }; // Store the correct position for the bee icon
self.setPosition = function (x, y) {
self.x = x;
self.y = y;
};
@@ -40,17 +40,17 @@
var pieceSize = 200; // Size of each puzzle piece
var gridSize = 3; // 3x3 grid for the puzzle
var offsetX = (2048 - gridSize * pieceSize) / 2; // Center the puzzle horizontally
var offsetY = (2732 - gridSize * pieceSize) / 2; // Center the puzzle vertically
-// Create puzzle pieces and set their initial positions
+// Create bee icons and set their initial positions
for (var i = 0; i < gridSize; i++) {
for (var j = 0; j < gridSize; j++) {
- var piece = new PuzzlePiece();
- piece.correctPosition.x = offsetX + j * pieceSize;
- piece.correctPosition.y = offsetY + i * pieceSize;
- piece.setPosition(piece.correctPosition.x, piece.correctPosition.y);
- puzzlePieces.push(piece);
- game.addChild(piece);
+ var bee = new BeeIcon();
+ bee.correctPosition.x = offsetX + j * pieceSize;
+ bee.correctPosition.y = offsetY + i * pieceSize;
+ bee.setPosition(bee.correctPosition.x, bee.correctPosition.y);
+ puzzlePieces.push(bee);
+ game.addChild(bee);
}
}
// Shuffle the puzzle pieces
function shufflePieces() {