/**** * 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 }); }); // Player class representing the football player var Player = Container.expand(function () { var self = Container.call(this); var playerGraphics = self.attachAsset('player', { 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 player = game.addChild(new Player()); player.x = 2048 / 2; player.y = 2732 - 300; 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
@@ -44,8 +44,16 @@
anchorX: 0.5,
anchorY: 0.5
});
});
+// Player class representing the football player
+var Player = Container.expand(function () {
+ var self = Container.call(this);
+ var playerGraphics = self.attachAsset('player', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+});
/****
* Initialize Game
****/
@@ -58,8 +66,11 @@
****/
var ball = game.addChild(new Ball());
ball.x = 2048 / 2;
ball.y = 2732 - 200;
+var player = game.addChild(new Player());
+player.x = 2048 / 2;
+player.y = 2732 - 300;
var goalkeeper = game.addChild(new Goalkeeper());
goalkeeper.x = 2048 / 2;
goalkeeper.y = 200;
var goal = game.addChild(new Goal());