/**** * Classes ****/ var LetterPiece = Container.expand(function () { var self = Container.call(this); self.assetId = 'letter' + String.fromCharCode(65 + Math.floor(Math.random() * 6)); var pieceGraphics = self.attachAsset(self.assetId, { anchorX: 0.5, anchorY: 0.5 }); self.correctPosition = { x: 0, y: 0 }; self.isCorrect = false; self.setCorrectPosition = function (x, y) { self.correctPosition.x = x; self.correctPosition.y = y; }; self.checkPosition = function () { if (Math.abs(self.x - self.correctPosition.x) < 10 && Math.abs(self.y - self.correctPosition.y) < 10) { self.isCorrect = true; self.x = self.correctPosition.x; self.y = self.correctPosition.y; } else { self.isCorrect = false; } }; self.down = function (x, y, obj) { dragNode = self; }; self.up = function (x, y, obj) { dragNode = null; self.checkPosition(); }; return self; }); //<Assets used in the game will automatically appear here> // PuzzlePiece class to represent each piece of the puzzle var PuzzlePiece = Container.expand(function () { var self = Container.call(this); self.assetId = 'piece' + (Math.floor(Math.random() * 10) + 1); var pieceGraphics = self.attachAsset(self.assetId, { anchorX: 0.5, anchorY: 0.5 }); self.correctPosition = { x: 0, y: 0 }; self.isCorrect = false; self.setCorrectPosition = function (x, y) { self.correctPosition.x = x; self.correctPosition.y = y; }; self.checkPosition = function () { if (Math.abs(self.x - self.correctPosition.x) < 10 && Math.abs(self.y - self.correctPosition.y) < 10) { self.isCorrect = true; self.x = self.correctPosition.x; self.y = self.correctPosition.y; } else { self.isCorrect = false; } self.joinSameColor(); }; self.joinSameColor = function () { pieces.forEach(function (piece) { if (piece !== self && piece.assetId === self.assetId && Math.abs(self.x - piece.x) < 10 && Math.abs(self.y - piece.y) < 10) { self.x = piece.x; self.y = piece.y; self.isCorrect = true; piece.isCorrect = true; } }); }; self.down = function (x, y, obj) { dragNode = self; }; self.up = function (x, y, obj) { dragNode = null; self.checkPosition(); }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ var background = LK.getAsset('background', { anchorX: 0.5, anchorY: 0.5, x: 2048 / 2, y: 2732 / 2 }); game.addChild(background); var scoreTxt = new Text2('Level: 1', { size: 100, fill: "#ffffff" }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); var pieces = []; var dragNode = null; var level = 1; var maxLevels = 80; var scoreTxt = new Text2('Level: 1', { size: 100, fill: "#ffffff" }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); function createPuzzle(level) { // Clear previous pieces pieces.forEach(function (piece) { return piece.destroy(); }); pieces = []; // Create new pieces for the current level for (var i = 0; i < 10; i++) { var piece = new LetterPiece(); piece.x = Math.random() * 2048; piece.y = Math.random() * 2732; piece.setCorrectPosition(100 + i * 150, 100 + i * 150); pieces.push(piece); game.addChild(piece); } } function handleMove(x, y, obj) { if (dragNode) { dragNode.x = x; dragNode.y = y; } } game.move = handleMove; game.down = function (x, y, obj) { handleMove(x, y, obj); }; game.up = function (x, y, obj) { dragNode = null; }; game.update = function () { if (pieces.every(function (piece) { return piece.isCorrect; })) { if (level < maxLevels) { level++; scoreTxt.setText('Level: ' + level); createPuzzle(level); } else { LK.showGameOver(); } } }; // Initialize the first level createPuzzle(level);
/****
* Classes
****/
var LetterPiece = Container.expand(function () {
var self = Container.call(this);
self.assetId = 'letter' + String.fromCharCode(65 + Math.floor(Math.random() * 6));
var pieceGraphics = self.attachAsset(self.assetId, {
anchorX: 0.5,
anchorY: 0.5
});
self.correctPosition = {
x: 0,
y: 0
};
self.isCorrect = false;
self.setCorrectPosition = function (x, y) {
self.correctPosition.x = x;
self.correctPosition.y = y;
};
self.checkPosition = function () {
if (Math.abs(self.x - self.correctPosition.x) < 10 && Math.abs(self.y - self.correctPosition.y) < 10) {
self.isCorrect = true;
self.x = self.correctPosition.x;
self.y = self.correctPosition.y;
} else {
self.isCorrect = false;
}
};
self.down = function (x, y, obj) {
dragNode = self;
};
self.up = function (x, y, obj) {
dragNode = null;
self.checkPosition();
};
return self;
});
//<Assets used in the game will automatically appear here>
// PuzzlePiece class to represent each piece of the puzzle
var PuzzlePiece = Container.expand(function () {
var self = Container.call(this);
self.assetId = 'piece' + (Math.floor(Math.random() * 10) + 1);
var pieceGraphics = self.attachAsset(self.assetId, {
anchorX: 0.5,
anchorY: 0.5
});
self.correctPosition = {
x: 0,
y: 0
};
self.isCorrect = false;
self.setCorrectPosition = function (x, y) {
self.correctPosition.x = x;
self.correctPosition.y = y;
};
self.checkPosition = function () {
if (Math.abs(self.x - self.correctPosition.x) < 10 && Math.abs(self.y - self.correctPosition.y) < 10) {
self.isCorrect = true;
self.x = self.correctPosition.x;
self.y = self.correctPosition.y;
} else {
self.isCorrect = false;
}
self.joinSameColor();
};
self.joinSameColor = function () {
pieces.forEach(function (piece) {
if (piece !== self && piece.assetId === self.assetId && Math.abs(self.x - piece.x) < 10 && Math.abs(self.y - piece.y) < 10) {
self.x = piece.x;
self.y = piece.y;
self.isCorrect = true;
piece.isCorrect = true;
}
});
};
self.down = function (x, y, obj) {
dragNode = self;
};
self.up = function (x, y, obj) {
dragNode = null;
self.checkPosition();
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
var background = LK.getAsset('background', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2,
y: 2732 / 2
});
game.addChild(background);
var scoreTxt = new Text2('Level: 1', {
size: 100,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
var pieces = [];
var dragNode = null;
var level = 1;
var maxLevels = 80;
var scoreTxt = new Text2('Level: 1', {
size: 100,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
function createPuzzle(level) {
// Clear previous pieces
pieces.forEach(function (piece) {
return piece.destroy();
});
pieces = [];
// Create new pieces for the current level
for (var i = 0; i < 10; i++) {
var piece = new LetterPiece();
piece.x = Math.random() * 2048;
piece.y = Math.random() * 2732;
piece.setCorrectPosition(100 + i * 150, 100 + i * 150);
pieces.push(piece);
game.addChild(piece);
}
}
function handleMove(x, y, obj) {
if (dragNode) {
dragNode.x = x;
dragNode.y = y;
}
}
game.move = handleMove;
game.down = function (x, y, obj) {
handleMove(x, y, obj);
};
game.up = function (x, y, obj) {
dragNode = null;
};
game.update = function () {
if (pieces.every(function (piece) {
return piece.isCorrect;
})) {
if (level < maxLevels) {
level++;
scoreTxt.setText('Level: ' + level);
createPuzzle(level);
} else {
LK.showGameOver();
}
}
};
// Initialize the first level
createPuzzle(level);