/****
* Classes
****/
// Assets will be automatically created based on usage in the code.
// 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.move = function () {
self.x += self.speedX;
self.y += self.speedY;
};
});
// Brick class
var Brick = Container.expand(function () {
var self = Container.call(this);
var brickGraphics = self.attachAsset('brick', {
anchorX: 0.5,
anchorY: 0.5
});
});
// Paddle class
var Paddle = Container.expand(function () {
var self = Container.call(this);
var paddleGraphics = self.attachAsset('paddle', {
anchorX: 0.5,
anchorY: 0.5
});
self.move = function (xPosition) {
self.x = xPosition;
};
});
// PowerUp class
var PowerUp = Container.expand(function () {
var self = Container.call(this);
var powerUpGraphics = self.attachAsset('powerUp', {
anchorX: 0.5,
anchorY: 0.5
});
self.effect = function (ball) {
ball.width *= 1.5; // Increase ball size by 50%
ball.height *= 1.5; // Increase ball size by 50%
ballGraphics.scaleX *= 1.5; // Scale up the ball's graphic representation
ballGraphics.scaleY *= 1.5; // Scale up the ball's graphic representation
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 // Init game with black background
});
/****
* Game Code
****/
var ball = game.addChild(new Ball());
ball.x = 1024; // Center horizontally
ball.y = 1366; // Center vertically
var paddle = game.addChild(new Paddle());
paddle.x = 1024; // Center horizontally
paddle.y = 2500; // Position towards the bottom
var bricks = [];
var brickRowCount = 5;
var brickColumnCount = 8;
var brickWidth = 200;
var brickHeight = 100;
var brickPadding = 20;
var brickOffsetTop = 200;
var brickOffsetLeft = 204;
// Create bricks
for (var c = 0; c < brickColumnCount; c++) {
for (var r = 0; r < brickRowCount; r++) {
var brickX = c * (brickWidth + brickPadding) + brickOffsetLeft;
var brickY = r * (brickHeight + brickPadding) + brickOffsetTop;
var brick = game.addChild(new Brick());
brick.x = brickX + brickWidth / 2;
brick.y = brickY + brickHeight / 2;
bricks.push(brick);
}
}
// Handle touch move for paddle
game.on('move', function (obj) {
var pos = obj.event.getLocalPosition(game);
paddle.move(pos.x);
});
// Game tick
var spawnTick = 0; // Initialize spawn tick counter
LK.on('tick', function () {
spawnTick++;
// Every 600 ticks (10 seconds), spawn a new row of bricks at the top and move existing bricks down
if (spawnTick % 600 == 0) {
for (var c = 0; c < brickColumnCount; c++) {
var brickX = c * (brickWidth + brickPadding) + brickOffsetLeft;
var brick = game.addChild(new Brick());
brick.x = brickX + brickWidth / 2;
brick.y = brickOffsetTop + brickHeight / 2;
bricks.push(brick);
}
// Move existing bricks down
for (var i = 0; i < bricks.length; i++) {
bricks[i].y += brickHeight + brickPadding;
}
}
ball.move();
// Spawn PowerUp at game start and then randomly every 3000 ticks
if (spawnTick == 0 || spawnTick % 3000 == 0) {
var powerUpX = Math.random() * (2048 - 100) + 50; // Random X within game bounds
var powerUp = game.addChild(new PowerUp());
powerUp.x = powerUpX;
powerUp.y = 100; // Start from top
}
// Check for PowerUp collision with ball
for (var p = 0; p < game.children.length; p++) {
var child = game.children[p];
if (child instanceof PowerUp && ball.intersects(child)) {
child.effect(ball); // Apply PowerUp effect to the ball
child.destroy(); // Remove PowerUp after use
}
}
// Ball collision with walls
if (ball.x <= 0 || ball.x >= 2048) {
ball.speedX = -ball.speedX;
}
if (ball.y <= 0) {
ball.speedY = -ball.speedY;
}
// Ball collision with paddle
if (ball.intersects(paddle)) {
ball.speedY = -ball.speedY;
}
// Ball collision with bricks
for (var b = bricks.length - 1; b >= 0; b--) {
if (ball.intersects(bricks[b])) {
ball.speedY = -ball.speedY;
bricks[b].destroy();
bricks.splice(b, 1);
}
}
// Game over condition
if (ball.y >= 2732) {
LK.showGameOver();
}
}); ===================================================================
--- original.js
+++ change.js
@@ -108,10 +108,10 @@
bricks[i].y += brickHeight + brickPadding;
}
}
ball.move();
- // Spawn PowerUp randomly
- if (spawnTick % 3000 == 0) {
+ // Spawn PowerUp at game start and then randomly every 3000 ticks
+ if (spawnTick == 0 || spawnTick % 3000 == 0) {
var powerUpX = Math.random() * (2048 - 100) + 50; // Random X within game bounds
var powerUp = game.addChild(new PowerUp());
powerUp.x = powerUpX;
powerUp.y = 100; // Start from top