var Ball = Container.expand(function () { var self = Container.call(this); var ballGraphics = self.createAsset('ball', 'Soccer Ball', .5, .5); self.speed = { x: 10, y: 10 }; self.move = function () { self.x += self.speed.x; self.y += self.speed.y; }; self.bounce = function (axis) { self.speed[axis] *= -1; }; }); var Paddle = Container.expand(function () { var self = Container.call(this); var paddleGraphics = self.createAsset('paddle', 'Player Paddle', .5, .5); self.speed = 10; self.move = function (direction) { self.y += direction * self.speed; }; }); var Game = Container.expand(function () { var self = Container.call(this); var background = self.createAsset('background', 'Game Background', 0, 0); background.width = 2048; background.height = 2732; var ball = self.addChild(new Ball()); ball.x = 2048 / 2; ball.y = 2732 / 2; var playerPaddle = self.addChild(new Paddle()); playerPaddle.x = 100; playerPaddle.y = 2732 / 2; var aiPaddle = self.addChild(new Paddle()); aiPaddle.x = 2048 - 100; aiPaddle.y = 2732 / 2; var scoreTxt = new Text2('0 - 0', { size: 150, fill: "#ffffff" }); scoreTxt.anchor.set(.5, 0); LK.gui.topCenter.addChild(scoreTxt); var playerScore = 0; var aiScore = 0; function updateScore() { scoreTxt.setText(playerScore + ' - ' + aiScore); } LK.on('tick', function () { ball.move(); if (ball.x <= 0) { if (aiScore < 5) { aiScore++; updateScore(); if (aiScore == 5) { LK.showGameOver(); } } else { LK.showGameOver(); } ball.x = 2048 / 2; ball.y = 2732 / 2; } if (ball.x >= 2048) { if (playerScore < 5) { playerScore++; updateScore(); if (playerScore == 5) { LK.showGameOver(); } } else { LK.showGameOver(); } ball.x = 2048 / 2; ball.y = 2732 / 2; } if (ball.y <= 0 || ball.y >= 2732) { ball.bounce('y'); } if (ball.intersects(playerPaddle) || ball.intersects(aiPaddle)) { ball.bounce('x'); } var diff = ball.y - aiPaddle.y; if (diff > 0) { aiPaddle.move(1); } else { aiPaddle.move(-1); } }); stage.on('move', function (obj) { var event = obj.event; var pos = event.getLocalPosition(self); playerPaddle.y = pos.y; }); });
var Ball = Container.expand(function () {
var self = Container.call(this);
var ballGraphics = self.createAsset('ball', 'Soccer Ball', .5, .5);
self.speed = {
x: 10,
y: 10
};
self.move = function () {
self.x += self.speed.x;
self.y += self.speed.y;
};
self.bounce = function (axis) {
self.speed[axis] *= -1;
};
});
var Paddle = Container.expand(function () {
var self = Container.call(this);
var paddleGraphics = self.createAsset('paddle', 'Player Paddle', .5, .5);
self.speed = 10;
self.move = function (direction) {
self.y += direction * self.speed;
};
});
var Game = Container.expand(function () {
var self = Container.call(this);
var background = self.createAsset('background', 'Game Background', 0, 0);
background.width = 2048;
background.height = 2732;
var ball = self.addChild(new Ball());
ball.x = 2048 / 2;
ball.y = 2732 / 2;
var playerPaddle = self.addChild(new Paddle());
playerPaddle.x = 100;
playerPaddle.y = 2732 / 2;
var aiPaddle = self.addChild(new Paddle());
aiPaddle.x = 2048 - 100;
aiPaddle.y = 2732 / 2;
var scoreTxt = new Text2('0 - 0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(.5, 0);
LK.gui.topCenter.addChild(scoreTxt);
var playerScore = 0;
var aiScore = 0;
function updateScore() {
scoreTxt.setText(playerScore + ' - ' + aiScore);
}
LK.on('tick', function () {
ball.move();
if (ball.x <= 0) {
if (aiScore < 5) {
aiScore++;
updateScore();
if (aiScore == 5) {
LK.showGameOver();
}
} else {
LK.showGameOver();
}
ball.x = 2048 / 2;
ball.y = 2732 / 2;
}
if (ball.x >= 2048) {
if (playerScore < 5) {
playerScore++;
updateScore();
if (playerScore == 5) {
LK.showGameOver();
}
} else {
LK.showGameOver();
}
ball.x = 2048 / 2;
ball.y = 2732 / 2;
}
if (ball.y <= 0 || ball.y >= 2732) {
ball.bounce('y');
}
if (ball.intersects(playerPaddle) || ball.intersects(aiPaddle)) {
ball.bounce('x');
}
var diff = ball.y - aiPaddle.y;
if (diff > 0) {
aiPaddle.move(1);
} else {
aiPaddle.move(-1);
}
});
stage.on('move', function (obj) {
var event = obj.event;
var pos = event.getLocalPosition(self);
playerPaddle.y = pos.y;
});
});