/**** * Classes ****/ // Ball class var Ball = Container.expand(function () { var self = Container.call(this); var ballGraphics = self.attachAsset('ball', { anchorX: 0.5, anchorY: 0.5 }); self.speedX = 5; self.speedY = -5; self.update = function () { self.x += self.speedX; self.y += self.speedY; }; self.bounce = function (axis) { if (axis === 'x') { self.speedX *= -1; } else if (axis === 'y') { self.speedY *= -1; } }; }); // Block class var Block = Container.expand(function () { var self = Container.call(this); var blockGraphics = self.attachAsset('block', { anchorX: 0.5, anchorY: 0.5 }); self.width = 100; self.height = 50; }); //<Assets used in the game will automatically appear here> //<Write imports for supported plugins here> // Paddle class var Paddle = Container.expand(function () { var self = Container.call(this); var paddleGraphics = self.attachAsset('paddle', { anchorX: 0.5, anchorY: 0.5 }); self.width = 300; self.height = 30; self.speed = 10; self.move = function (x) { self.x += (x - self.x) * 0.1; }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ // Function to generate blocks in different formations function generateBlocks() { // Clear existing blocks for (var i = blocks.length - 1; i >= 0; i--) { blocks[i].destroy(); } blocks = []; // Generate new blocks var formation = Math.floor(Math.random() * 3); // Randomly select a formation switch (formation) { case 0: // Formation 1: 5 rows of 10 blocks for (var i = 0; i < 5; i++) { for (var j = 0; j < 10; j++) { var block = new Block(); block.x = 200 + j * 150; block.y = 200 + i * 100; blocks.push(block); game.addChild(block); } } break; case 1: // Formation 2: 10 rows of 5 blocks for (var i = 0; i < 10; i++) { for (var j = 0; j < 5; j++) { var block = new Block(); block.x = 400 + j * 300; block.y = 200 + i * 100; blocks.push(block); game.addChild(block); } } break; case 2: // Formation 3: 7 rows of 7 blocks for (var i = 0; i < 7; i++) { for (var j = 0; j < 7; j++) { var block = new Block(); block.x = 300 + j * 200; block.y = 200 + i * 100; blocks.push(block); game.addChild(block); } } break; } } // Initialize paddle var paddle = game.addChild(new Paddle()); paddle.x = 2048 / 2; paddle.y = 2500; // Initialize ball var ball = game.addChild(new Ball()); ball.x = 2048 / 2; ball.y = 2400; // Initialize blocks var blocks = []; generateBlocks(); // Handle paddle movement game.move = function (x, y, obj) { paddle.move(x); }; // Update game state game.update = function () { ball.update(); // Ball collision with walls if (ball.x <= 0 || ball.x >= 2048) { ball.bounce('x'); } if (ball.y <= 0) { ball.bounce('y'); } // Ball collision with paddle if (ball.intersects(paddle)) { ball.bounce('y'); } // Ball collision with blocks for (var i = blocks.length - 1; i >= 0; i--) { if (ball.intersects(blocks[i])) { ball.bounce('y'); blocks[i].destroy(); blocks.splice(i, 1); } } // Check for game over if (ball.y > 2732) { LK.showGameOver(); } // Check for win condition if (blocks.length === 0) { generateBlocks(); } };
/****
* Classes
****/
// Ball class
var Ball = Container.expand(function () {
var self = Container.call(this);
var ballGraphics = self.attachAsset('ball', {
anchorX: 0.5,
anchorY: 0.5
});
self.speedX = 5;
self.speedY = -5;
self.update = function () {
self.x += self.speedX;
self.y += self.speedY;
};
self.bounce = function (axis) {
if (axis === 'x') {
self.speedX *= -1;
} else if (axis === 'y') {
self.speedY *= -1;
}
};
});
// Block class
var Block = Container.expand(function () {
var self = Container.call(this);
var blockGraphics = self.attachAsset('block', {
anchorX: 0.5,
anchorY: 0.5
});
self.width = 100;
self.height = 50;
});
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Paddle class
var Paddle = Container.expand(function () {
var self = Container.call(this);
var paddleGraphics = self.attachAsset('paddle', {
anchorX: 0.5,
anchorY: 0.5
});
self.width = 300;
self.height = 30;
self.speed = 10;
self.move = function (x) {
self.x += (x - self.x) * 0.1;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Function to generate blocks in different formations
function generateBlocks() {
// Clear existing blocks
for (var i = blocks.length - 1; i >= 0; i--) {
blocks[i].destroy();
}
blocks = [];
// Generate new blocks
var formation = Math.floor(Math.random() * 3); // Randomly select a formation
switch (formation) {
case 0:
// Formation 1: 5 rows of 10 blocks
for (var i = 0; i < 5; i++) {
for (var j = 0; j < 10; j++) {
var block = new Block();
block.x = 200 + j * 150;
block.y = 200 + i * 100;
blocks.push(block);
game.addChild(block);
}
}
break;
case 1:
// Formation 2: 10 rows of 5 blocks
for (var i = 0; i < 10; i++) {
for (var j = 0; j < 5; j++) {
var block = new Block();
block.x = 400 + j * 300;
block.y = 200 + i * 100;
blocks.push(block);
game.addChild(block);
}
}
break;
case 2:
// Formation 3: 7 rows of 7 blocks
for (var i = 0; i < 7; i++) {
for (var j = 0; j < 7; j++) {
var block = new Block();
block.x = 300 + j * 200;
block.y = 200 + i * 100;
blocks.push(block);
game.addChild(block);
}
}
break;
}
}
// Initialize paddle
var paddle = game.addChild(new Paddle());
paddle.x = 2048 / 2;
paddle.y = 2500;
// Initialize ball
var ball = game.addChild(new Ball());
ball.x = 2048 / 2;
ball.y = 2400;
// Initialize blocks
var blocks = [];
generateBlocks();
// Handle paddle movement
game.move = function (x, y, obj) {
paddle.move(x);
};
// Update game state
game.update = function () {
ball.update();
// Ball collision with walls
if (ball.x <= 0 || ball.x >= 2048) {
ball.bounce('x');
}
if (ball.y <= 0) {
ball.bounce('y');
}
// Ball collision with paddle
if (ball.intersects(paddle)) {
ball.bounce('y');
}
// Ball collision with blocks
for (var i = blocks.length - 1; i >= 0; i--) {
if (ball.intersects(blocks[i])) {
ball.bounce('y');
blocks[i].destroy();
blocks.splice(i, 1);
}
}
// Check for game over
if (ball.y > 2732) {
LK.showGameOver();
}
// Check for win condition
if (blocks.length === 0) {
generateBlocks();
}
};