/****
* Classes
****/
// Assets will be automatically created and loaded by the LK engine based on usage in the code.
// Block class for stackable game blocks
var Block = Container.expand(function () {
var self = Container.call(this);
// Attach a square asset to represent the block
var blockGraphics = self.attachAsset('block', {
width: 600,
height: 150,
color: 0xFFFFFF,
shape: 'box',
anchorX: 0.5,
anchorY: 0.5
});
});
// HeightIndicator class for the height indicator
var HeightIndicator = Container.expand(function () {
var self = Container.call(this);
// Attach a Text2 object to represent the height indicator
var heightIndicatorText = new Text2('0', {
size: 200,
fill: "#ffffff"
});
self.addChild(heightIndicatorText);
// Update method called every game tick
self.update = function () {
// Update the text to display the current height
heightIndicatorText.setText(blocks.length - 1);
};
});
// Player class for the block that the player controls
var PlayerBlock = Container.expand(function () {
var self = Container.call(this);
// Attach a square asset to represent the player block
var playerBlockGraphics = self.attachAsset('playerBlock', {
width: 600,
height: 150,
color: 0xFFD700,
shape: 'box',
anchorX: 0.5,
anchorY: 0.5
});
// Set initial speed for horizontal movement
self.speed = 5;
// Direction of movement, 1 for right, -1 for left
self.direction = 1;
// Update method called every game tick
self.update = function () {
// Move the player block left or right
self.x += self.speed * self.direction;
// Reverse direction when hitting screen bounds
if (self.x <= 300 || self.x >= 1748) {
self.direction *= -1;
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 // Init game with black background
});
/****
* Game Code
****/
var background = game.attachAsset('Background', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
y: 1366,
width: 2048,
height: 2732
});
// Initialize height indicator
var heightIndicator = game.addChild(new HeightIndicator());
// Set initial position of the height indicator
heightIndicator.x = 1024; // Center of the screen
heightIndicator.y = 100; // Position the score display a little more down on the screen
// Initialize player block
var playerBlock = game.addChild(new PlayerBlock());
// Set initial position of the player block
playerBlock.x = 512; // More on the left of the screen
playerBlock.y = 966; // Moved up by its height from its previous position
// Initialize an array to keep track of stacked blocks
var blocks = [];
// Add a starting block to the game
addBlock();
// Function to add a new block to the stack
function addBlock() {
// Increase player block speed every 10 stacks
if (blocks.length % 10 == 0) {
playerBlock.speed += 1;
}
var block = new Block();
// Change the color of the block after each placement
var colors = [0x15b523, 0x4b2b81, 0xf65884, 0xFFD700, 0xFFFFFF];
var currentColorIndex = blocks.length % colors.length;
block.attachAsset('block', {
width: 600,
height: 150,
color: colors[currentColorIndex],
shape: 'box',
anchorX: 0.5,
anchorY: 0.5
});
// Change the color of the player block after each placement
playerBlock.attachAsset('playerBlock', {
width: 600,
height: 150,
color: colors[currentColorIndex],
shape: 'box',
anchorX: 0.5,
anchorY: 0.5
});
if (blocks.length == 0) {
block.x = 1024; // Set the new block's position to the center of the screen
} else {
block.x = playerBlock.x; // Set the new block's position to the player block's current position
}
block.y = playerBlock.y + playerBlock.height; // Move the starting block under the playerBlock
blocks.push(block); // Add the new block to the array
game.addChild(block); // Add the new block to the game
// Move all existing blocks down
for (var i = 0; i < blocks.length - 1; i++) {
blocks[i].y += 150;
}
// Adjust the size and position of the block based on the block directly under it
if (blocks.length > 1) {
var directlyUnderBlock = blocks[blocks.length - 2];
var deltaX = block.x - directlyUnderBlock.x;
if (Math.abs(deltaX) < directlyUnderBlock.width) {
// If there is a block directly under it, adjust the width and position based on the overlap
var newWidth = Math.min(block.width, directlyUnderBlock.width - Math.abs(deltaX));
block.width = newWidth;
block.x = directlyUnderBlock.x + deltaX / 2;
playerBlock.width = newWidth;
playerBlock.x = directlyUnderBlock.x + deltaX / 2;
} else {
// If there is no block directly under it, consider it a miss and trigger game over logic
LK.showGameOver();
}
}
// Removed the overlap check with all other blocks except the last one
}
// Event listener for touch or mouse down to place the player block
var canPlaceBlock = true;
game.down = function (x, y, obj) {
if (canPlaceBlock) {
addBlock(); // Add the current player block to the stack
playerBlock.x = 512; // Reset player block to its starting position on each placement
playerBlock.direction = 1; // Ensure player block always starts moving right
canPlaceBlock = false;
LK.setTimeout(function () {
canPlaceBlock = true;
}, 100);
// Check if the placed block is overlapping with any other blocks
var isOverlapping = false;
for (var i = 0; i < blocks.length - 1; i++) {
// Calculate the overlap between the current block and the previous block
var overlap = Math.abs(blocks[blocks.length - 1].x - blocks[i].x);
// If the overlap is less than the width of the block, they are overlapping
// Decrease the required overlap for a successful placement
if (overlap < blocks[blocks.length - 1].width * 0.75) {
isOverlapping = true;
break;
}
}
// If the placed block is not overlapping with any other blocks and it's not the first block, end the game
// Add a grace period of 500ms before checking for overlap
LK.setTimeout(function () {
if (!isOverlapping && blocks.length > 1) {
LK.showGameOver();
}
}, 10);
}
};
// Update function called every game tick
game.update = function () {
// Removed the game over condition when the score reaches 20
// Update the height indicator
heightIndicator.update();
};
// Note: The game does not handle dynamic resizing, orientation changes, or provide pause functionality as per the LK engine's automatic handling. /****
* Classes
****/
// Assets will be automatically created and loaded by the LK engine based on usage in the code.
// Block class for stackable game blocks
var Block = Container.expand(function () {
var self = Container.call(this);
// Attach a square asset to represent the block
var blockGraphics = self.attachAsset('block', {
width: 600,
height: 150,
color: 0xFFFFFF,
shape: 'box',
anchorX: 0.5,
anchorY: 0.5
});
});
// HeightIndicator class for the height indicator
var HeightIndicator = Container.expand(function () {
var self = Container.call(this);
// Attach a Text2 object to represent the height indicator
var heightIndicatorText = new Text2('0', {
size: 200,
fill: "#ffffff"
});
self.addChild(heightIndicatorText);
// Update method called every game tick
self.update = function () {
// Update the text to display the current height
heightIndicatorText.setText(blocks.length - 1);
};
});
// Player class for the block that the player controls
var PlayerBlock = Container.expand(function () {
var self = Container.call(this);
// Attach a square asset to represent the player block
var playerBlockGraphics = self.attachAsset('playerBlock', {
width: 600,
height: 150,
color: 0xFFD700,
shape: 'box',
anchorX: 0.5,
anchorY: 0.5
});
// Set initial speed for horizontal movement
self.speed = 5;
// Direction of movement, 1 for right, -1 for left
self.direction = 1;
// Update method called every game tick
self.update = function () {
// Move the player block left or right
self.x += self.speed * self.direction;
// Reverse direction when hitting screen bounds
if (self.x <= 300 || self.x >= 1748) {
self.direction *= -1;
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 // Init game with black background
});
/****
* Game Code
****/
var background = game.attachAsset('Background', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
y: 1366,
width: 2048,
height: 2732
});
// Initialize height indicator
var heightIndicator = game.addChild(new HeightIndicator());
// Set initial position of the height indicator
heightIndicator.x = 1024; // Center of the screen
heightIndicator.y = 100; // Position the score display a little more down on the screen
// Initialize player block
var playerBlock = game.addChild(new PlayerBlock());
// Set initial position of the player block
playerBlock.x = 512; // More on the left of the screen
playerBlock.y = 966; // Moved up by its height from its previous position
// Initialize an array to keep track of stacked blocks
var blocks = [];
// Add a starting block to the game
addBlock();
// Function to add a new block to the stack
function addBlock() {
// Increase player block speed every 10 stacks
if (blocks.length % 10 == 0) {
playerBlock.speed += 1;
}
var block = new Block();
// Change the color of the block after each placement
var colors = [0x15b523, 0x4b2b81, 0xf65884, 0xFFD700, 0xFFFFFF];
var currentColorIndex = blocks.length % colors.length;
block.attachAsset('block', {
width: 600,
height: 150,
color: colors[currentColorIndex],
shape: 'box',
anchorX: 0.5,
anchorY: 0.5
});
// Change the color of the player block after each placement
playerBlock.attachAsset('playerBlock', {
width: 600,
height: 150,
color: colors[currentColorIndex],
shape: 'box',
anchorX: 0.5,
anchorY: 0.5
});
if (blocks.length == 0) {
block.x = 1024; // Set the new block's position to the center of the screen
} else {
block.x = playerBlock.x; // Set the new block's position to the player block's current position
}
block.y = playerBlock.y + playerBlock.height; // Move the starting block under the playerBlock
blocks.push(block); // Add the new block to the array
game.addChild(block); // Add the new block to the game
// Move all existing blocks down
for (var i = 0; i < blocks.length - 1; i++) {
blocks[i].y += 150;
}
// Adjust the size and position of the block based on the block directly under it
if (blocks.length > 1) {
var directlyUnderBlock = blocks[blocks.length - 2];
var deltaX = block.x - directlyUnderBlock.x;
if (Math.abs(deltaX) < directlyUnderBlock.width) {
// If there is a block directly under it, adjust the width and position based on the overlap
var newWidth = Math.min(block.width, directlyUnderBlock.width - Math.abs(deltaX));
block.width = newWidth;
block.x = directlyUnderBlock.x + deltaX / 2;
playerBlock.width = newWidth;
playerBlock.x = directlyUnderBlock.x + deltaX / 2;
} else {
// If there is no block directly under it, consider it a miss and trigger game over logic
LK.showGameOver();
}
}
// Removed the overlap check with all other blocks except the last one
}
// Event listener for touch or mouse down to place the player block
var canPlaceBlock = true;
game.down = function (x, y, obj) {
if (canPlaceBlock) {
addBlock(); // Add the current player block to the stack
playerBlock.x = 512; // Reset player block to its starting position on each placement
playerBlock.direction = 1; // Ensure player block always starts moving right
canPlaceBlock = false;
LK.setTimeout(function () {
canPlaceBlock = true;
}, 100);
// Check if the placed block is overlapping with any other blocks
var isOverlapping = false;
for (var i = 0; i < blocks.length - 1; i++) {
// Calculate the overlap between the current block and the previous block
var overlap = Math.abs(blocks[blocks.length - 1].x - blocks[i].x);
// If the overlap is less than the width of the block, they are overlapping
// Decrease the required overlap for a successful placement
if (overlap < blocks[blocks.length - 1].width * 0.75) {
isOverlapping = true;
break;
}
}
// If the placed block is not overlapping with any other blocks and it's not the first block, end the game
// Add a grace period of 500ms before checking for overlap
LK.setTimeout(function () {
if (!isOverlapping && blocks.length > 1) {
LK.showGameOver();
}
}, 10);
}
};
// Update function called every game tick
game.update = function () {
// Removed the game over condition when the score reaches 20
// Update the height indicator
heightIndicator.update();
};
// Note: The game does not handle dynamic resizing, orientation changes, or provide pause functionality as per the LK engine's automatic handling.