/**** * Classes ****/ // Class for the base block var BaseBlock = Container.expand(function () { var self = Container.call(this); var baseBlockGraphics = self.attachAsset('base_block', { anchorX: 0.5, anchorY: 0.5, scaleX: 2, scaleY: 2 }); }); // Class for the blocks var Block = Container.expand(function () { var self = Container.call(this); var blockGraphics = self.attachAsset('block', { anchorX: 0.5, anchorY: 0.5, scaleX: 3, scaleY: 3 }); self.falling = false; self.speed = 20; self.update = function () { if (self.falling) { self.y += self.speed; self.x += self.momentum; } }; }); //<Assets used in the game will automatically appear here> //<Write imports for supported plugins here> // Class for the swinging crane var Crane = Container.expand(function () { var self = Container.call(this); var craneGraphics = self.attachAsset('crane', { anchorX: 0.5, anchorY: 0.0 }); self.angle = 0; self.speed = 0.03; self.direction = 1; self.update = function () { self.angle += self.speed * self.direction; if (self.angle > 1.5 || self.angle < -1.5) { self.direction *= -1; } self.rotation = self.angle; }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x87CEEB // Sky blue background }); /**** * Game Code ****/ // Initialize variables var crane = game.addChild(new Crane()); var baseBlock = game.addChild(new BaseBlock()); baseBlock.x = 2048 / 2; baseBlock.y = 2732 - baseBlock.height / 2; crane.x = 2048 / 2; crane.y = 50; // Move the crane up var blocks = []; var currentBlock = null; var towerHeight = 0; var scoreTxt = new Text2('0', { size: 100, fill: 0xFFFFFF }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); // Function to drop a block function dropBlock() { if (currentBlock && !currentBlock.falling) { currentBlock.falling = true; // Calculate the horizontal speed based on the crane's angular velocity var horizontalSpeed = Math.sin(crane.rotation) * crane.speed * 10; // Reduced scaling factor currentBlock.momentum = horizontalSpeed; blocks.push(currentBlock); currentBlock = null; } } // Function to check for collisions and update score function checkCollisions() { for (var i = blocks.length - 1; i >= 0; i--) { if (!blocks[i].falling) { continue; } if (blocks[i].intersects(baseBlock)) { blocks[i].falling = false; blocks[i].y = baseBlock.y - baseBlock.height / 2 - blocks[i].height / 2 - towerHeight * 100; towerHeight++; LK.setScore(towerHeight); scoreTxt.setText(LK.getScore()); // Slide base block and the blocks stacked upon it downwards LK.getSound('place').play(); } else { for (var j = 0; j < blocks.length; j++) { if (i !== j && blocks[i].falling && blocks[j].y < 2732 && !blocks[j].falling && blocks[i].intersects(blocks[j])) { if (Math.abs(blocks[i].x - blocks[j].x) > blocks[i].width / 4) { // Check if the block is not aligned correctly blocks[i].falling = true; blocks[i].speed = 10; // Add rotation and change trajectory based on the alignment if (blocks[i].x - blocks[j].x > 0) { blocks[i].rotation = 0.5; // Rotate to the right blocks[i].momentum += 5; // Fall to the right } else { blocks[i].rotation = -0.5; // Rotate to the left blocks[i].momentum -= 5; // Fall to the left } LK.getSound('gameover').play(); LK.setTimeout(function () { LK.showGameOver(); }, 1000); } else { blocks[i].falling = false; blocks[i].y = blocks[j].y - blocks[j].height / 2 - blocks[i].height / 2; LK.getSound('place').play(); // Play sound when block is aligned perfectly baseBlock.y += blocks[i].height; for (var j = 0; j < blocks.length; j++) { if (!blocks[j].falling) { blocks[j].y += blocks[i].height; } } // Update score when block is aligned perfectly towerHeight++; LK.setScore(towerHeight); scoreTxt.setText(LK.getScore()); break; } } } if (blocks[i].falling && blocks[i].y > 2732) { game.removeChild(blocks[i]); blocks.splice(i, 1); console.log("Block lost!"); } } } } // Game update loop game.update = function () { crane.update(); if (!currentBlock) { currentBlock = new Block(); // Position the new block directly attached to the crane's end currentBlock.x = crane.x + Math.sin(crane.rotation) * (crane.width / 2 + currentBlock.width / 2 + 200); // Increase the length of the rope currentBlock.y = crane.y + Math.cos(crane.rotation) * (crane.height / 2 + currentBlock.height / 2 + 200); // Increase the length of the rope game.addChild(currentBlock); } else if (!currentBlock.falling) { // Update the block's position to stay synced with the crane currentBlock.x = crane.x + Math.sin(crane.rotation) * (crane.width / 2 + currentBlock.width / 2 + 200); // Increase the length of the rope currentBlock.y = crane.y + Math.cos(crane.rotation) * (crane.height / 2 + currentBlock.height / 2 + 200); // Increase the length of the rope } if (currentBlock) { currentBlock.update(); } checkCollisions(); }; // Event listener for dropping blocks game.down = function (x, y, obj) { dropBlock(); // Remove tutorial text after user clicks or drops a block LK.gui.center.removeChild(tutorialTxt); }; // Add tutorial text var tutorialTxt = new Text2('Click to drop', { size: 100, fill: 0xFFFFFF }); tutorialTxt.anchor.set(0.5, 0.5); LK.gui.center.addChild(tutorialTxt); // Play background music LK.playMusic('bck_music', { loop: true }); ; ;
/****
* Classes
****/
// Class for the base block
var BaseBlock = Container.expand(function () {
var self = Container.call(this);
var baseBlockGraphics = self.attachAsset('base_block', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 2,
scaleY: 2
});
});
// Class for the blocks
var Block = Container.expand(function () {
var self = Container.call(this);
var blockGraphics = self.attachAsset('block', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 3,
scaleY: 3
});
self.falling = false;
self.speed = 20;
self.update = function () {
if (self.falling) {
self.y += self.speed;
self.x += self.momentum;
}
};
});
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Class for the swinging crane
var Crane = Container.expand(function () {
var self = Container.call(this);
var craneGraphics = self.attachAsset('crane', {
anchorX: 0.5,
anchorY: 0.0
});
self.angle = 0;
self.speed = 0.03;
self.direction = 1;
self.update = function () {
self.angle += self.speed * self.direction;
if (self.angle > 1.5 || self.angle < -1.5) {
self.direction *= -1;
}
self.rotation = self.angle;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB // Sky blue background
});
/****
* Game Code
****/
// Initialize variables
var crane = game.addChild(new Crane());
var baseBlock = game.addChild(new BaseBlock());
baseBlock.x = 2048 / 2;
baseBlock.y = 2732 - baseBlock.height / 2;
crane.x = 2048 / 2;
crane.y = 50; // Move the crane up
var blocks = [];
var currentBlock = null;
var towerHeight = 0;
var scoreTxt = new Text2('0', {
size: 100,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Function to drop a block
function dropBlock() {
if (currentBlock && !currentBlock.falling) {
currentBlock.falling = true;
// Calculate the horizontal speed based on the crane's angular velocity
var horizontalSpeed = Math.sin(crane.rotation) * crane.speed * 10; // Reduced scaling factor
currentBlock.momentum = horizontalSpeed;
blocks.push(currentBlock);
currentBlock = null;
}
}
// Function to check for collisions and update score
function checkCollisions() {
for (var i = blocks.length - 1; i >= 0; i--) {
if (!blocks[i].falling) {
continue;
}
if (blocks[i].intersects(baseBlock)) {
blocks[i].falling = false;
blocks[i].y = baseBlock.y - baseBlock.height / 2 - blocks[i].height / 2 - towerHeight * 100;
towerHeight++;
LK.setScore(towerHeight);
scoreTxt.setText(LK.getScore());
// Slide base block and the blocks stacked upon it downwards
LK.getSound('place').play();
} else {
for (var j = 0; j < blocks.length; j++) {
if (i !== j && blocks[i].falling && blocks[j].y < 2732 && !blocks[j].falling && blocks[i].intersects(blocks[j])) {
if (Math.abs(blocks[i].x - blocks[j].x) > blocks[i].width / 4) {
// Check if the block is not aligned correctly
blocks[i].falling = true;
blocks[i].speed = 10;
// Add rotation and change trajectory based on the alignment
if (blocks[i].x - blocks[j].x > 0) {
blocks[i].rotation = 0.5; // Rotate to the right
blocks[i].momentum += 5; // Fall to the right
} else {
blocks[i].rotation = -0.5; // Rotate to the left
blocks[i].momentum -= 5; // Fall to the left
}
LK.getSound('gameover').play();
LK.setTimeout(function () {
LK.showGameOver();
}, 1000);
} else {
blocks[i].falling = false;
blocks[i].y = blocks[j].y - blocks[j].height / 2 - blocks[i].height / 2;
LK.getSound('place').play(); // Play sound when block is aligned perfectly
baseBlock.y += blocks[i].height;
for (var j = 0; j < blocks.length; j++) {
if (!blocks[j].falling) {
blocks[j].y += blocks[i].height;
}
}
// Update score when block is aligned perfectly
towerHeight++;
LK.setScore(towerHeight);
scoreTxt.setText(LK.getScore());
break;
}
}
}
if (blocks[i].falling && blocks[i].y > 2732) {
game.removeChild(blocks[i]);
blocks.splice(i, 1);
console.log("Block lost!");
}
}
}
}
// Game update loop
game.update = function () {
crane.update();
if (!currentBlock) {
currentBlock = new Block();
// Position the new block directly attached to the crane's end
currentBlock.x = crane.x + Math.sin(crane.rotation) * (crane.width / 2 + currentBlock.width / 2 + 200); // Increase the length of the rope
currentBlock.y = crane.y + Math.cos(crane.rotation) * (crane.height / 2 + currentBlock.height / 2 + 200); // Increase the length of the rope
game.addChild(currentBlock);
} else if (!currentBlock.falling) {
// Update the block's position to stay synced with the crane
currentBlock.x = crane.x + Math.sin(crane.rotation) * (crane.width / 2 + currentBlock.width / 2 + 200); // Increase the length of the rope
currentBlock.y = crane.y + Math.cos(crane.rotation) * (crane.height / 2 + currentBlock.height / 2 + 200); // Increase the length of the rope
}
if (currentBlock) {
currentBlock.update();
}
checkCollisions();
};
// Event listener for dropping blocks
game.down = function (x, y, obj) {
dropBlock();
// Remove tutorial text after user clicks or drops a block
LK.gui.center.removeChild(tutorialTxt);
};
// Add tutorial text
var tutorialTxt = new Text2('Click to drop', {
size: 100,
fill: 0xFFFFFF
});
tutorialTxt.anchor.set(0.5, 0.5);
LK.gui.center.addChild(tutorialTxt);
// Play background music
LK.playMusic('bck_music', {
loop: true
});
;
;