/**** * 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 = 15; self.speedY = 15; self.update = function () { self.x += self.speedX; self.y += self.speedY; }; }); //<Assets used in the game will automatically appear here> // Paddle class for player and opponent var Paddle = Container.expand(function () { var self = Container.call(this); var paddleGraphics = self.attachAsset('paddle', { anchorX: 0.5, anchorY: 0.5, width: 400 }); self.width = paddleGraphics.width; self.height = paddleGraphics.height; self.update = function () { // Paddle update logic if needed }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x0000FF //Init game with blue background }); /**** * Game Code ****/ // Initialize paddles, ball and middle line var playerPaddle = new Paddle(); var opponentPaddle = new Paddle(); var ball = new Ball(); var middleLine = LK.getAsset('middleLine', { anchorX: 0.5, anchorY: 0.5, x: 2048 / 2, y: 2732 / 2 }); middleLine.alpha = 1; game.addChild(middleLine); // Initialize player and opponent scores var playerScore = 0; var opponentScore = 0; // Initialize round var round = 1; // Position paddles and ball playerPaddle.x = 2048 / 2; playerPaddle.y = 2732 - 100; opponentPaddle.x = 2048 / 2; opponentPaddle.y = 100; ball.x = 2048 / 2; ball.y = 2732 / 2; // Add paddles and ball to the game game.addChild(playerPaddle); game.addChild(opponentPaddle); game.addChild(ball); // Create score display text for player and opponent var playerScoreTxt = new Text2('0', { size: 100, fill: "#ffffff", font: "'Press Start 2P',Impact,'Arial Black',Tahoma" }); var opponentScoreTxt = new Text2('0', { size: 100, fill: "#ffffff", font: "'Press Start 2P',Impact,'Arial Black',Tahoma" }); // Position score display text playerScoreTxt.x = 100; playerScoreTxt.y = middleLine.y + 100; opponentScoreTxt.x = 100; opponentScoreTxt.y = middleLine.y - 200; // Add score display text to the game game.addChild(playerScoreTxt); game.addChild(opponentScoreTxt); // Handle player paddle movement game.move = function (x, y, obj) { playerPaddle.x = x; }; // Update game logic game.update = function () { // Update ball position ball.update(); // Check for collisions with paddles if (ball.intersects(playerPaddle)) { ball.speedY *= -1; ball.y = playerPaddle.y - playerPaddle.height / 2 - ball.height / 2; LK.getSound('paddleHit').play(); LK.effects.flashObject(playerPaddle, 0xffffff, 500); } if (ball.intersects(opponentPaddle)) { ball.speedY *= -1; ball.y = opponentPaddle.y + opponentPaddle.height / 2 + ball.height / 2; LK.getSound('paddleHit').play(); LK.effects.flashObject(opponentPaddle, 0xffffff, 500); } // Check for wall collisions if (ball.x <= 0 || ball.x >= 2048) { ball.speedX *= -1; } // Check for scoring if (ball.y <= 0 || ball.y >= 2732) { // Update scores if (ball.y <= 0) { playerScore += 1; playerScoreTxt.setText(playerScore.toString()); opponentScoreTxt.setText(opponentScore.toString()); // Increase the opponent's paddle speed each time the player scores opponentPaddle.speed += 1; // Play a sound when the player scores a point LK.getSound('scorePoint').play(); // Add an animation when the player scores LK.effects.flashScreen(0xffffff, 500); // Add screen shake effect when the player scores } else if (ball.y >= 2732) { opponentScore += 1; opponentScoreTxt.setText(opponentScore.toString()); // Update opponent score text // Play a sound when the opponent scores a point LK.getSound('opponentScorePoint').play(); // Make the screen flash red when the opponent scores LK.effects.flashScreen(0xff0000, 500); // Check if the opponent has scored three points and end the game if true if (opponentScore >= 3) { LK.showGameOver(); } } // Stop the ball ball.speedX = 0; ball.speedY = 0; // Reset ball position ball.x = 2048 / 2; ball.y = 2732 / 2; // Add a delay of 1 second before the ball shoots again LK.setTimeout(function () { // Alternate the direction of the ball based on the current round if (round % 2 === 0) { ball.speedY = -15; } else { ball.speedY = 15; } // Start the ball ball.speedX = 15; }, 1000); // Increment the round round++; } // Simple AI for opponent paddle with increased speed if (ball.x < opponentPaddle.x) { opponentPaddle.x -= 20; } else if (ball.x > opponentPaddle.x) { opponentPaddle.x += 20; } };
/****
* 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 = 15;
self.speedY = 15;
self.update = function () {
self.x += self.speedX;
self.y += self.speedY;
};
});
//<Assets used in the game will automatically appear here>
// Paddle class for player and opponent
var Paddle = Container.expand(function () {
var self = Container.call(this);
var paddleGraphics = self.attachAsset('paddle', {
anchorX: 0.5,
anchorY: 0.5,
width: 400
});
self.width = paddleGraphics.width;
self.height = paddleGraphics.height;
self.update = function () {
// Paddle update logic if needed
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x0000FF //Init game with blue background
});
/****
* Game Code
****/
// Initialize paddles, ball and middle line
var playerPaddle = new Paddle();
var opponentPaddle = new Paddle();
var ball = new Ball();
var middleLine = LK.getAsset('middleLine', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2,
y: 2732 / 2
});
middleLine.alpha = 1;
game.addChild(middleLine);
// Initialize player and opponent scores
var playerScore = 0;
var opponentScore = 0;
// Initialize round
var round = 1;
// Position paddles and ball
playerPaddle.x = 2048 / 2;
playerPaddle.y = 2732 - 100;
opponentPaddle.x = 2048 / 2;
opponentPaddle.y = 100;
ball.x = 2048 / 2;
ball.y = 2732 / 2;
// Add paddles and ball to the game
game.addChild(playerPaddle);
game.addChild(opponentPaddle);
game.addChild(ball);
// Create score display text for player and opponent
var playerScoreTxt = new Text2('0', {
size: 100,
fill: "#ffffff",
font: "'Press Start 2P',Impact,'Arial Black',Tahoma"
});
var opponentScoreTxt = new Text2('0', {
size: 100,
fill: "#ffffff",
font: "'Press Start 2P',Impact,'Arial Black',Tahoma"
});
// Position score display text
playerScoreTxt.x = 100;
playerScoreTxt.y = middleLine.y + 100;
opponentScoreTxt.x = 100;
opponentScoreTxt.y = middleLine.y - 200;
// Add score display text to the game
game.addChild(playerScoreTxt);
game.addChild(opponentScoreTxt);
// Handle player paddle movement
game.move = function (x, y, obj) {
playerPaddle.x = x;
};
// Update game logic
game.update = function () {
// Update ball position
ball.update();
// Check for collisions with paddles
if (ball.intersects(playerPaddle)) {
ball.speedY *= -1;
ball.y = playerPaddle.y - playerPaddle.height / 2 - ball.height / 2;
LK.getSound('paddleHit').play();
LK.effects.flashObject(playerPaddle, 0xffffff, 500);
}
if (ball.intersects(opponentPaddle)) {
ball.speedY *= -1;
ball.y = opponentPaddle.y + opponentPaddle.height / 2 + ball.height / 2;
LK.getSound('paddleHit').play();
LK.effects.flashObject(opponentPaddle, 0xffffff, 500);
}
// Check for wall collisions
if (ball.x <= 0 || ball.x >= 2048) {
ball.speedX *= -1;
}
// Check for scoring
if (ball.y <= 0 || ball.y >= 2732) {
// Update scores
if (ball.y <= 0) {
playerScore += 1;
playerScoreTxt.setText(playerScore.toString());
opponentScoreTxt.setText(opponentScore.toString());
// Increase the opponent's paddle speed each time the player scores
opponentPaddle.speed += 1;
// Play a sound when the player scores a point
LK.getSound('scorePoint').play();
// Add an animation when the player scores
LK.effects.flashScreen(0xffffff, 500);
// Add screen shake effect when the player scores
} else if (ball.y >= 2732) {
opponentScore += 1;
opponentScoreTxt.setText(opponentScore.toString()); // Update opponent score text
// Play a sound when the opponent scores a point
LK.getSound('opponentScorePoint').play();
// Make the screen flash red when the opponent scores
LK.effects.flashScreen(0xff0000, 500);
// Check if the opponent has scored three points and end the game if true
if (opponentScore >= 3) {
LK.showGameOver();
}
}
// Stop the ball
ball.speedX = 0;
ball.speedY = 0;
// Reset ball position
ball.x = 2048 / 2;
ball.y = 2732 / 2;
// Add a delay of 1 second before the ball shoots again
LK.setTimeout(function () {
// Alternate the direction of the ball based on the current round
if (round % 2 === 0) {
ball.speedY = -15;
} else {
ball.speedY = 15;
}
// Start the ball
ball.speedX = 15;
}, 1000);
// Increment the round
round++;
}
// Simple AI for opponent paddle with increased speed
if (ball.x < opponentPaddle.x) {
opponentPaddle.x -= 20;
} else if (ball.x > opponentPaddle.x) {
opponentPaddle.x += 20;
}
};