/****
* 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;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// 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 = [];
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);
}
}
// 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) {
LK.showYouWin();
}
};