/****
* Classes
****/
// Assets will be automatically created and loaded by the LK engine based on their usage in the code.
// Define a Ball class for 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;
// Bounce off the walls
if (self.x < 0 || self.x > 2048) {
self.speedX *= -1;
}
if (self.y < 0 || self.y > 2732) {
self.speedY *= -1;
}
};
});
// Define a Goal class for the goal area
var Goal = Container.expand(function () {
var self = Container.call(this);
var goalGraphics = self.attachAsset('goal', {
anchorX: 0.5,
anchorY: 0.5
});
});
// Define a Player class for the soccer player
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
self.down = function (x, y, obj) {
var game_position = game.toLocal(obj.global);
self.x = game_position.x;
self.y = game_position.y;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x008000 // Init game with green background to represent a soccer field
});
/****
* Game Code
****/
// Initialize game elements
var player = game.addChild(new Player());
player.x = 1024; // Center horizontally
player.y = 2400; // Near the bottom
var ball = game.addChild(new Ball());
ball.x = 1024; // Center horizontally
ball.y = 1366; // Center vertically
var goal = game.addChild(new Goal());
goal.x = 1024; // Center horizontally
goal.y = 100; // Near the top
// Score display
var score = 0;
var scoreTxt = new Text2('Score: 0', {
size: 100,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Game logic
game.update = function () {
// Update ball position
ball.update();
// Check for goal
if (ball.intersects(goal)) {
score += 1;
scoreTxt.setText('Score: ' + score);
// Reset ball position
ball.x = 1024;
ball.y = 1366;
ball.speedX = 0;
ball.speedY = 0;
// Reset the number of kicks
kicks = 0;
} else if (kicks >= 3) {
// End the game if the player has used all their kicks without scoring
LK.showGameOver();
}
};
// Handle player movement
// Initialize a variable to keep track of the number of kicks
var kicks = 0;
game.down = function (x, y, obj) {
player.down(x, y, obj);
// Kick the ball if close enough and the player has kicks remaining
if (player.intersects(ball) && kicks < 3) {
ball.speedX = (ball.x - player.x) * 0.1;
ball.speedY = (ball.y - player.y) * 0.1;
kicks += 1;
}
}; ===================================================================
--- original.js
+++ change.js
@@ -85,8 +85,13 @@
ball.x = 1024;
ball.y = 1366;
ball.speedX = 0;
ball.speedY = 0;
+ // Reset the number of kicks
+ kicks = 0;
+ } else if (kicks >= 3) {
+ // End the game if the player has used all their kicks without scoring
+ LK.showGameOver();
}
};
// Handle player movement
// Initialize a variable to keep track of the number of kicks
soccer ball,Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
soccer goal, Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
single soccer boot, Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.