/****
* Classes
****/
//<Assets used in the game will automatically appear here>
// Block class representing each block in the game
var Block = Container.expand(function () {
var self = Container.call(this);
var blockGraphics = self.attachAsset('block', {
anchorX: 0.5,
anchorY: 0.5
});
self.width = blockGraphics.width;
self.height = blockGraphics.height;
self.speed = 5;
self.direction = 1; // 1 for right, -1 for left
self.update = function () {
self.x += self.speed * self.direction;
if (self.x + self.width / 2 > 2048 || self.x - self.width / 2 < 0) {
self.direction *= -1;
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize variables
var blocks = [];
var currentBlock = null;
var baseBlock = null;
var score = 0;
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Function to create a new block
function createBlock(y) {
var block = new Block();
block.x = 2048 / 2;
block.y = y;
blocks.push(block);
game.addChild(block);
return block;
}
// Initialize the base block
baseBlock = createBlock(2732 - 100);
// Function to handle block stacking
function stackBlock() {
if (!currentBlock) return;
var previousBlock = blocks[blocks.length - 2];
var overlap = Math.min(currentBlock.x + currentBlock.width / 2, previousBlock.x + previousBlock.width / 2) - Math.max(currentBlock.x - currentBlock.width / 2, previousBlock.x - previousBlock.width / 2);
if (overlap > 0) {
score++;
scoreTxt.setText(score);
currentBlock.width = overlap;
currentBlock.x = (currentBlock.x + previousBlock.x) / 2;
currentBlock = createBlock(currentBlock.y - currentBlock.height);
} else {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
// Initialize the first moving block
currentBlock = createBlock(2732 - 300);
// Event listener for touch/click
game.down = function (x, y, obj) {
stackBlock();
};
// Update function for the game
game.update = function () {
if (currentBlock) {
currentBlock.update();
}
};