/**** * 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; // Ball collision logic }; }); //<Assets used in the game will automatically appear here> //<Write imports for supported plugins here> // Paddle class for player and AI var Paddle = Container.expand(function () { var self = Container.call(this); var paddleGraphics = self.attachAsset('paddle', { anchorX: 0.5, anchorY: 0.5 }); self.width = 500; self.height = 40; self.speed = 10; self.update = function () { // Paddle update logic }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ // Initialize score text var scoreTxt = new Text2('0', { size: 150, fill: 0xFFFFFF }); scoreTxt.anchor.set(0.5, 0); // Sets anchor to the center of the top edge of the text. LK.gui.top.addChild(scoreTxt); // Add the score text to the GUI overlay at the top-center of the screen. // Initialize paddles and ball var playerPaddle = game.addChild(new Paddle()); var aiPaddle = game.addChild(new Paddle()); var ball = game.addChild(new Ball()); // Position paddles and ball playerPaddle.x = 2048 / 2; playerPaddle.y = 2732 - 50; aiPaddle.x = 2048 / 2; aiPaddle.y = 50; ball.x = 2048 / 2; ball.y = 2732 / 2; // Game update logic game.update = function () { // Update paddles and ball playerPaddle.update(); aiPaddle.update(); ball.update(); // Improved AI for moving the AI paddle if (ball.x < aiPaddle.x - aiPaddle.width / 2) { aiPaddle.x -= aiPaddle.speed; } else if (ball.x > aiPaddle.x + aiPaddle.width / 2) { aiPaddle.x += aiPaddle.speed; } // Ball collision with walls if (ball.x <= 0 || ball.x >= 2048) { ball.speedX *= -1; LK.getSound('hit').play(); // Play sound when ball hits the wall } if (ball.y <= 0) { ball.speedY *= -1; LK.getSound('hit').play(); // Play sound when ball hits the wall } // Regenerate the ball when it doesn't hit the paddle or the AI paddle if (ball.y >= 2732 && !ball.intersects(playerPaddle)) { ball.x = 2048 / 2; ball.y = 2732 / 2; ball.speedY *= -1; ball.speedX = Math.random() < 0.5 ? -5 : 5; // Add random direction for the ball LK.getSound('falling').play(); // Play sound when ball falls } if (ball.y <= 0 && !ball.intersects(aiPaddle)) { ball.x = 2048 / 2; ball.y = 2732 / 2; ball.speedY *= -1; ball.speedX = Math.random() < 0.5 ? -5 : 5; // Add random direction for the ball LK.getSound('falling').play(); // Play sound when ball falls } // Ball collision with paddles if (ball.intersects(playerPaddle) || ball.intersects(aiPaddle)) { ball.speedY *= -1; LK.getSound('hit').play(); // Play sound when ball hits the paddle LK.setScore(LK.getScore() + 1); // Increase score by 1 scoreTxt.setText(LK.getScore()); // Update score text } // Increase ball speed every 10 seconds if (LK.ticks % 600 == 0) { ball.speedX *= 1.1; ball.speedY *= 1.1; } }; // Player paddle control game.move = function (x, y, obj) { playerPaddle.x = x; };
/****
* 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;
// Ball collision logic
};
});
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Paddle class for player and AI
var Paddle = Container.expand(function () {
var self = Container.call(this);
var paddleGraphics = self.attachAsset('paddle', {
anchorX: 0.5,
anchorY: 0.5
});
self.width = 500;
self.height = 40;
self.speed = 10;
self.update = function () {
// Paddle update logic
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize score text
var scoreTxt = new Text2('0', {
size: 150,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0); // Sets anchor to the center of the top edge of the text.
LK.gui.top.addChild(scoreTxt); // Add the score text to the GUI overlay at the top-center of the screen.
// Initialize paddles and ball
var playerPaddle = game.addChild(new Paddle());
var aiPaddle = game.addChild(new Paddle());
var ball = game.addChild(new Ball());
// Position paddles and ball
playerPaddle.x = 2048 / 2;
playerPaddle.y = 2732 - 50;
aiPaddle.x = 2048 / 2;
aiPaddle.y = 50;
ball.x = 2048 / 2;
ball.y = 2732 / 2;
// Game update logic
game.update = function () {
// Update paddles and ball
playerPaddle.update();
aiPaddle.update();
ball.update();
// Improved AI for moving the AI paddle
if (ball.x < aiPaddle.x - aiPaddle.width / 2) {
aiPaddle.x -= aiPaddle.speed;
} else if (ball.x > aiPaddle.x + aiPaddle.width / 2) {
aiPaddle.x += aiPaddle.speed;
}
// Ball collision with walls
if (ball.x <= 0 || ball.x >= 2048) {
ball.speedX *= -1;
LK.getSound('hit').play(); // Play sound when ball hits the wall
}
if (ball.y <= 0) {
ball.speedY *= -1;
LK.getSound('hit').play(); // Play sound when ball hits the wall
}
// Regenerate the ball when it doesn't hit the paddle or the AI paddle
if (ball.y >= 2732 && !ball.intersects(playerPaddle)) {
ball.x = 2048 / 2;
ball.y = 2732 / 2;
ball.speedY *= -1;
ball.speedX = Math.random() < 0.5 ? -5 : 5; // Add random direction for the ball
LK.getSound('falling').play(); // Play sound when ball falls
}
if (ball.y <= 0 && !ball.intersects(aiPaddle)) {
ball.x = 2048 / 2;
ball.y = 2732 / 2;
ball.speedY *= -1;
ball.speedX = Math.random() < 0.5 ? -5 : 5; // Add random direction for the ball
LK.getSound('falling').play(); // Play sound when ball falls
}
// Ball collision with paddles
if (ball.intersects(playerPaddle) || ball.intersects(aiPaddle)) {
ball.speedY *= -1;
LK.getSound('hit').play(); // Play sound when ball hits the paddle
LK.setScore(LK.getScore() + 1); // Increase score by 1
scoreTxt.setText(LK.getScore()); // Update score text
}
// Increase ball speed every 10 seconds
if (LK.ticks % 600 == 0) {
ball.speedX *= 1.1;
ball.speedY *= 1.1;
}
};
// Player paddle control
game.move = function (x, y, obj) {
playerPaddle.x = x;
};