/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); /**** * Classes ****/ var Block = Container.expand(function (blockType) { var self = Container.call(this); var blockAssets = ['blockRed', 'blockBlue', 'blockGreen', 'blockYellow', 'blockPurple']; var assetId = blockAssets[blockType || 0]; var blockGraphics = self.attachAsset(assetId, { anchorX: 0.5, anchorY: 0.5 }); self.blockType = blockType || 0; self.isPlaced = false; self.isStable = false; self.velocityY = 0; self.velocityX = 0; self.gravity = 0.5; self.friction = 0.98; self.lastY = 0; self.stableTimer = 0; self.requiredStableTime = 30; // 0.5 seconds at 60fps self.update = function () { if (self.isPlaced && !self.isStable) { // Apply gravity self.velocityY += self.gravity; // Apply velocity self.x += self.velocityX; self.y += self.velocityY; // Ground collision if (self.y > 2732 - 30) { self.y = 2732 - 30; self.velocityY = 0; self.velocityX *= self.friction; } // Check collision with other blocks self.checkBlockCollisions(); // Check if block is stable if (Math.abs(self.velocityY) < 0.1 && Math.abs(self.velocityX) < 0.1) { self.stableTimer++; if (self.stableTimer >= self.requiredStableTime) { self.isStable = true; self.flashStable(); } } else { self.stableTimer = 0; } // Check if block fell off screen if (self.y > 2800) { self.triggerCollapse(); } } self.lastY = self.y; }; self.checkBlockCollisions = function () { for (var i = 0; i < placedBlocks.length; i++) { var otherBlock = placedBlocks[i]; if (otherBlock !== self && self.intersects(otherBlock)) { // Simple collision response if (self.y < otherBlock.y) { self.y = otherBlock.y - 60; self.velocityY = 0; self.velocityX *= self.friction; } } } }; self.flashStable = function () { var originalTint = blockGraphics.tint; tween(blockGraphics, { tint: 0xffffff }, { duration: 200 }); tween(blockGraphics, { tint: originalTint }, { duration: 200 }); LK.getSound('place').play(); // Award points LK.setScore(LK.getScore() + 10); scoreTxt.setText(LK.getScore()); }; self.triggerCollapse = function () { gameOver = true; LK.getSound('collapse').play(); LK.effects.flashScreen(0xff0000, 1000); LK.setTimeout(function () { LK.showGameOver(); }, 1000); }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x87ceeb }); /**** * Game Code ****/ var placedBlocks = []; var draggedBlock = null; var spawnTimer = 0; var spawnInterval = 180; // 3 seconds at 60fps var gameOver = false; // Create spawn area var spawnAreaGraphics = game.attachAsset('spawnArea', { anchorX: 0, anchorY: 0, x: 0, y: 2532 }); spawnAreaGraphics.alpha = 0.3; // Score display var scoreTxt = new Text2('0', { size: 80, fill: 0xFFFFFF }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); // Instructions var instructionTxt = new Text2('Drag blocks to build stable structures!', { size: 50, fill: 0xFFFFFF }); instructionTxt.anchor.set(0.5, 0); instructionTxt.y = 100; LK.gui.top.addChild(instructionTxt); function spawnNewBlock() { if (gameOver) return; var blockType = Math.floor(Math.random() * 5); var newBlock = new Block(blockType); // Random position in spawn area newBlock.x = 200 + Math.random() * (2048 - 400); newBlock.y = 2632; game.addChild(newBlock); // Remove old blocks from spawn area if too many var spawnBlocks = []; for (var i = 0; i < game.children.length; i++) { var child = game.children[i]; if (child instanceof Block && !child.isPlaced && child.y > 2500) { spawnBlocks.push(child); } } if (spawnBlocks.length > 5) { var oldestBlock = spawnBlocks[0]; oldestBlock.destroy(); } } function handleMove(x, y, obj) { if (draggedBlock && !gameOver) { draggedBlock.x = x; draggedBlock.y = y; } } game.move = handleMove; game.down = function (x, y, obj) { if (gameOver) return; // Check if touching a block in spawn area for (var i = game.children.length - 1; i >= 0; i--) { var child = game.children[i]; if (child instanceof Block && !child.isPlaced) { var localPos = child.toLocal({ x: x, y: y }); if (Math.abs(localPos.x) < 60 && Math.abs(localPos.y) < 30) { draggedBlock = child; break; } } } }; game.up = function (x, y, obj) { if (draggedBlock && !gameOver) { // Place the block draggedBlock.isPlaced = true; placedBlocks.push(draggedBlock); // Give initial downward velocity draggedBlock.velocityY = 2; draggedBlock = null; } }; // Spawn first block spawnNewBlock(); game.update = function () { if (gameOver) return; // Spawn new blocks spawnTimer++; if (spawnTimer >= spawnInterval) { spawnNewBlock(); spawnTimer = 0; // Gradually increase spawn rate if (spawnInterval > 60) { spawnInterval -= 2; } } // Check for unstable structures for (var i = placedBlocks.length - 1; i >= 0; i--) { var block = placedBlocks[i]; if (block.y > 2800) { // Remove from array placedBlocks.splice(i, 1); } } // Update score display scoreTxt.setText('Score: ' + LK.getScore()); };
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var Block = Container.expand(function (blockType) {
var self = Container.call(this);
var blockAssets = ['blockRed', 'blockBlue', 'blockGreen', 'blockYellow', 'blockPurple'];
var assetId = blockAssets[blockType || 0];
var blockGraphics = self.attachAsset(assetId, {
anchorX: 0.5,
anchorY: 0.5
});
self.blockType = blockType || 0;
self.isPlaced = false;
self.isStable = false;
self.velocityY = 0;
self.velocityX = 0;
self.gravity = 0.5;
self.friction = 0.98;
self.lastY = 0;
self.stableTimer = 0;
self.requiredStableTime = 30; // 0.5 seconds at 60fps
self.update = function () {
if (self.isPlaced && !self.isStable) {
// Apply gravity
self.velocityY += self.gravity;
// Apply velocity
self.x += self.velocityX;
self.y += self.velocityY;
// Ground collision
if (self.y > 2732 - 30) {
self.y = 2732 - 30;
self.velocityY = 0;
self.velocityX *= self.friction;
}
// Check collision with other blocks
self.checkBlockCollisions();
// Check if block is stable
if (Math.abs(self.velocityY) < 0.1 && Math.abs(self.velocityX) < 0.1) {
self.stableTimer++;
if (self.stableTimer >= self.requiredStableTime) {
self.isStable = true;
self.flashStable();
}
} else {
self.stableTimer = 0;
}
// Check if block fell off screen
if (self.y > 2800) {
self.triggerCollapse();
}
}
self.lastY = self.y;
};
self.checkBlockCollisions = function () {
for (var i = 0; i < placedBlocks.length; i++) {
var otherBlock = placedBlocks[i];
if (otherBlock !== self && self.intersects(otherBlock)) {
// Simple collision response
if (self.y < otherBlock.y) {
self.y = otherBlock.y - 60;
self.velocityY = 0;
self.velocityX *= self.friction;
}
}
}
};
self.flashStable = function () {
var originalTint = blockGraphics.tint;
tween(blockGraphics, {
tint: 0xffffff
}, {
duration: 200
});
tween(blockGraphics, {
tint: originalTint
}, {
duration: 200
});
LK.getSound('place').play();
// Award points
LK.setScore(LK.getScore() + 10);
scoreTxt.setText(LK.getScore());
};
self.triggerCollapse = function () {
gameOver = true;
LK.getSound('collapse').play();
LK.effects.flashScreen(0xff0000, 1000);
LK.setTimeout(function () {
LK.showGameOver();
}, 1000);
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87ceeb
});
/****
* Game Code
****/
var placedBlocks = [];
var draggedBlock = null;
var spawnTimer = 0;
var spawnInterval = 180; // 3 seconds at 60fps
var gameOver = false;
// Create spawn area
var spawnAreaGraphics = game.attachAsset('spawnArea', {
anchorX: 0,
anchorY: 0,
x: 0,
y: 2532
});
spawnAreaGraphics.alpha = 0.3;
// Score display
var scoreTxt = new Text2('0', {
size: 80,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Instructions
var instructionTxt = new Text2('Drag blocks to build stable structures!', {
size: 50,
fill: 0xFFFFFF
});
instructionTxt.anchor.set(0.5, 0);
instructionTxt.y = 100;
LK.gui.top.addChild(instructionTxt);
function spawnNewBlock() {
if (gameOver) return;
var blockType = Math.floor(Math.random() * 5);
var newBlock = new Block(blockType);
// Random position in spawn area
newBlock.x = 200 + Math.random() * (2048 - 400);
newBlock.y = 2632;
game.addChild(newBlock);
// Remove old blocks from spawn area if too many
var spawnBlocks = [];
for (var i = 0; i < game.children.length; i++) {
var child = game.children[i];
if (child instanceof Block && !child.isPlaced && child.y > 2500) {
spawnBlocks.push(child);
}
}
if (spawnBlocks.length > 5) {
var oldestBlock = spawnBlocks[0];
oldestBlock.destroy();
}
}
function handleMove(x, y, obj) {
if (draggedBlock && !gameOver) {
draggedBlock.x = x;
draggedBlock.y = y;
}
}
game.move = handleMove;
game.down = function (x, y, obj) {
if (gameOver) return;
// Check if touching a block in spawn area
for (var i = game.children.length - 1; i >= 0; i--) {
var child = game.children[i];
if (child instanceof Block && !child.isPlaced) {
var localPos = child.toLocal({
x: x,
y: y
});
if (Math.abs(localPos.x) < 60 && Math.abs(localPos.y) < 30) {
draggedBlock = child;
break;
}
}
}
};
game.up = function (x, y, obj) {
if (draggedBlock && !gameOver) {
// Place the block
draggedBlock.isPlaced = true;
placedBlocks.push(draggedBlock);
// Give initial downward velocity
draggedBlock.velocityY = 2;
draggedBlock = null;
}
};
// Spawn first block
spawnNewBlock();
game.update = function () {
if (gameOver) return;
// Spawn new blocks
spawnTimer++;
if (spawnTimer >= spawnInterval) {
spawnNewBlock();
spawnTimer = 0;
// Gradually increase spawn rate
if (spawnInterval > 60) {
spawnInterval -= 2;
}
}
// Check for unstable structures
for (var i = placedBlocks.length - 1; i >= 0; i--) {
var block = placedBlocks[i];
if (block.y > 2800) {
// Remove from array
placedBlocks.splice(i, 1);
}
}
// Update score display
scoreTxt.setText('Score: ' + LK.getScore());
};