/**** * Classes ****/ var Ball = Container.expand(function () { var self = Container.call(this); var ballGraphics = self.attachAsset('ball', { anchorX: 0.5, anchorY: 0.5 }); self.velocityX = 0; self.velocityY = 0; self.update = function () { self.x += self.velocityX; self.y += self.velocityY; }; return self; }); var Brick = Container.expand(function () { var self = Container.call(this); var brickGraphics = self.attachAsset('brick', { anchorX: 0.5, anchorY: 0.5 }); self.setColor = function (color) { brickGraphics.tint = color; }; return self; }); var Paddle = Container.expand(function () { var self = Container.call(this); var paddleGraphics = self.attachAsset('paddle', { anchorX: 0.5, anchorY: 0.5 }); return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 }); /**** * Game Code ****/ // Game constants var PADDLE_SPEED = 15; var BALL_SPEED = 8; var BRICK_ROWS = 3; var BRICK_COLS = 7; var BRICK_SPACING = 5; // Game variables var paddle; var ball; var bricks = []; var gameStarted = false; var ballLaunched = false; // Score display var scoreTxt = new Text2('0', { size: 80, fill: 0xFFFFFF }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); // Initialize paddle paddle = game.addChild(new Paddle()); paddle.x = 1024; paddle.y = 2400; // Initialize ball ball = game.addChild(new Ball()); ball.x = 1024; ball.y = 2355; // Create bricks var brickColors = [0xff0000, 0xff7700, 0xffff00, 0x00ff00, 0x0077ff, 0x7700ff]; for (var row = 0; row < BRICK_ROWS; row++) { for (var col = 0; col < BRICK_COLS; col++) { var brick = new Brick(); brick.x = 320 + col * (180 + BRICK_SPACING * 2); brick.y = 300 + row * (60 + BRICK_SPACING * 2); brick.setColor(brickColors[row % brickColors.length]); bricks.push(brick); game.addChild(brick); } } // Touch controls var touchX = null; game.down = function (x, y, obj) { touchX = x; if (!ballLaunched) { // Launch ball on first touch ballLaunched = true; ball.velocityX = BALL_SPEED * 0.7; ball.velocityY = -BALL_SPEED; } }; game.move = function (x, y, obj) { if (touchX !== null) { var deltaX = x - touchX; paddle.x += deltaX; // Keep paddle within screen bounds var halfPaddleWidth = 300; if (paddle.x < halfPaddleWidth) { paddle.x = halfPaddleWidth; } else if (paddle.x > 2048 - halfPaddleWidth) { paddle.x = 2048 - halfPaddleWidth; } // Move ball with paddle if not launched if (!ballLaunched) { ball.x = paddle.x; } touchX = x; } }; game.up = function (x, y, obj) { touchX = null; }; // Main game update game.update = function () { if (!ballLaunched) return; // Ball collision with walls if (ball.x <= 30 || ball.x >= 2018) { ball.velocityX = -ball.velocityX; LK.getSound('hit').play(); } // Ball collision with ceiling if (ball.y <= 30) { ball.velocityY = -ball.velocityY; LK.getSound('hit').play(); } // Ball fell off screen if (ball.y > 2732) { LK.showGameOver(); } // Ball collision with paddle if (ball.intersects(paddle) && ball.velocityY > 0) { ball.velocityY = -ball.velocityY; // Add spin based on where ball hits paddle var hitPosition = (ball.x - paddle.x) / 300; ball.velocityX = hitPosition * BALL_SPEED; LK.getSound('hit').play(); } // Ball collision with bricks for (var i = bricks.length - 1; i >= 0; i--) { var brick = bricks[i]; if (ball.intersects(brick)) { // Remove brick brick.destroy(); bricks.splice(i, 1); // Reverse ball direction ball.velocityY = -ball.velocityY; // Update score LK.setScore(LK.getScore() + 10); scoreTxt.setText(LK.getScore()); LK.getSound('break').play(); // Check win condition if (bricks.length === 0) { LK.showYouWin(); } break; } } };
/****
* Classes
****/
var Ball = Container.expand(function () {
var self = Container.call(this);
var ballGraphics = self.attachAsset('ball', {
anchorX: 0.5,
anchorY: 0.5
});
self.velocityX = 0;
self.velocityY = 0;
self.update = function () {
self.x += self.velocityX;
self.y += self.velocityY;
};
return self;
});
var Brick = Container.expand(function () {
var self = Container.call(this);
var brickGraphics = self.attachAsset('brick', {
anchorX: 0.5,
anchorY: 0.5
});
self.setColor = function (color) {
brickGraphics.tint = color;
};
return self;
});
var Paddle = Container.expand(function () {
var self = Container.call(this);
var paddleGraphics = self.attachAsset('paddle', {
anchorX: 0.5,
anchorY: 0.5
});
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000
});
/****
* Game Code
****/
// Game constants
var PADDLE_SPEED = 15;
var BALL_SPEED = 8;
var BRICK_ROWS = 3;
var BRICK_COLS = 7;
var BRICK_SPACING = 5;
// Game variables
var paddle;
var ball;
var bricks = [];
var gameStarted = false;
var ballLaunched = false;
// Score display
var scoreTxt = new Text2('0', {
size: 80,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Initialize paddle
paddle = game.addChild(new Paddle());
paddle.x = 1024;
paddle.y = 2400;
// Initialize ball
ball = game.addChild(new Ball());
ball.x = 1024;
ball.y = 2355;
// Create bricks
var brickColors = [0xff0000, 0xff7700, 0xffff00, 0x00ff00, 0x0077ff, 0x7700ff];
for (var row = 0; row < BRICK_ROWS; row++) {
for (var col = 0; col < BRICK_COLS; col++) {
var brick = new Brick();
brick.x = 320 + col * (180 + BRICK_SPACING * 2);
brick.y = 300 + row * (60 + BRICK_SPACING * 2);
brick.setColor(brickColors[row % brickColors.length]);
bricks.push(brick);
game.addChild(brick);
}
}
// Touch controls
var touchX = null;
game.down = function (x, y, obj) {
touchX = x;
if (!ballLaunched) {
// Launch ball on first touch
ballLaunched = true;
ball.velocityX = BALL_SPEED * 0.7;
ball.velocityY = -BALL_SPEED;
}
};
game.move = function (x, y, obj) {
if (touchX !== null) {
var deltaX = x - touchX;
paddle.x += deltaX;
// Keep paddle within screen bounds
var halfPaddleWidth = 300;
if (paddle.x < halfPaddleWidth) {
paddle.x = halfPaddleWidth;
} else if (paddle.x > 2048 - halfPaddleWidth) {
paddle.x = 2048 - halfPaddleWidth;
}
// Move ball with paddle if not launched
if (!ballLaunched) {
ball.x = paddle.x;
}
touchX = x;
}
};
game.up = function (x, y, obj) {
touchX = null;
};
// Main game update
game.update = function () {
if (!ballLaunched) return;
// Ball collision with walls
if (ball.x <= 30 || ball.x >= 2018) {
ball.velocityX = -ball.velocityX;
LK.getSound('hit').play();
}
// Ball collision with ceiling
if (ball.y <= 30) {
ball.velocityY = -ball.velocityY;
LK.getSound('hit').play();
}
// Ball fell off screen
if (ball.y > 2732) {
LK.showGameOver();
}
// Ball collision with paddle
if (ball.intersects(paddle) && ball.velocityY > 0) {
ball.velocityY = -ball.velocityY;
// Add spin based on where ball hits paddle
var hitPosition = (ball.x - paddle.x) / 300;
ball.velocityX = hitPosition * BALL_SPEED;
LK.getSound('hit').play();
}
// Ball collision with bricks
for (var i = bricks.length - 1; i >= 0; i--) {
var brick = bricks[i];
if (ball.intersects(brick)) {
// Remove brick
brick.destroy();
bricks.splice(i, 1);
// Reverse ball direction
ball.velocityY = -ball.velocityY;
// Update score
LK.setScore(LK.getScore() + 10);
scoreTxt.setText(LK.getScore());
LK.getSound('break').play();
// Check win condition
if (bricks.length === 0) {
LK.showYouWin();
}
break;
}
}
};