/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); /**** * Classes ****/ var Ball = Container.expand(function () { var self = Container.call(this); var ballGraphics = self.attachAsset('ball', { anchorX: 0.5, anchorY: 0.5 }); self.velocityX = 0; self.velocityY = 0; self.baseSpeed = 8; self.speedMultiplier = 1; self.reset = function () { self.x = 2048 / 2; self.y = 2732 / 2; self.speedMultiplier = 1; // Random starting direction var angle = (Math.random() - 0.5) * Math.PI / 3; // ±30 degrees var direction = Math.random() < 0.5 ? 1 : -1; self.velocityX = Math.cos(angle) * self.baseSpeed * direction; self.velocityY = Math.sin(angle) * self.baseSpeed; }; self.update = function () { self.x += self.velocityX * self.speedMultiplier; self.y += self.velocityY * self.speedMultiplier; // Ball collision with top and bottom walls var ballRadius = ballGraphics.height / 2; if (self.y - ballRadius <= 0 || self.y + ballRadius >= 2732) { self.velocityY = -self.velocityY; self.y = Math.max(ballRadius, Math.min(2732 - ballRadius, self.y)); LK.getSound('wallHit').play(); } // Ball collision with paddles if (self.intersects(leftPaddle) && self.velocityX < 0) { self.velocityX = -self.velocityX; self.speedMultiplier += 0.1; // Add some spin based on paddle hit position var paddleCenter = leftPaddle.y; var hitOffset = (self.y - paddleCenter) / 75; // Normalize hit position self.velocityY += hitOffset * 2; LK.getSound('paddleHit').play(); LK.effects.flashObject(leftPaddle, 0x00ff00, 200); } if (self.intersects(rightPaddle) && self.velocityX > 0) { self.velocityX = -self.velocityX; self.speedMultiplier += 0.1; // Add some spin based on paddle hit position var paddleCenter = rightPaddle.y; var hitOffset = (self.y - paddleCenter) / 75; // Normalize hit position self.velocityY += hitOffset * 2; LK.getSound('paddleHit').play(); LK.effects.flashObject(rightPaddle, 0x00ff00, 200); } // Score detection if (self.x < 0) { // Right player scores rightScore++; rightScoreText.setText(rightScore.toString()); LK.getSound('score').play(); LK.effects.flashScreen(0x0000ff, 500); self.reset(); } if (self.x > 2048) { // Left player scores leftScore++; leftScoreText.setText(leftScore.toString()); LK.getSound('score').play(); LK.effects.flashScreen(0xff0000, 500); self.reset(); } }; return self; }); var Paddle = Container.expand(function () { var self = Container.call(this); var paddleGraphics = self.attachAsset('paddle', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 8; self.targetY = 0; self.update = function () { // Smooth paddle movement towards target var diff = self.targetY - self.y; if (Math.abs(diff) > 5) { self.y += diff * 0.15; } else { self.y = self.targetY; } // Keep paddle within screen bounds var halfHeight = paddleGraphics.height / 2; if (self.y - halfHeight < 0) { self.y = halfHeight; self.targetY = halfHeight; } if (self.y + halfHeight > 2732) { self.y = 2732 - halfHeight; self.targetY = 2732 - halfHeight; } }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 }); /**** * Game Code ****/ // Game variables var leftScore = 0; var rightScore = 0; var leftPaddle; var rightPaddle; var ball; var centerLine; var leftScoreText; var rightScoreText; var dragTarget = null; // Create center line centerLine = game.addChild(LK.getAsset('centerLine', { anchorX: 0.5, anchorY: 0.5 })); centerLine.x = 2048 / 2; centerLine.y = 2732 / 2; // Create paddles leftPaddle = game.addChild(new Paddle()); leftPaddle.x = 50; leftPaddle.y = 2732 / 2; rightPaddle = game.addChild(new Paddle()); rightPaddle.x = 2048 - 50; rightPaddle.y = 2732 / 2; // Create ball ball = game.addChild(new Ball()); ball.reset(); // Create score display leftScoreText = new Text2('0', { size: 120, fill: 0xFFFFFF }); leftScoreText.anchor.set(0.5, 0.5); leftScoreText.x = 2048 / 4; leftScoreText.y = 200; LK.gui.center.addChild(leftScoreText); rightScoreText = new Text2('0', { size: 120, fill: 0xFFFFFF }); rightScoreText.anchor.set(0.5, 0.5); rightScoreText.x = 2048 / 4 * 3; rightScoreText.y = 200; LK.gui.center.addChild(rightScoreText); // Create win text displays var leftWinText = new Text2('WIN!', { size: 200, fill: 0x00FF00 }); leftWinText.anchor.set(0.5, 0.5); leftWinText.x = 2048 / 4; leftWinText.y = 2732 / 2; leftWinText.visible = false; LK.gui.center.addChild(leftWinText); var rightWinText = new Text2('WIN!', { size: 200, fill: 0x00FF00 }); rightWinText.anchor.set(0.5, 0.5); rightWinText.x = 2048 / 4 * 3; rightWinText.y = 2732 / 2; rightWinText.visible = false; LK.gui.center.addChild(rightWinText); // Touch controls game.down = function (x, y, obj) { if (x < 2048 / 2) { // Left side - control left paddle dragTarget = leftPaddle; } else { // Right side - control right paddle dragTarget = rightPaddle; } if (dragTarget) { dragTarget.targetY = y; } }; game.move = function (x, y, obj) { if (dragTarget) { dragTarget.targetY = y; } }; game.up = function (x, y, obj) { dragTarget = null; }; // Main game loop game.update = function () { // Check for winning condition if (leftScore >= 11 || rightScore >= 11) { if (Math.abs(leftScore - rightScore) >= 2) { // Show which player won if (leftScore > rightScore) { leftWinText.visible = true; LK.effects.flashObject(leftWinText, 0xFFFF00, 1000); } else { rightWinText.visible = true; LK.effects.flashObject(rightWinText, 0xFFFF00, 1000); } // Delay showing game over to let players see the win text LK.setTimeout(function () { LK.showYouWin(); }, 1500); } } };
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var Ball = Container.expand(function () {
var self = Container.call(this);
var ballGraphics = self.attachAsset('ball', {
anchorX: 0.5,
anchorY: 0.5
});
self.velocityX = 0;
self.velocityY = 0;
self.baseSpeed = 8;
self.speedMultiplier = 1;
self.reset = function () {
self.x = 2048 / 2;
self.y = 2732 / 2;
self.speedMultiplier = 1;
// Random starting direction
var angle = (Math.random() - 0.5) * Math.PI / 3; // ±30 degrees
var direction = Math.random() < 0.5 ? 1 : -1;
self.velocityX = Math.cos(angle) * self.baseSpeed * direction;
self.velocityY = Math.sin(angle) * self.baseSpeed;
};
self.update = function () {
self.x += self.velocityX * self.speedMultiplier;
self.y += self.velocityY * self.speedMultiplier;
// Ball collision with top and bottom walls
var ballRadius = ballGraphics.height / 2;
if (self.y - ballRadius <= 0 || self.y + ballRadius >= 2732) {
self.velocityY = -self.velocityY;
self.y = Math.max(ballRadius, Math.min(2732 - ballRadius, self.y));
LK.getSound('wallHit').play();
}
// Ball collision with paddles
if (self.intersects(leftPaddle) && self.velocityX < 0) {
self.velocityX = -self.velocityX;
self.speedMultiplier += 0.1;
// Add some spin based on paddle hit position
var paddleCenter = leftPaddle.y;
var hitOffset = (self.y - paddleCenter) / 75; // Normalize hit position
self.velocityY += hitOffset * 2;
LK.getSound('paddleHit').play();
LK.effects.flashObject(leftPaddle, 0x00ff00, 200);
}
if (self.intersects(rightPaddle) && self.velocityX > 0) {
self.velocityX = -self.velocityX;
self.speedMultiplier += 0.1;
// Add some spin based on paddle hit position
var paddleCenter = rightPaddle.y;
var hitOffset = (self.y - paddleCenter) / 75; // Normalize hit position
self.velocityY += hitOffset * 2;
LK.getSound('paddleHit').play();
LK.effects.flashObject(rightPaddle, 0x00ff00, 200);
}
// Score detection
if (self.x < 0) {
// Right player scores
rightScore++;
rightScoreText.setText(rightScore.toString());
LK.getSound('score').play();
LK.effects.flashScreen(0x0000ff, 500);
self.reset();
}
if (self.x > 2048) {
// Left player scores
leftScore++;
leftScoreText.setText(leftScore.toString());
LK.getSound('score').play();
LK.effects.flashScreen(0xff0000, 500);
self.reset();
}
};
return self;
});
var Paddle = Container.expand(function () {
var self = Container.call(this);
var paddleGraphics = self.attachAsset('paddle', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 8;
self.targetY = 0;
self.update = function () {
// Smooth paddle movement towards target
var diff = self.targetY - self.y;
if (Math.abs(diff) > 5) {
self.y += diff * 0.15;
} else {
self.y = self.targetY;
}
// Keep paddle within screen bounds
var halfHeight = paddleGraphics.height / 2;
if (self.y - halfHeight < 0) {
self.y = halfHeight;
self.targetY = halfHeight;
}
if (self.y + halfHeight > 2732) {
self.y = 2732 - halfHeight;
self.targetY = 2732 - halfHeight;
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000
});
/****
* Game Code
****/
// Game variables
var leftScore = 0;
var rightScore = 0;
var leftPaddle;
var rightPaddle;
var ball;
var centerLine;
var leftScoreText;
var rightScoreText;
var dragTarget = null;
// Create center line
centerLine = game.addChild(LK.getAsset('centerLine', {
anchorX: 0.5,
anchorY: 0.5
}));
centerLine.x = 2048 / 2;
centerLine.y = 2732 / 2;
// Create paddles
leftPaddle = game.addChild(new Paddle());
leftPaddle.x = 50;
leftPaddle.y = 2732 / 2;
rightPaddle = game.addChild(new Paddle());
rightPaddle.x = 2048 - 50;
rightPaddle.y = 2732 / 2;
// Create ball
ball = game.addChild(new Ball());
ball.reset();
// Create score display
leftScoreText = new Text2('0', {
size: 120,
fill: 0xFFFFFF
});
leftScoreText.anchor.set(0.5, 0.5);
leftScoreText.x = 2048 / 4;
leftScoreText.y = 200;
LK.gui.center.addChild(leftScoreText);
rightScoreText = new Text2('0', {
size: 120,
fill: 0xFFFFFF
});
rightScoreText.anchor.set(0.5, 0.5);
rightScoreText.x = 2048 / 4 * 3;
rightScoreText.y = 200;
LK.gui.center.addChild(rightScoreText);
// Create win text displays
var leftWinText = new Text2('WIN!', {
size: 200,
fill: 0x00FF00
});
leftWinText.anchor.set(0.5, 0.5);
leftWinText.x = 2048 / 4;
leftWinText.y = 2732 / 2;
leftWinText.visible = false;
LK.gui.center.addChild(leftWinText);
var rightWinText = new Text2('WIN!', {
size: 200,
fill: 0x00FF00
});
rightWinText.anchor.set(0.5, 0.5);
rightWinText.x = 2048 / 4 * 3;
rightWinText.y = 2732 / 2;
rightWinText.visible = false;
LK.gui.center.addChild(rightWinText);
// Touch controls
game.down = function (x, y, obj) {
if (x < 2048 / 2) {
// Left side - control left paddle
dragTarget = leftPaddle;
} else {
// Right side - control right paddle
dragTarget = rightPaddle;
}
if (dragTarget) {
dragTarget.targetY = y;
}
};
game.move = function (x, y, obj) {
if (dragTarget) {
dragTarget.targetY = y;
}
};
game.up = function (x, y, obj) {
dragTarget = null;
};
// Main game loop
game.update = function () {
// Check for winning condition
if (leftScore >= 11 || rightScore >= 11) {
if (Math.abs(leftScore - rightScore) >= 2) {
// Show which player won
if (leftScore > rightScore) {
leftWinText.visible = true;
LK.effects.flashObject(leftWinText, 0xFFFF00, 1000);
} else {
rightWinText.visible = true;
LK.effects.flashObject(rightWinText, 0xFFFF00, 1000);
}
// Delay showing game over to let players see the win text
LK.setTimeout(function () {
LK.showYouWin();
}, 1500);
}
}
};