/****
* 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.speed = {
x: 0,
y: 0
};
self.update = function () {
self.x += self.speed.x;
self.y += self.speed.y;
if (self.x < 0 || self.x > 2048) self.speed.x *= -1;
if (self.y < 0 || self.y > 2732) self.speed.y *= -1;
};
});
// Target class
var Target = Container.expand(function () {
var self = Container.call(this);
var targetGraphics = self.attachAsset('target', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
// Target specific update logic if needed
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize variables
var ball;
var target;
var score = 0;
var scoreTxt;
// Initialize game elements
function initGame() {
// Create ball
ball = new Ball();
ball.x = 1024;
ball.y = 1366;
game.addChild(ball);
// Create target
target = new Target();
target.x = Math.random() * 2048;
target.y = Math.random() * 2732;
game.addChild(target);
// Create score text
scoreTxt = new Text2('Score: 0', {
size: 100,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
}
// Update score
function updateScore() {
score += 1;
scoreTxt.setText('Score: ' + score);
}
// Handle touch down event
game.down = function (x, y, obj) {
var localPos = game.toLocal(obj.global);
var angle = Math.atan2(localPos.y - ball.y, localPos.x - ball.x);
ball.speed.x = Math.cos(angle) * 10;
ball.speed.y = Math.sin(angle) * 10;
};
// Game update loop
game.update = function () {
ball.update();
if (ball.intersects(target)) {
updateScore();
target.x = Math.random() * 2048;
target.y = Math.random() * 2732;
}
};
// Initialize game elements
initGame();