Code edit (1 edits merged)
Please save this source code
User prompt
add icon of ball inside of buy button
Code edit (4 edits merged)
Please save this source code
User prompt
show cost of ball under the button
User prompt
Can you fix collision between ball and bricks
User prompt
when a brick is destroyed only add the points it had in the begining
User prompt
Please fix it
User prompt
Hardcode 10000 points on game start. will remove this later
User prompt
please implement it
User prompt
add a listener to track players tap on brick. console log every time the player touches a brick with a tap
User prompt
add a listner to track players taps on the screen
User prompt
Remove ball spawn on player tap
User prompt
for some reason player is ot beigg able to tap bricks. help
User prompt
bricks should be touchable
User prompt
bricks should be tapable by user
User prompt
refactor how player can touch bricks
Code edit (1 edits merged)
Please save this source code
User prompt
identify if player touches the coordinates of the screen where a brick is positioned
Code edit (1 edits merged)
Please save this source code
User prompt
can you draw the hitbox of the brick
User prompt
Please fix the bug: 'Graphics is not a constructor' in or related to this line: 'var hitbox = new Graphics();' Line Number: 47
User prompt
Please fix the bug: 'Graphics is not a constructor' in or related to this line: 'var hitbox = new Graphics();' Line Number: 47
User prompt
Make sure bricks can be touched in any part of their surface
User prompt
sometimes balls bounce close to a brick butu do not touch it and nor destroy it, can you fix that
User prompt
add a level sctructurue, level two should have more bricks and some should have 2 hitpints
/****
* Classes
****/
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Ball class representing the ball that breaks bricks
var Ball = Container.expand(function () {
var self = Container.call(this);
var ballGraphics = self.attachAsset('ball', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.direction = {
x: 1,
y: -1
};
self.update = function () {
self.x += self.speed * self.direction.x;
self.y += self.speed * self.direction.y;
// Bounce off walls
if (self.x <= 0 || self.x >= 2048) {
self.direction.x *= -1;
self.x = self.x <= 0 ? 0 : 2048; // Correct position to prevent getting stuck in the wall
}
if (self.y <= 0 || self.y >= 2632) {
self.direction.y *= -1;
self.y = self.y <= 0 ? 0 : 2632; // Correct position to prevent getting stuck in the wall
}
};
});
// Brick class representing the bricks to be broken
var Brick = Container.expand(function () {
var self = Container.call(this);
var brickGraphics = self.attachAsset('brick', {
anchorX: 0.5,
anchorY: 0.5
});
self.health = 1;
// Create a text object to display the brick's health
self.healthText = new Text2(self.health.toString(), {
size: 50,
fill: 0xFFFFFF
});
self.healthText.anchor.set(0.5, 0.5);
self.addChild(self.healthText);
self.hit = function () {
self.health -= 1;
score += 1;
self.healthText.setText(self.health.toString());
scoreTxt.setText(score.toString());
if (self.health <= 0) {
self.destroy();
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize game variables
// Create a HUD to display the points and the buy button
var hud = new Container();
LK.gui.topRight.addChild(hud);
// Display the points
var scoreTxt = new Text2('0', {
size: 100,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(1, 0); // Anchor to the top-right corner
hud.addChild(scoreTxt);
// Create a button to buy extra balls
var buyBallButton = LK.getAsset('BuyBall50', {
size: 80,
fill: 0xFFFFFF,
anchorX: 0.5,
anchorY: 0,
y: 0,
x: -300
});
buyBallButton.down = function () {
if (score >= 50) {
score -= 50; // Deduct points
scoreTxt.setText(score.toString()); // Update score display
createBall(); // Add a new ball
}
};
hud.addChild(buyBallButton);
var balls = [];
var bricks = [];
var score = 0;
// Create and position bricks
function createBricks() {
for (var i = 0; i < 5; i++) {
for (var j = 0; j < 10; j++) {
var brick = new Brick();
brick.x = 2048 / 2 - 10 * 160 / 2 + j * 160; // Center bricks horizontally
brick.y = 2732 / 2 - 5 * 120 / 2 + i * 120; // Center bricks vertically
bricks.push(brick);
game.addChild(brick);
}
}
}
// Create a new ball
function createBall() {
var ball = new Ball();
ball.x = 1024;
ball.y = 2000;
// Assign a random direction to the ball
ball.direction.x = Math.random() < 0.5 ? -1 : 1;
ball.direction.y = Math.random() < 0.5 ? -1 : 1;
balls.push(ball);
game.addChild(ball);
}
// Handle game updates
game.update = function () {
// Update balls
for (var i = balls.length - 1; i >= 0; i--) {
var ball = balls[i];
ball.update();
// Check for collisions with bricks
for (var j = bricks.length - 1; j >= 0; j--) {
var brick = bricks[j];
if (ball.intersects(brick)) {
brick.hit();
// Calculate bounce direction based on the position where the ball hit the brick
var hitPosition = (ball.x - brick.x) / brick.width;
ball.direction.x = hitPosition;
ball.direction.y *= -1;
score += 10;
}
}
// Remove ball if it goes off screen
if (ball.y > 2732) {
ball.destroy();
balls.splice(i, 1);
}
}
// Check for game over
if (balls.length === 0) {
createBall();
}
};
// Handle screen tap to launch a new ball
game.down = function (x, y, obj) {
// Check if a brick was touched
for (var i = 0; i < bricks.length; i++) {
var brick = bricks[i];
if (brick.intersects({
x: x,
y: y
})) {
brick.hit();
break;
}
}
// Check if the player has enough points to buy an extra ball
if (score >= 50) {
score -= 50; // Deduct points
scoreTxt.setText(score.toString()); // Update score display
createBall(); // Add a new ball
}
};
// Initialize game elements
createBricks();
createBall();