/**** * 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; }; }); // Goal class representing the goal var Goal = Container.expand(function () { var self = Container.call(this); var goalGraphics = self.attachAsset('goal', { anchorX: 0.5, anchorY: 0.5 }); }); // 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; }; }); // PenaltyArea class representing the penalty area var PenaltyArea = Container.expand(function () { var self = Container.call(this); var penaltyAreaGraphics = self.attachAsset('penaltyArea', { anchorX: 0.5, anchorY: 0.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 goal = game.addChild(new Goal()); goal.x = 2048 / 2; goal.y = 100; var penaltyArea = game.addChild(new PenaltyArea()); penaltyArea.x = 2048 / 2; penaltyArea.y = 400; 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; }
===================================================================
--- original.js
+++ change.js
@@ -1,80 +1,102 @@
-/****
+/****
* 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;
- };
+ 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;
+ };
});
+// Goal class representing the goal
+var Goal = Container.expand(function () {
+ var self = Container.call(this);
+ var goalGraphics = self.attachAsset('goal', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+});
// 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;
- };
+ 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;
+ };
});
+// PenaltyArea class representing the penalty area
+var PenaltyArea = Container.expand(function () {
+ var self = Container.call(this);
+ var penaltyAreaGraphics = self.attachAsset('penaltyArea', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+});
-/****
+/****
* Initialize Game
-****/
+****/
var game = new LK.Game({
- backgroundColor: 0x008000 // Init game with green background
+ 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 goal = game.addChild(new Goal());
+goal.x = 2048 / 2;
+goal.y = 100;
+var penaltyArea = game.addChild(new PenaltyArea());
+penaltyArea.x = 2048 / 2;
+penaltyArea.y = 400;
var score = 0;
var scoreTxt = new Text2('Score: 0', {
- size: 100,
- fill: 0xFFFFFF
+ 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;
+ 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();
- }
+ 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;
+ ball.x = 2048 / 2;
+ ball.y = 2732 - 200;
+ ball.speedX = 0;
+ ball.speedY = 0;
}
\ No newline at end of file