/****
* Classes
****/
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Ball class representing the soccer ball
var Ball = Container.expand(function () {
var self = Container.call(this);
var ballGraphics = self.attachAsset('ball', {
anchorX: 0.5,
anchorY: 0.5
});
self.speedX = 0;
self.speedY = 0;
self.update = function () {
self.x += self.speedX;
self.y += self.speedY;
};
});
// Goalkeeper class representing the goalkeeper
var Goalkeeper = Container.expand(function () {
var self = Container.call(this);
var goalkeeperGraphics = self.attachAsset('goalkeeper', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
// Simple AI to move the goalkeeper left and right
self.x += Math.sin(LK.ticks / 30) * 5;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x008000 // Init game with green background
});
/****
* Game Code
****/
var ball = game.addChild(new Ball());
ball.x = 2048 / 2;
ball.y = 2732 - 200;
var goalkeeper = game.addChild(new Goalkeeper());
goalkeeper.x = 2048 / 2;
goalkeeper.y = 200;
var score = 0;
var scoreTxt = new Text2('Score: 0', {
size: 100,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
game.down = function (x, y, obj) {
var angle = Math.atan2(y - ball.y, x - ball.x);
ball.speedX = Math.cos(angle) * 10;
ball.speedY = Math.sin(angle) * 10;
};
game.update = function () {
ball.update();
goalkeeper.update();
// Check if the ball is in the goal area
if (ball.y < 100 && Math.abs(ball.x - goalkeeper.x) > 100) {
score++;
scoreTxt.setText('Score: ' + score);
resetBall();
}
// Reset ball if it goes out of bounds
if (ball.y < 0 || ball.y > 2732 || ball.x < 0 || ball.x > 2048) {
resetBall();
}
};
function resetBall() {
ball.x = 2048 / 2;
ball.y = 2732 - 200;
ball.speedX = 0;
ball.speedY = 0;
} /****
* Classes
****/
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Ball class representing the soccer ball
var Ball = Container.expand(function () {
var self = Container.call(this);
var ballGraphics = self.attachAsset('ball', {
anchorX: 0.5,
anchorY: 0.5
});
self.speedX = 0;
self.speedY = 0;
self.update = function () {
self.x += self.speedX;
self.y += self.speedY;
};
});
// Goalkeeper class representing the goalkeeper
var Goalkeeper = Container.expand(function () {
var self = Container.call(this);
var goalkeeperGraphics = self.attachAsset('goalkeeper', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
// Simple AI to move the goalkeeper left and right
self.x += Math.sin(LK.ticks / 30) * 5;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x008000 // Init game with green background
});
/****
* Game Code
****/
var ball = game.addChild(new Ball());
ball.x = 2048 / 2;
ball.y = 2732 - 200;
var goalkeeper = game.addChild(new Goalkeeper());
goalkeeper.x = 2048 / 2;
goalkeeper.y = 200;
var score = 0;
var scoreTxt = new Text2('Score: 0', {
size: 100,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
game.down = function (x, y, obj) {
var angle = Math.atan2(y - ball.y, x - ball.x);
ball.speedX = Math.cos(angle) * 10;
ball.speedY = Math.sin(angle) * 10;
};
game.update = function () {
ball.update();
goalkeeper.update();
// Check if the ball is in the goal area
if (ball.y < 100 && Math.abs(ball.x - goalkeeper.x) > 100) {
score++;
scoreTxt.setText('Score: ' + score);
resetBall();
}
// Reset ball if it goes out of bounds
if (ball.y < 0 || ball.y > 2732 || ball.x < 0 || ball.x > 2048) {
resetBall();
}
};
function resetBall() {
ball.x = 2048 / 2;
ball.y = 2732 - 200;
ball.speedX = 0;
ball.speedY = 0;
}