/****
* Classes
****/
//<Assets used in the game will automatically appear here>
// 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;
if (self.x <= 0 || self.x >= 2048) {
self.speedX *= -1;
}
if (self.y <= 0 || self.y >= 2732) {
self.speedY *= -1;
}
};
});
// Paddle class
var Paddle = Container.expand(function () {
var self = Container.call(this);
var paddleGraphics = self.attachAsset('paddle', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
// Paddle movement logic will be handled in the game move event
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
var background = game.attachAsset('background', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
y: 1366
});
var ball = new Ball();
ball.x = 1024;
ball.y = 1366;
game.addChild(ball);
var playerPaddle = new Paddle();
playerPaddle.x = 1024;
playerPaddle.y = 2500;
game.addChild(playerPaddle);
var aiPaddle = new Paddle();
aiPaddle.x = 1024;
aiPaddle.y = 200;
game.addChild(aiPaddle);
var scoreTxt = new Text2('0 - 0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
var playerScore = 0;
var aiScore = 0;
function updateScore() {
scoreTxt.setText("".concat(playerScore, " - ").concat(aiScore));
}
game.move = function (x, y, obj) {
playerPaddle.x = x;
};
game.update = function () {
ball.update();
playerPaddle.update();
aiPaddle.update();
// AI paddle movement
if (ball.x > aiPaddle.x) {
aiPaddle.x += 5;
} else {
aiPaddle.x -= 5;
}
// Ball collision with paddles
if (ball.intersects(playerPaddle) || ball.intersects(aiPaddle)) {
ball.speedY *= -1;
// Play MusicBG1 when tennis ball hits the racket
LK.getSound('MusicBG1').play();
}
// Scoring
if (ball.y <= 0) {
playerScore++;
updateScore();
ball.x = 1024;
ball.y = 1366;
} else if (ball.y >= 2732) {
aiScore++;
updateScore();
ball.x = 1024;
ball.y = 1366;
}
};
Tennis Racket. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
Tennis Ball. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
Tennis Court Background. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows. Up view