/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var Ball = Container.expand(function () {
var self = Container.call(this);
self.velocityX = 24 * 2.5;
self.velocityY = 12 * 2.5;
self.speed = 60 * 2.5;
var ballGraphics = self.attachAsset('ball', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
self.x += self.velocityX;
self.y += self.velocityY;
// Bounce off top and bottom walls
if (self.y <= 30 || self.y >= FIELD_HEIGHT - 30) {
self.velocityY = -self.velocityY;
LK.getSound('bounce').play();
}
// Check goal boundaries
if (self.x <= 30) {
// Player 2 scores
player2Score++;
updateUI();
checkGameState();
if (!isGameActive) return;
resetBall();
LK.getSound('goal').play();
} else if (self.x >= FIELD_WIDTH - 30) {
// Player 1 scores
player1Score++;
updateUI();
checkGameState();
if (!isGameActive) return;
resetBall();
LK.getSound('goal').play();
}
};
return self;
});
var Paddle = Container.expand(function () {
var self = Container.call(this);
var paddleGraphics = self.attachAsset('paddle', {
anchorX: 0.5,
anchorY: 0.5
});
self.targetY = self.y;
self.update = function () {
// Smooth movement towards target
var diff = self.targetY - self.y;
self.y += diff * 0.1;
// Keep paddle within field bounds
if (self.y < 150) self.y = 150;
if (self.y > FIELD_HEIGHT - 150) self.y = FIELD_HEIGHT - 150;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x006400
});
/****
* Game Code
****/
var FIELD_WIDTH = 2048;
var FIELD_HEIGHT = 1366;
var PADDLE_SPEED = 8;
var GOAL_WIDTH = 40;
var GOAL_HEIGHT = 300;
// Game state
var player1Score = 0;
var player2Score = 0;
var isGameActive = true;
var WINNING_SCORE = 5;
// Create field background
var field = game.addChild(LK.getAsset('field', {
anchorX: 0.5,
anchorY: 0.5
}));
field.x = FIELD_WIDTH / 2;
field.y = FIELD_HEIGHT / 2;
// Goals removed - only invisible boundaries remain for scoring
// Create paddles
var leftPaddle = game.addChild(new Paddle());
leftPaddle.x = 200;
leftPaddle.y = FIELD_HEIGHT / 2;
var rightPaddle = game.addChild(new Paddle());
rightPaddle.x = FIELD_WIDTH - 200;
rightPaddle.y = FIELD_HEIGHT / 2;
// Create ball
var ball = game.addChild(new Ball());
// UI Elements
var player1ScoreText = new Text2('Player 1: 0', {
size: 80,
fill: 0xFFFFFF
});
player1ScoreText.anchor.set(0, 0);
player1ScoreText.x = 20;
player1ScoreText.y = 20;
LK.gui.topLeft.addChild(player1ScoreText);
var player2ScoreText = new Text2('Player 2: 0', {
size: 80,
fill: 0xFFFFFF
});
player2ScoreText.anchor.set(1, 0);
player2ScoreText.x = -20;
player2ScoreText.y = 20;
LK.gui.topRight.addChild(player2ScoreText);
function resetBall() {
ball.x = FIELD_WIDTH / 2;
ball.y = FIELD_HEIGHT / 2;
ball.velocityX = (Math.random() > 0.5 ? 12 : -12) * 2.5;
ball.velocityY = (Math.random() - 0.5) * 12 * 2.5;
updateUI();
checkGameState();
}
function updateUI() {
player1ScoreText.setText('Player 1: ' + player1Score);
player2ScoreText.setText('Player 2: ' + player2Score);
}
function checkGameState() {
if (player1Score >= WINNING_SCORE || player2Score >= WINNING_SCORE) {
isGameActive = false;
if (player1Score >= WINNING_SCORE) {
showWinner("Player 1");
} else if (player2Score >= WINNING_SCORE) {
showWinner("Player 2");
}
}
}
function showWinner(playerName) {
var winnerText = new Text2(playerName + ' Kazandı!', {
size: 150,
fill: 0xFFD700
});
winnerText.anchor.set(0.5, 0.5);
winnerText.x = FIELD_WIDTH / 2;
winnerText.y = FIELD_HEIGHT / 2;
game.addChild(winnerText);
// Flash effect for winner text
tween(winnerText, {
alpha: 0.3
}, {
duration: 500
});
// Show game over after delay
LK.setTimeout(function () {
LK.showGameOver();
}, 3000);
}
// Touch controls
game.down = function (x, y, obj) {
if (!isGameActive) return;
// Left half controls left paddle
if (x < FIELD_WIDTH / 2) {
leftPaddle.targetY = y;
} else {
// Right half controls right paddle
rightPaddle.targetY = y;
}
};
game.move = function (x, y, obj) {
if (!isGameActive) return;
// Use direct coordinates - LK engine already provides game-relative coordinates
// Left half controls left paddle
if (x < FIELD_WIDTH / 2) {
leftPaddle.targetY = y;
} else {
// Right half controls right paddle
rightPaddle.targetY = y;
}
};
game.update = function () {
if (!isGameActive) return;
// Check ball-paddle collisions
if (ball.intersects(leftPaddle)) {
if (ball.velocityX < 0) {
ball.velocityX = -ball.velocityX;
var relativeIntersectY = ball.y - leftPaddle.y;
ball.velocityY = relativeIntersectY * 0.1;
LK.getSound('bounce').play();
}
}
if (ball.intersects(rightPaddle)) {
if (ball.velocityX > 0) {
ball.velocityX = -ball.velocityX;
var relativeIntersectY = ball.y - rightPaddle.y;
ball.velocityY = relativeIntersectY * 0.1;
LK.getSound('bounce').play();
}
}
};
// Initialize game
resetBall();
updateUI(); /****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var Ball = Container.expand(function () {
var self = Container.call(this);
self.velocityX = 24 * 2.5;
self.velocityY = 12 * 2.5;
self.speed = 60 * 2.5;
var ballGraphics = self.attachAsset('ball', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
self.x += self.velocityX;
self.y += self.velocityY;
// Bounce off top and bottom walls
if (self.y <= 30 || self.y >= FIELD_HEIGHT - 30) {
self.velocityY = -self.velocityY;
LK.getSound('bounce').play();
}
// Check goal boundaries
if (self.x <= 30) {
// Player 2 scores
player2Score++;
updateUI();
checkGameState();
if (!isGameActive) return;
resetBall();
LK.getSound('goal').play();
} else if (self.x >= FIELD_WIDTH - 30) {
// Player 1 scores
player1Score++;
updateUI();
checkGameState();
if (!isGameActive) return;
resetBall();
LK.getSound('goal').play();
}
};
return self;
});
var Paddle = Container.expand(function () {
var self = Container.call(this);
var paddleGraphics = self.attachAsset('paddle', {
anchorX: 0.5,
anchorY: 0.5
});
self.targetY = self.y;
self.update = function () {
// Smooth movement towards target
var diff = self.targetY - self.y;
self.y += diff * 0.1;
// Keep paddle within field bounds
if (self.y < 150) self.y = 150;
if (self.y > FIELD_HEIGHT - 150) self.y = FIELD_HEIGHT - 150;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x006400
});
/****
* Game Code
****/
var FIELD_WIDTH = 2048;
var FIELD_HEIGHT = 1366;
var PADDLE_SPEED = 8;
var GOAL_WIDTH = 40;
var GOAL_HEIGHT = 300;
// Game state
var player1Score = 0;
var player2Score = 0;
var isGameActive = true;
var WINNING_SCORE = 5;
// Create field background
var field = game.addChild(LK.getAsset('field', {
anchorX: 0.5,
anchorY: 0.5
}));
field.x = FIELD_WIDTH / 2;
field.y = FIELD_HEIGHT / 2;
// Goals removed - only invisible boundaries remain for scoring
// Create paddles
var leftPaddle = game.addChild(new Paddle());
leftPaddle.x = 200;
leftPaddle.y = FIELD_HEIGHT / 2;
var rightPaddle = game.addChild(new Paddle());
rightPaddle.x = FIELD_WIDTH - 200;
rightPaddle.y = FIELD_HEIGHT / 2;
// Create ball
var ball = game.addChild(new Ball());
// UI Elements
var player1ScoreText = new Text2('Player 1: 0', {
size: 80,
fill: 0xFFFFFF
});
player1ScoreText.anchor.set(0, 0);
player1ScoreText.x = 20;
player1ScoreText.y = 20;
LK.gui.topLeft.addChild(player1ScoreText);
var player2ScoreText = new Text2('Player 2: 0', {
size: 80,
fill: 0xFFFFFF
});
player2ScoreText.anchor.set(1, 0);
player2ScoreText.x = -20;
player2ScoreText.y = 20;
LK.gui.topRight.addChild(player2ScoreText);
function resetBall() {
ball.x = FIELD_WIDTH / 2;
ball.y = FIELD_HEIGHT / 2;
ball.velocityX = (Math.random() > 0.5 ? 12 : -12) * 2.5;
ball.velocityY = (Math.random() - 0.5) * 12 * 2.5;
updateUI();
checkGameState();
}
function updateUI() {
player1ScoreText.setText('Player 1: ' + player1Score);
player2ScoreText.setText('Player 2: ' + player2Score);
}
function checkGameState() {
if (player1Score >= WINNING_SCORE || player2Score >= WINNING_SCORE) {
isGameActive = false;
if (player1Score >= WINNING_SCORE) {
showWinner("Player 1");
} else if (player2Score >= WINNING_SCORE) {
showWinner("Player 2");
}
}
}
function showWinner(playerName) {
var winnerText = new Text2(playerName + ' Kazandı!', {
size: 150,
fill: 0xFFD700
});
winnerText.anchor.set(0.5, 0.5);
winnerText.x = FIELD_WIDTH / 2;
winnerText.y = FIELD_HEIGHT / 2;
game.addChild(winnerText);
// Flash effect for winner text
tween(winnerText, {
alpha: 0.3
}, {
duration: 500
});
// Show game over after delay
LK.setTimeout(function () {
LK.showGameOver();
}, 3000);
}
// Touch controls
game.down = function (x, y, obj) {
if (!isGameActive) return;
// Left half controls left paddle
if (x < FIELD_WIDTH / 2) {
leftPaddle.targetY = y;
} else {
// Right half controls right paddle
rightPaddle.targetY = y;
}
};
game.move = function (x, y, obj) {
if (!isGameActive) return;
// Use direct coordinates - LK engine already provides game-relative coordinates
// Left half controls left paddle
if (x < FIELD_WIDTH / 2) {
leftPaddle.targetY = y;
} else {
// Right half controls right paddle
rightPaddle.targetY = y;
}
};
game.update = function () {
if (!isGameActive) return;
// Check ball-paddle collisions
if (ball.intersects(leftPaddle)) {
if (ball.velocityX < 0) {
ball.velocityX = -ball.velocityX;
var relativeIntersectY = ball.y - leftPaddle.y;
ball.velocityY = relativeIntersectY * 0.1;
LK.getSound('bounce').play();
}
}
if (ball.intersects(rightPaddle)) {
if (ball.velocityX > 0) {
ball.velocityX = -ball.velocityX;
var relativeIntersectY = ball.y - rightPaddle.y;
ball.velocityY = relativeIntersectY * 0.1;
LK.getSound('bounce').play();
}
}
};
// Initialize game
resetBall();
updateUI();