/**** * Classes ****/ //<Assets used in the game will automatically appear here> //<Write imports for supported plugins here> // Define a class for Tetris blocks var TetrisBlock = Container.expand(function () { var self = Container.call(this); self.blocks = []; self.shape = 'I'; // Default shape // Initialize the block with a specific shape self.init = function (shape) { self.shape = shape; self.blocks.forEach(function (block) { return block.destroy(); }); self.blocks = []; // Create blocks based on the shape switch (shape) { case 'O': var block = self.attachAsset('brick', { anchorX: 0.5, anchorY: 0.5 }); block.width = 65; // Set fixed width to 65 block.height = 65; // Set fixed height to 65 self.blocks.push(block); break; case 'I': for (var i = 0; i < 4; i++) { var block = self.attachAsset('brick', { anchorX: 0.5, anchorY: 0.5 }); block.width = 65; // Set fixed width to 65 block.height = 65; // Set fixed height to 65 block.x = i * block.width; self.blocks.push(block); } break; case 'T': for (var i = 0; i < 3; i++) { var block = self.attachAsset('brick', { anchorX: 0.5, anchorY: 0.5 }); block.width = 65; // Set fixed width to 65 block.height = 65; // Set fixed height to 65 block.x = i * block.width; self.blocks.push(block); } var middleBlock = self.attachAsset('brick', { anchorX: 0.5, anchorY: 0.5 }); middleBlock.width = 65; // Set fixed width to 65 middleBlock.height = 65; // Set fixed height to 65 middleBlock.x = block.width; middleBlock.y = block.height; self.blocks.push(middleBlock); break; case 'Z': for (var i = 0; i < 2; i++) { var block = self.attachAsset('brick', { anchorX: 0.5, anchorY: 0.5 }); block.width = 65; // Set fixed width to 65 block.height = 65; // Set fixed height to 65 block.x = i * block.width; self.blocks.push(block); } for (var i = 0; i < 2; i++) { var block = self.attachAsset('brick', { anchorX: 0.5, anchorY: 0.5 }); block.width = 65; // Set fixed width to 65 block.height = 65; // Set fixed height to 65 block.x = (i + 1) * block.width; block.y = block.height; self.blocks.push(block); } break; case 'L': for (var i = 0; i < 3; i++) { var block = self.attachAsset('brick', { anchorX: 0.5, anchorY: 0.5 }); block.width = 65; // Set fixed width to 65 block.height = 65; // Set fixed height to 65 block.x = i * block.width; self.blocks.push(block); } var block = self.attachAsset('brick', { anchorX: 0.5, anchorY: 0.5 }); block.width = 65; // Set fixed width to 65 block.height = 65; // Set fixed height to 65 block.x = 2 * block.width; block.y = block.height; self.blocks.push(block); break; } }; // Add rotation logic self.rotate = function () { if (self.update === function () {} || self.y + self.height >= 2732) { // Check if the block has stopped moving or reached the bottom return; // If it has, do not rotate } var centerX = 0; var centerY = 0; self.blocks.forEach(function (block) { centerX += block.x; centerY += block.y; }); centerX /= self.blocks.length; centerY /= self.blocks.length; self.blocks.forEach(function (block) { var x = block.y - centerY; var y = block.x - centerX; block.x = centerX - x; block.y = centerY + y; }); }; // Update the position of the block self.update = function () { self.y += 0.3125; // Move downwards at half the previous speed for (var i = 0; i < tetrisBlocks.length; i++) { if (self !== tetrisBlocks[i] && self.intersects(tetrisBlocks[i])) { if (!soundCooldown) { LK.getSound('A').play(); soundCooldown = true; LK.setTimeout(function () { soundCooldown = false; }, 100); // Cooldown to prevent rapid sound play } var overlap = Math.abs(self.x - tetrisBlocks[i].x); var halfWidth = self.blocks[0].width / 2; if (overlap < halfWidth) { self.x = tetrisBlocks[i].x; // Slide it in between } self.y = Math.floor((tetrisBlocks[i].y - self.height) / self.blocks[0].height) * self.blocks[0].height; // Align block directly on top of the intersecting block self.update = function () {}; // Stop the block from moving break; } } if (self.x + self.blocks[0].width > screen.x + screen.width) { self.x = screen.x + screen.width - self.blocks[0].width * self.blocks.length; } if (self.y + self.height >= screen.y + screen.height / 2) { // Stop at the bottom of the screen asset self.y = Math.floor((screen.y + screen.height / 2 - self.height) / self.blocks[0].height) * self.blocks[0].height; // Align block perfectly at the bottom self.update = function () {}; // Stop the block from moving } }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ // Define the logo variable before using it var logo = LK.getAsset('logo', { anchorX: 0.5, anchorY: 0.5, x: 1024, y: 100 }); // Add 'LA' asset to the map var la = game.attachAsset('LA', { anchorX: 0.5, anchorY: 0.5, x: 1416, y: 2395 }); game.setChildIndex(la, game.children.length - 1); var ja = game.attachAsset('JA', { anchorX: 0.5, anchorY: 0.5, x: la.x + 393, // Position 'JA' asset 200 units to the right of 'LA' y: la.y - 160 }); game.setChildIndex(ja, game.children.length - 1); // Add click event to the 'JA' asset to rotate the block right by 90 degrees ja.down = function (x, y, obj) { LK.getSound('B').play(); if (currentBlock) { // Rotate the block right by 90 degrees currentBlock.rotate(); } }; // Initialize variables var tetrisBlocks = []; var currentBlock = null; var blockShapes = ['I', 'T', 'Z', 'L', 'O']; var soundCooldown = false; // Cooldown flag for sound // Create a Text2 object to display the block counter var blockCounter = new Text2('Blocks: 0', { size: 50, fill: 0x000000 }); blockCounter.anchor.set(0, 0); // Set anchor to top-left corner blockCounter.x += 150; // Move the block counter right by 150 units blockCounter.y += 165; // Move the block counter down by 165 units LK.gui.topLeft.addChild(blockCounter); // Add the block counter to the upper left corner of the screen // Function to update the brick counter function updateBlockCounter() { blockCounter.setText('Blocks: ' + tetrisBlocks.length); } // Function to spawn a new block function spawnBlock() { var shape = blockShapes[Math.floor(Math.random() * blockShapes.length)]; currentBlock = new TetrisBlock(); currentBlock.init(shape); if (shape === 'T') { LK.getSound('T').play(); } if (currentBlock) { currentBlock.x = screen.x - currentBlock.width / 2; // Center horizontally within the screen asset currentBlock.y = screen.y - screen.height / 2 - currentBlock.height / 2; // Start from the exact top of the screen asset } game.addChildAt(currentBlock, game.getChildIndex(frame) - 1); tetrisBlocks.push(currentBlock); game.addChildAt(currentBlock, game.getChildIndex(screen)); updateBlockCounter(); } // Add screen asset to the top of the map var screen = game.attachAsset('screen', { anchorX: 0.5, anchorY: 0.5, x: 1024 - 15, y: 880 }); // Divide the screen into equal squares for proper brick distribution var gridSize = 75; // Size of each square in the grid var gridWidth = Math.floor(screen.width / gridSize); var gridHeight = Math.floor(screen.height / gridSize); var grid = Array.from({ length: gridHeight }, function () { return Array(gridWidth).fill(null); }); // Add frame asset to the center of the screen asset var frame = game.attachAsset('frame', { anchorX: 0.5, anchorY: 0.5, x: screen.x, y: screen.y }); game.setChildIndex(screen, game.children.length - 1); // Add handconsole asset to the game background var handconsole = game.attachAsset('handconsole', { anchorX: 0.5, anchorY: 0.5, x: 1024, y: 1366 }); game.setChildIndex(handconsole, game.children.length - 1); // Add title text 'AVA Brick Game' to the center of the handconsole var titleText = new Text2('AVA Brick Game', { size: 100, fill: 0x000000 }); titleText.anchor.set(0.5, 0.5); // Center the text titleText.x = handconsole.x; // Align with handconsole center titleText.y = handconsole.y + 225; // Move down by 25 units game.addChild(titleText); // Add 'left' asset to the center of the 'handconsole' asset var left = game.attachAsset('left', { anchorX: 0.5, anchorY: 0.5, x: handconsole.x - 780, y: handconsole.y + 995 }); game.setChildIndex(left, game.children.length - 1); // Add 'up' asset next to the 'left' asset right by 225 units and move it up by 169 units var up = game.attachAsset('up', { anchorX: 0.5, anchorY: 0.5, x: left.x + 225, y: left.y - 169 }); game.setChildIndex(up, game.children.length - 1); // Add 'right' asset next to the 'left' asset right by 445 units var right = game.attachAsset('right', { anchorX: 0.5, anchorY: 0.5, x: left.x + 445, y: left.y }); game.setChildIndex(right, game.children.length - 1); // Add 'down' asset next to the 'left' asset right by 225 units and move it down by 169 units var down = game.attachAsset('down', { anchorX: 0.5, anchorY: 0.5, x: left.x + 222, y: left.y + 169 }); game.setChildIndex(down, game.children.length - 1); // Add click event to the 'down' asset to play sound 'B' down.down = function (x, y, obj) { LK.getSound('B').play(); if (currentBlock) { currentBlock.y += 5; // Increase the falling speed of the current block } }; // Add 'LA' asset to the map var la = game.attachAsset('LA', { anchorX: 0.5, anchorY: 0.5, x: 1416, y: 2395 }); game.setChildIndex(la, game.children.length - 1); var restartButton = game.attachAsset('restart', { anchorX: 0.5, anchorY: 0.5, x: 1024, // Center restart button horizontally at the bottom of the map y: 2006 // Move restart button down by 640 units }); game.setChildIndex(restartButton, game.children.length - 1); restartButton.down = function (x, y, obj) { // Clear existing blocks LK.getSound('B').play(); tetrisBlocks.forEach(function (block) { block.destroy(); }); tetrisBlocks = []; updateBlockCounter(); currentBlock = null; // Spawn the first block again spawnBlock(); }; // Add 'JA' asset next to the 'LA' asset var ja = game.attachAsset('JA', { anchorX: 0.5, anchorY: 0.5, x: la.x + 393, // Position 'JA' asset 200 units to the right of 'LA' y: la.y - 160 }); game.setChildIndex(ja, game.children.length - 1); // Handle game update game.update = function () { if (!currentBlock) { spawnBlock(); } else { for (var i = 0; i < tetrisBlocks.length; i++) { if (currentBlock !== tetrisBlocks[i] && currentBlock.intersects(tetrisBlocks[i])) { // Check if the blocks are touching at the top of the screen if (currentBlock.y <= screen.y) { LK.showGameOver(); // Trigger game over return; } currentBlock = null; break; } } if (currentBlock) { if (currentBlock.y + currentBlock.height >= screen.y + screen.height / 2) { // Stop at the bottom of the screen asset currentBlock.y = screen.y + screen.height / 2 - currentBlock.height; currentBlock.update = function () {}; // Stop the block from moving spawnBlock(); } else { for (var i = 0; i < tetrisBlocks.length; i++) { if (currentBlock !== tetrisBlocks[i] && currentBlock.intersects(tetrisBlocks[i])) { if (currentBlock.y < tetrisBlocks[i].y) { currentBlock.y = Math.floor((tetrisBlocks[i].y - currentBlock.height) / currentBlock.blocks[0].height) * currentBlock.blocks[0].height; currentBlock.update = function () {}; // Stop the block from moving spawnBlock(); } else { currentBlock.y -= 0.625; // Move upwards to avoid collision } break; } } currentBlock.y += 2.5; // Double the speed when the down asset is clicked for (var i = 0; i < tetrisBlocks.length; i++) { if (currentBlock !== tetrisBlocks[i] && currentBlock.intersects(tetrisBlocks[i])) { // Check if the current block is above the intersecting block if (currentBlock.y < tetrisBlocks[i].y) { currentBlock.y -= 0.625; // Move upwards to avoid collision currentBlock.update = function () {}; // Stop the block from moving spawnBlock(); } else { // If the current block is not above, move it to the top of the display order game.setChildIndex(currentBlock, game.children.length - 1); } break; } } } } } }; // Start the game by spawning the first block spawnBlock(); // Add click event to the 'LA' asset to rotate the block left by 90 degrees la.down = function (x, y, obj) { LK.getSound('B').play(); if (currentBlock) { currentBlock.rotate(); } }; // Add click event to the game left.down = function (x, y, obj) { if (currentBlock && currentBlock.x - currentBlock.blocks[0].width >= 240) { var canMoveLeft = true; for (var i = 0; i < tetrisBlocks.length; i++) { if (currentBlock !== tetrisBlocks[i] && currentBlock.intersects(tetrisBlocks[i])) { canMoveLeft = false; break; } } if (canMoveLeft) { LK.getSound('B').play(); currentBlock.x -= currentBlock.blocks[0].width; } } }; // Add click event to the 'right' asset to move the block right by one cube right.down = function (x, y, obj) { if (currentBlock && currentBlock.x + currentBlock.blocks[0].width * currentBlock.blocks.length <= 2048 - 385) { var canMoveRight = true; for (var i = 0; i < tetrisBlocks.length; i++) { if (currentBlock !== tetrisBlocks[i] && currentBlock.intersects(tetrisBlocks[i])) { canMoveRight = false; break; } } if (canMoveRight) { LK.getSound('B').play(); currentBlock.x += currentBlock.blocks[0].width; } } }; // Add click event to the 'up' asset to decrease the block speed by half up.down = function (x, y, obj) { if (currentBlock) { LK.getSound('B').play(); currentBlock.y += 1.25; // Decrease the speed by half } };
/****
* Classes
****/
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Define a class for Tetris blocks
var TetrisBlock = Container.expand(function () {
var self = Container.call(this);
self.blocks = [];
self.shape = 'I'; // Default shape
// Initialize the block with a specific shape
self.init = function (shape) {
self.shape = shape;
self.blocks.forEach(function (block) {
return block.destroy();
});
self.blocks = [];
// Create blocks based on the shape
switch (shape) {
case 'O':
var block = self.attachAsset('brick', {
anchorX: 0.5,
anchorY: 0.5
});
block.width = 65; // Set fixed width to 65
block.height = 65; // Set fixed height to 65
self.blocks.push(block);
break;
case 'I':
for (var i = 0; i < 4; i++) {
var block = self.attachAsset('brick', {
anchorX: 0.5,
anchorY: 0.5
});
block.width = 65; // Set fixed width to 65
block.height = 65; // Set fixed height to 65
block.x = i * block.width;
self.blocks.push(block);
}
break;
case 'T':
for (var i = 0; i < 3; i++) {
var block = self.attachAsset('brick', {
anchorX: 0.5,
anchorY: 0.5
});
block.width = 65; // Set fixed width to 65
block.height = 65; // Set fixed height to 65
block.x = i * block.width;
self.blocks.push(block);
}
var middleBlock = self.attachAsset('brick', {
anchorX: 0.5,
anchorY: 0.5
});
middleBlock.width = 65; // Set fixed width to 65
middleBlock.height = 65; // Set fixed height to 65
middleBlock.x = block.width;
middleBlock.y = block.height;
self.blocks.push(middleBlock);
break;
case 'Z':
for (var i = 0; i < 2; i++) {
var block = self.attachAsset('brick', {
anchorX: 0.5,
anchorY: 0.5
});
block.width = 65; // Set fixed width to 65
block.height = 65; // Set fixed height to 65
block.x = i * block.width;
self.blocks.push(block);
}
for (var i = 0; i < 2; i++) {
var block = self.attachAsset('brick', {
anchorX: 0.5,
anchorY: 0.5
});
block.width = 65; // Set fixed width to 65
block.height = 65; // Set fixed height to 65
block.x = (i + 1) * block.width;
block.y = block.height;
self.blocks.push(block);
}
break;
case 'L':
for (var i = 0; i < 3; i++) {
var block = self.attachAsset('brick', {
anchorX: 0.5,
anchorY: 0.5
});
block.width = 65; // Set fixed width to 65
block.height = 65; // Set fixed height to 65
block.x = i * block.width;
self.blocks.push(block);
}
var block = self.attachAsset('brick', {
anchorX: 0.5,
anchorY: 0.5
});
block.width = 65; // Set fixed width to 65
block.height = 65; // Set fixed height to 65
block.x = 2 * block.width;
block.y = block.height;
self.blocks.push(block);
break;
}
};
// Add rotation logic
self.rotate = function () {
if (self.update === function () {} || self.y + self.height >= 2732) {
// Check if the block has stopped moving or reached the bottom
return; // If it has, do not rotate
}
var centerX = 0;
var centerY = 0;
self.blocks.forEach(function (block) {
centerX += block.x;
centerY += block.y;
});
centerX /= self.blocks.length;
centerY /= self.blocks.length;
self.blocks.forEach(function (block) {
var x = block.y - centerY;
var y = block.x - centerX;
block.x = centerX - x;
block.y = centerY + y;
});
};
// Update the position of the block
self.update = function () {
self.y += 0.3125; // Move downwards at half the previous speed
for (var i = 0; i < tetrisBlocks.length; i++) {
if (self !== tetrisBlocks[i] && self.intersects(tetrisBlocks[i])) {
if (!soundCooldown) {
LK.getSound('A').play();
soundCooldown = true;
LK.setTimeout(function () {
soundCooldown = false;
}, 100); // Cooldown to prevent rapid sound play
}
var overlap = Math.abs(self.x - tetrisBlocks[i].x);
var halfWidth = self.blocks[0].width / 2;
if (overlap < halfWidth) {
self.x = tetrisBlocks[i].x; // Slide it in between
}
self.y = Math.floor((tetrisBlocks[i].y - self.height) / self.blocks[0].height) * self.blocks[0].height; // Align block directly on top of the intersecting block
self.update = function () {}; // Stop the block from moving
break;
}
}
if (self.x + self.blocks[0].width > screen.x + screen.width) {
self.x = screen.x + screen.width - self.blocks[0].width * self.blocks.length;
}
if (self.y + self.height >= screen.y + screen.height / 2) {
// Stop at the bottom of the screen asset
self.y = Math.floor((screen.y + screen.height / 2 - self.height) / self.blocks[0].height) * self.blocks[0].height; // Align block perfectly at the bottom
self.update = function () {}; // Stop the block from moving
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Define the logo variable before using it
var logo = LK.getAsset('logo', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
y: 100
});
// Add 'LA' asset to the map
var la = game.attachAsset('LA', {
anchorX: 0.5,
anchorY: 0.5,
x: 1416,
y: 2395
});
game.setChildIndex(la, game.children.length - 1);
var ja = game.attachAsset('JA', {
anchorX: 0.5,
anchorY: 0.5,
x: la.x + 393,
// Position 'JA' asset 200 units to the right of 'LA'
y: la.y - 160
});
game.setChildIndex(ja, game.children.length - 1);
// Add click event to the 'JA' asset to rotate the block right by 90 degrees
ja.down = function (x, y, obj) {
LK.getSound('B').play();
if (currentBlock) {
// Rotate the block right by 90 degrees
currentBlock.rotate();
}
};
// Initialize variables
var tetrisBlocks = [];
var currentBlock = null;
var blockShapes = ['I', 'T', 'Z', 'L', 'O'];
var soundCooldown = false; // Cooldown flag for sound
// Create a Text2 object to display the block counter
var blockCounter = new Text2('Blocks: 0', {
size: 50,
fill: 0x000000
});
blockCounter.anchor.set(0, 0); // Set anchor to top-left corner
blockCounter.x += 150; // Move the block counter right by 150 units
blockCounter.y += 165; // Move the block counter down by 165 units
LK.gui.topLeft.addChild(blockCounter); // Add the block counter to the upper left corner of the screen
// Function to update the brick counter
function updateBlockCounter() {
blockCounter.setText('Blocks: ' + tetrisBlocks.length);
}
// Function to spawn a new block
function spawnBlock() {
var shape = blockShapes[Math.floor(Math.random() * blockShapes.length)];
currentBlock = new TetrisBlock();
currentBlock.init(shape);
if (shape === 'T') {
LK.getSound('T').play();
}
if (currentBlock) {
currentBlock.x = screen.x - currentBlock.width / 2; // Center horizontally within the screen asset
currentBlock.y = screen.y - screen.height / 2 - currentBlock.height / 2; // Start from the exact top of the screen asset
}
game.addChildAt(currentBlock, game.getChildIndex(frame) - 1);
tetrisBlocks.push(currentBlock);
game.addChildAt(currentBlock, game.getChildIndex(screen));
updateBlockCounter();
}
// Add screen asset to the top of the map
var screen = game.attachAsset('screen', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024 - 15,
y: 880
});
// Divide the screen into equal squares for proper brick distribution
var gridSize = 75; // Size of each square in the grid
var gridWidth = Math.floor(screen.width / gridSize);
var gridHeight = Math.floor(screen.height / gridSize);
var grid = Array.from({
length: gridHeight
}, function () {
return Array(gridWidth).fill(null);
});
// Add frame asset to the center of the screen asset
var frame = game.attachAsset('frame', {
anchorX: 0.5,
anchorY: 0.5,
x: screen.x,
y: screen.y
});
game.setChildIndex(screen, game.children.length - 1);
// Add handconsole asset to the game background
var handconsole = game.attachAsset('handconsole', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
y: 1366
});
game.setChildIndex(handconsole, game.children.length - 1);
// Add title text 'AVA Brick Game' to the center of the handconsole
var titleText = new Text2('AVA Brick Game', {
size: 100,
fill: 0x000000
});
titleText.anchor.set(0.5, 0.5); // Center the text
titleText.x = handconsole.x; // Align with handconsole center
titleText.y = handconsole.y + 225; // Move down by 25 units
game.addChild(titleText);
// Add 'left' asset to the center of the 'handconsole' asset
var left = game.attachAsset('left', {
anchorX: 0.5,
anchorY: 0.5,
x: handconsole.x - 780,
y: handconsole.y + 995
});
game.setChildIndex(left, game.children.length - 1);
// Add 'up' asset next to the 'left' asset right by 225 units and move it up by 169 units
var up = game.attachAsset('up', {
anchorX: 0.5,
anchorY: 0.5,
x: left.x + 225,
y: left.y - 169
});
game.setChildIndex(up, game.children.length - 1);
// Add 'right' asset next to the 'left' asset right by 445 units
var right = game.attachAsset('right', {
anchorX: 0.5,
anchorY: 0.5,
x: left.x + 445,
y: left.y
});
game.setChildIndex(right, game.children.length - 1);
// Add 'down' asset next to the 'left' asset right by 225 units and move it down by 169 units
var down = game.attachAsset('down', {
anchorX: 0.5,
anchorY: 0.5,
x: left.x + 222,
y: left.y + 169
});
game.setChildIndex(down, game.children.length - 1);
// Add click event to the 'down' asset to play sound 'B'
down.down = function (x, y, obj) {
LK.getSound('B').play();
if (currentBlock) {
currentBlock.y += 5; // Increase the falling speed of the current block
}
};
// Add 'LA' asset to the map
var la = game.attachAsset('LA', {
anchorX: 0.5,
anchorY: 0.5,
x: 1416,
y: 2395
});
game.setChildIndex(la, game.children.length - 1);
var restartButton = game.attachAsset('restart', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
// Center restart button horizontally at the bottom of the map
y: 2006 // Move restart button down by 640 units
});
game.setChildIndex(restartButton, game.children.length - 1);
restartButton.down = function (x, y, obj) {
// Clear existing blocks
LK.getSound('B').play();
tetrisBlocks.forEach(function (block) {
block.destroy();
});
tetrisBlocks = [];
updateBlockCounter();
currentBlock = null;
// Spawn the first block again
spawnBlock();
};
// Add 'JA' asset next to the 'LA' asset
var ja = game.attachAsset('JA', {
anchorX: 0.5,
anchorY: 0.5,
x: la.x + 393,
// Position 'JA' asset 200 units to the right of 'LA'
y: la.y - 160
});
game.setChildIndex(ja, game.children.length - 1);
// Handle game update
game.update = function () {
if (!currentBlock) {
spawnBlock();
} else {
for (var i = 0; i < tetrisBlocks.length; i++) {
if (currentBlock !== tetrisBlocks[i] && currentBlock.intersects(tetrisBlocks[i])) {
// Check if the blocks are touching at the top of the screen
if (currentBlock.y <= screen.y) {
LK.showGameOver(); // Trigger game over
return;
}
currentBlock = null;
break;
}
}
if (currentBlock) {
if (currentBlock.y + currentBlock.height >= screen.y + screen.height / 2) {
// Stop at the bottom of the screen asset
currentBlock.y = screen.y + screen.height / 2 - currentBlock.height;
currentBlock.update = function () {}; // Stop the block from moving
spawnBlock();
} else {
for (var i = 0; i < tetrisBlocks.length; i++) {
if (currentBlock !== tetrisBlocks[i] && currentBlock.intersects(tetrisBlocks[i])) {
if (currentBlock.y < tetrisBlocks[i].y) {
currentBlock.y = Math.floor((tetrisBlocks[i].y - currentBlock.height) / currentBlock.blocks[0].height) * currentBlock.blocks[0].height;
currentBlock.update = function () {}; // Stop the block from moving
spawnBlock();
} else {
currentBlock.y -= 0.625; // Move upwards to avoid collision
}
break;
}
}
currentBlock.y += 2.5; // Double the speed when the down asset is clicked
for (var i = 0; i < tetrisBlocks.length; i++) {
if (currentBlock !== tetrisBlocks[i] && currentBlock.intersects(tetrisBlocks[i])) {
// Check if the current block is above the intersecting block
if (currentBlock.y < tetrisBlocks[i].y) {
currentBlock.y -= 0.625; // Move upwards to avoid collision
currentBlock.update = function () {}; // Stop the block from moving
spawnBlock();
} else {
// If the current block is not above, move it to the top of the display order
game.setChildIndex(currentBlock, game.children.length - 1);
}
break;
}
}
}
}
}
};
// Start the game by spawning the first block
spawnBlock();
// Add click event to the 'LA' asset to rotate the block left by 90 degrees
la.down = function (x, y, obj) {
LK.getSound('B').play();
if (currentBlock) {
currentBlock.rotate();
}
};
// Add click event to the game
left.down = function (x, y, obj) {
if (currentBlock && currentBlock.x - currentBlock.blocks[0].width >= 240) {
var canMoveLeft = true;
for (var i = 0; i < tetrisBlocks.length; i++) {
if (currentBlock !== tetrisBlocks[i] && currentBlock.intersects(tetrisBlocks[i])) {
canMoveLeft = false;
break;
}
}
if (canMoveLeft) {
LK.getSound('B').play();
currentBlock.x -= currentBlock.blocks[0].width;
}
}
};
// Add click event to the 'right' asset to move the block right by one cube
right.down = function (x, y, obj) {
if (currentBlock && currentBlock.x + currentBlock.blocks[0].width * currentBlock.blocks.length <= 2048 - 385) {
var canMoveRight = true;
for (var i = 0; i < tetrisBlocks.length; i++) {
if (currentBlock !== tetrisBlocks[i] && currentBlock.intersects(tetrisBlocks[i])) {
canMoveRight = false;
break;
}
}
if (canMoveRight) {
LK.getSound('B').play();
currentBlock.x += currentBlock.blocks[0].width;
}
}
};
// Add click event to the 'up' asset to decrease the block speed by half
up.down = function (x, y, obj) {
if (currentBlock) {
LK.getSound('B').play();
currentBlock.y += 1.25; // Decrease the speed by half
}
};