/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); /**** * Classes ****/ var Ball = Container.expand(function () { var self = Container.call(this); var ballGraphics = self.attachAsset('ball', { anchorX: 0.5, anchorY: 0.5 }); self.velocityX = 0; self.velocityY = 0; self.friction = 0.95; self.isMoving = false; self.update = function () { if (self.isMoving) { self.x += self.velocityX; self.y += self.velocityY; self.velocityX *= self.friction; self.velocityY *= self.friction; // Stop ball if velocity is very low if (Math.abs(self.velocityX) < 0.5 && Math.abs(self.velocityY) < 0.5) { self.velocityX = 0; self.velocityY = 0; self.isMoving = false; } // Field boundaries if (self.x < 50) { self.x = 50; self.velocityX = -self.velocityX * 0.8; } if (self.x > 1998) { self.x = 1998; self.velocityX = -self.velocityX * 0.8; } if (self.y < 300) { self.y = 300; self.velocityY = -self.velocityY * 0.8; } if (self.y > 1800) { self.y = 1800; self.velocityY = -self.velocityY * 0.8; } } }; self.shoot = function (targetX, targetY) { var dx = targetX - self.x; var dy = targetY - self.y; var distance = Math.sqrt(dx * dx + dy * dy); self.velocityX = dx / distance * 15; self.velocityY = dy / distance * 15; self.isMoving = true; LK.getSound('kick').play(); }; return self; }); var Defender = Container.expand(function () { var self = Container.call(this); var defenderGraphics = self.attachAsset('defender', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 3; self.targetX = 0; self.targetY = 0; self.update = function () { // Move towards target var dx = self.targetX - self.x; var dy = self.targetY - self.y; var distance = Math.sqrt(dx * dx + dy * dy); if (distance > 5) { self.x += dx / distance * self.speed; self.y += dy / distance * self.speed; } }; return self; }); var Goalkeeper = Container.expand(function () { var self = Container.call(this); var goalkeeperGraphics = self.attachAsset('goalkeeper', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 4; self.direction = 1; self.centerX = 0; self.baseY = 0; // Will be set when goalkeeper is positioned self.isMovingForward = false; self.lastPlayerDistance = 1000; self.update = function () { // Horizontal movement between goal posts self.x += self.direction * self.speed; // Bounce between goal posts if (self.x < self.centerX - 150 || self.x > self.centerX + 150) { self.direction *= -1; } // Check distance to player for forward movement if (typeof player !== 'undefined' && player) { var distanceToPlayer = Math.sqrt((player.x - self.x) * (player.x - self.x) + (player.y - self.y) * (player.y - self.y)); // If player comes within 300 pixels, move forward if (distanceToPlayer < 300 && !self.isMovingForward && self.lastPlayerDistance >= 300) { self.isMovingForward = true; // Move forward by 100 pixels from goal line var targetY = self.baseY + (self.baseY < 1000 ? 100 : -100); tween(self, { y: targetY }, { duration: 500, easing: tween.easeOut }); } // If player moves away beyond 400 pixels, return to goal line else if (distanceToPlayer > 400 && self.isMovingForward && self.lastPlayerDistance <= 400) { self.isMovingForward = false; // Return to goal line tween(self, { y: self.baseY }, { duration: 800, easing: tween.easeInOut }); } self.lastPlayerDistance = distanceToPlayer; } }; return self; }); var Player = Container.expand(function () { var self = Container.call(this); var playerGraphics = self.attachAsset('player', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 8; self.hasBall = false; self.setColor = function (color) { playerGraphics.tint = color; }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x228b22 }); /**** * Game Code ****/ // Menu system var menuActive = true; var gameStarted = false; // Menu grass background var menuGrassBackground = game.attachAsset('grass', { x: 0, y: 0, anchorX: 0, anchorY: 0 }); // Welcome text var welcomeTxt = new Text2('Welcome', { size: 80, fill: 0xFFFFFF }); welcomeTxt.anchor.set(0.5, 0.5); welcomeTxt.x = 1024; welcomeTxt.y = 500; game.addChild(welcomeTxt); // Menu title var titleTxt = new Text2('FFT26', { size: 120, fill: 0xFFFFFF }); titleTxt.anchor.set(0.5, 0.5); titleTxt.x = 1024; titleTxt.y = 700; game.addChild(titleTxt); // Demo text var demoTxt = new Text2('Demo', { size: 80, fill: 0xFFFFFF }); demoTxt.anchor.set(0.5, 0.5); demoTxt.x = 1024; demoTxt.y = 850; game.addChild(demoTxt); // Play button var playButton = new Text2('PLAY', { size: 100, fill: 0x00FF00 }); playButton.anchor.set(0.5, 0.5); playButton.x = 1024; playButton.y = 1200; game.addChild(playButton); // Exit button var exitButton = new Text2('EXIT', { size: 100, fill: 0xFF0000 }); exitButton.anchor.set(0.5, 0.5); exitButton.x = 1024; exitButton.y = 1400; game.addChild(exitButton); // Function to start the game function startGame() { gameStarted = true; // Hide menu elements welcomeTxt.visible = false; titleTxt.visible = false; demoTxt.visible = false; playButton.visible = false; exitButton.visible = false; // Show game elements initializeGameElements(); } // Function to initialize game elements function initializeGameElements() { // Grass background var grassBackground = game.attachAsset('grass', { x: 0, y: 0, anchorX: 0, anchorY: 0 }); // Game field setup var fieldBackground = game.attachAsset('field', { x: 0, y: 400, anchorX: 0, anchorY: 0 }); // Goal setup - Top goal goalArea = game.attachAsset('goal', { x: 1024, y: 300, anchorX: 0.5, anchorY: 0 }); goalArea.alpha = 0.3; leftGoalPost = game.attachAsset('goalpost', { x: 824, y: 300, anchorX: 0.5, anchorY: 0 }); rightGoalPost = game.attachAsset('goalpost', { x: 1224, y: 300, anchorX: 0.5, anchorY: 0 }); // Second goal setup - Bottom goal goalArea2 = game.attachAsset('goal', { x: 1024, y: 1700, anchorX: 0.5, anchorY: 0 }); goalArea2.alpha = 0.3; leftGoalPost2 = game.attachAsset('goalpost', { x: 824, y: 1700, anchorX: 0.5, anchorY: 0 }); rightGoalPost2 = game.attachAsset('goalpost', { x: 1224, y: 1700, anchorX: 0.5, anchorY: 0 }); // Center line var centerLine = game.attachAsset('centerLine', { x: 0, y: 1048, anchorX: 0, anchorY: 0.5 }); // Center circle var centerCircle = game.attachAsset('centerCircle', { x: 1024, y: 1050, anchorX: 0.5, anchorY: 0.5 }); centerCircle.alpha = 0.8; // Score display scoreTxt = new Text2('0-0', { size: 80, fill: 0xFFFFFF }); scoreTxt.anchor.set(0.5, 0); scoreTxt.y = 50; // Position score text below the top margin LK.gui.top.addChild(scoreTxt); // Timer display timerTxt = new Text2('Time: 90', { size: 60, fill: 0xFFFFFF }); timerTxt.anchor.set(1, 0); LK.gui.topRight.addChild(timerTxt); // Game entities player = game.addChild(new Player()); player.x = 1024; player.y = 1500; // Player color is now applied in constructor ball = game.addChild(new Ball()); ball.x = 1024; ball.y = 1050; goalkeeper = game.addChild(new Goalkeeper()); goalkeeper.x = 1024; goalkeeper.y = 350; goalkeeper.centerX = 1024; goalkeeper.baseY = 350; // Set base position for goal line // Second goalkeeper for bottom goal goalkeeper2 = game.addChild(new Goalkeeper()); goalkeeper2.x = 1024; goalkeeper2.y = 1750; goalkeeper2.centerX = 1024; goalkeeper2.baseY = 1750; // Set base position for goal line // Defenders defenders = []; for (var i = 0; i < 3; i++) { var defender = game.addChild(new Defender()); defender.x = 600 + i * 300; defender.y = 800 + i * 150; defenders.push(defender); } // Game variables gameTime = 90; goals = 0; opponentGoals = 0; gameActive = true; dragNode = null; lastGameTime = 90; // Game timer gameTimer = LK.setInterval(function () { if (gameActive) { gameTime--; timerTxt.setText('Time: ' + gameTime); if (gameTime <= 0) { gameActive = false; LK.showGameOver(); } } }, 1000); } // Function to increase difficulty after each goal function increaseDifficulty() { // Increase goalkeeper speed goalkeeper.speed += 0.5; goalkeeper2.speed += 0.5; // Increase defender speed for (var i = 0; i < defenders.length; i++) { defenders[i].speed += 0.3; } // Add new defender every 3 goals if (goals % 3 === 0 && defenders.length < 6) { var newDefender = game.addChild(new Defender()); newDefender.x = 400 + Math.random() * 1200; newDefender.y = 600 + Math.random() * 600; defenders.push(newDefender); } } // Declare game variables globally var goalArea, leftGoalPost, rightGoalPost, goalArea2, leftGoalPost2, rightGoalPost2, scoreTxt, timerTxt; var player, ball, goalkeeper, goalkeeper2, defenders; var gameTime, goals, opponentGoals, gameActive, dragNode, lastGameTime, gameTimer; // Touch controls game.down = function (x, y, obj) { if (menuActive) { // Check if play button was clicked var distanceToPlay = Math.sqrt((x - playButton.x) * (x - playButton.x) + (y - playButton.y) * (y - playButton.y)); if (distanceToPlay < 150) { menuActive = false; startGame(); return; } // Check if exit button was clicked var distanceToExit = Math.sqrt((x - exitButton.x) * (x - exitButton.x) + (y - exitButton.y) * (y - exitButton.y)); if (distanceToExit < 150) { // Exit functionality - show game over to end the game LK.showGameOver(); return; } return; } if (!gameActive || !gameStarted) return; var distanceToPlayer = Math.sqrt((x - player.x) * (x - player.x) + (y - player.y) * (y - player.y)); var distanceToBall = Math.sqrt((x - ball.x) * (x - ball.x) + (y - ball.y) * (y - ball.y)); if (distanceToPlayer < 100) { dragNode = player; } else if (distanceToBall < 80 && player.hasBall) { // Shoot ball towards tap location ball.shoot(x, y); player.hasBall = false; } }; game.move = function (x, y, obj) { if (!gameActive || !gameStarted || menuActive) return; if (dragNode === player) { player.x = x; player.y = y; // Keep player within field bounds if (player.x < 100) player.x = 100; if (player.x > 1948) player.x = 1948; if (player.y < 400) player.y = 400; if (player.y > 1850) player.y = 1850; // Move ball with player if close enough if (player.hasBall && !ball.isMoving) { ball.x = player.x; ball.y = player.y - 50; } } }; game.up = function (x, y, obj) { if (!gameStarted || menuActive) return; dragNode = null; }; // Main game loop game.update = function () { if (!gameActive || !gameStarted || menuActive) return; // Check if player can pick up ball - player always gets ball when touching it var distanceToBall = Math.sqrt((player.x - ball.x) * (player.x - ball.x) + (player.y - ball.y) * (player.y - ball.y)); if (distanceToBall < 60 && !player.hasBall) { player.hasBall = true; ball.velocityX = 0; ball.velocityY = 0; ball.isMoving = false; } // Update defenders to chase ball for (var i = 0; i < defenders.length; i++) { var defender = defenders[i]; defender.targetX = ball.x; defender.targetY = ball.y; // Check collision with ball - defender always gets the ball when touching it var distanceToBall = Math.sqrt((defender.x - ball.x) * (defender.x - ball.x) + (defender.y - ball.y) * (defender.y - ball.y)); if (distanceToBall < 60) { // Defender always gets the ball when touching it player.hasBall = false; ball.velocityX = 0; ball.velocityY = 0; ball.isMoving = false; // Defender gets the ball and shoots towards goal var goalX = 1024; // Center of goal var goalY = 300; // Top goal // If defender is closer to bottom goal, shoot there instead if (defender.y > 1000) { goalY = 1750; // Bottom goal } ball.shoot(goalX + (Math.random() - 0.5) * 200, goalY); LK.effects.flashObject(defender, 0x00ff00, 500); } // Check collision with player - when defender touches player with ball, defender gets it var distanceToPlayer = Math.sqrt((defender.x - player.x) * (defender.x - player.x) + (defender.y - player.y) * (defender.y - player.y)); if (distanceToPlayer < 80 && player.hasBall) { player.hasBall = false; ball.x = defender.x; ball.y = defender.y; // Defender immediately shoots towards goal var goalX = 1024; var goalY = 1750; // Shoot towards bottom goal (our goal) ball.shoot(goalX + (Math.random() - 0.5) * 200, goalY); LK.effects.flashObject(player, 0xff0000, 500); } } // Check collision with goalkeeper - when player touches goalkeeper, ball goes forward var distanceToGoalkeeper = Math.sqrt((player.x - goalkeeper.x) * (player.x - goalkeeper.x) + (player.y - goalkeeper.y) * (player.y - goalkeeper.y)); if (distanceToGoalkeeper < 80) { if (!ball.isMoving) { // Make ball go forward (towards bottom goal) ball.shoot(ball.x, ball.y + 300); player.hasBall = false; } } // Check collision with second goalkeeper - when player touches goalkeeper, ball goes forward var distanceToGoalkeeper2 = Math.sqrt((player.x - goalkeeper2.x) * (player.x - goalkeeper2.x) + (player.y - goalkeeper2.y) * (player.y - goalkeeper2.y)); if (distanceToGoalkeeper2 < 80) { if (!ball.isMoving) { // Make ball go forward (towards top goal) ball.shoot(ball.x, ball.y - 300); player.hasBall = false; } } // Check goal scoring - Top goal (we score) if (ball.x > 844 && ball.x < 1204 && ball.y < 500 && ball.y > 300) { if (ball.lastY === undefined || ball.lastY > 500) { goals++; LK.setScore(goals); scoreTxt.setText(goals + '-' + opponentGoals); LK.getSound('goal').play(); LK.effects.flashScreen(0x00ff00, 1000); // Reset ball and player positions player.x = 1024; player.y = 1500; ball.x = 1024; ball.y = 1050; ball.velocityX = 0; ball.velocityY = 0; ball.isMoving = false; player.hasBall = false; // Increase difficulty after each goal increaseDifficulty(); // Check win condition if (goals >= 10) { gameActive = false; LK.showYouWin(); } } } // Check goal scoring - Bottom goal (opponent scores against us) if (ball.x > 844 && ball.x < 1204 && ball.y > 1700 && ball.y < 1900) { if (ball.lastY === undefined || ball.lastY < 1700) { opponentGoals++; scoreTxt.setText(goals + '-' + opponentGoals); LK.getSound('goal').play(); LK.effects.flashScreen(0xff0000, 1000); // Reset ball and player positions player.x = 1024; player.y = 1500; ball.x = 1024; ball.y = 1050; ball.velocityX = 0; ball.velocityY = 0; ball.isMoving = false; player.hasBall = false; // Increase difficulty after each goal increaseDifficulty(); // Check lose condition if (opponentGoals >= 10) { gameActive = false; LK.showGameOver(); } } } ball.lastY = ball.y; };
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var Ball = Container.expand(function () {
var self = Container.call(this);
var ballGraphics = self.attachAsset('ball', {
anchorX: 0.5,
anchorY: 0.5
});
self.velocityX = 0;
self.velocityY = 0;
self.friction = 0.95;
self.isMoving = false;
self.update = function () {
if (self.isMoving) {
self.x += self.velocityX;
self.y += self.velocityY;
self.velocityX *= self.friction;
self.velocityY *= self.friction;
// Stop ball if velocity is very low
if (Math.abs(self.velocityX) < 0.5 && Math.abs(self.velocityY) < 0.5) {
self.velocityX = 0;
self.velocityY = 0;
self.isMoving = false;
}
// Field boundaries
if (self.x < 50) {
self.x = 50;
self.velocityX = -self.velocityX * 0.8;
}
if (self.x > 1998) {
self.x = 1998;
self.velocityX = -self.velocityX * 0.8;
}
if (self.y < 300) {
self.y = 300;
self.velocityY = -self.velocityY * 0.8;
}
if (self.y > 1800) {
self.y = 1800;
self.velocityY = -self.velocityY * 0.8;
}
}
};
self.shoot = function (targetX, targetY) {
var dx = targetX - self.x;
var dy = targetY - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
self.velocityX = dx / distance * 15;
self.velocityY = dy / distance * 15;
self.isMoving = true;
LK.getSound('kick').play();
};
return self;
});
var Defender = Container.expand(function () {
var self = Container.call(this);
var defenderGraphics = self.attachAsset('defender', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 3;
self.targetX = 0;
self.targetY = 0;
self.update = function () {
// Move towards target
var dx = self.targetX - self.x;
var dy = self.targetY - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance > 5) {
self.x += dx / distance * self.speed;
self.y += dy / distance * self.speed;
}
};
return self;
});
var Goalkeeper = Container.expand(function () {
var self = Container.call(this);
var goalkeeperGraphics = self.attachAsset('goalkeeper', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 4;
self.direction = 1;
self.centerX = 0;
self.baseY = 0; // Will be set when goalkeeper is positioned
self.isMovingForward = false;
self.lastPlayerDistance = 1000;
self.update = function () {
// Horizontal movement between goal posts
self.x += self.direction * self.speed;
// Bounce between goal posts
if (self.x < self.centerX - 150 || self.x > self.centerX + 150) {
self.direction *= -1;
}
// Check distance to player for forward movement
if (typeof player !== 'undefined' && player) {
var distanceToPlayer = Math.sqrt((player.x - self.x) * (player.x - self.x) + (player.y - self.y) * (player.y - self.y));
// If player comes within 300 pixels, move forward
if (distanceToPlayer < 300 && !self.isMovingForward && self.lastPlayerDistance >= 300) {
self.isMovingForward = true;
// Move forward by 100 pixels from goal line
var targetY = self.baseY + (self.baseY < 1000 ? 100 : -100);
tween(self, {
y: targetY
}, {
duration: 500,
easing: tween.easeOut
});
}
// If player moves away beyond 400 pixels, return to goal line
else if (distanceToPlayer > 400 && self.isMovingForward && self.lastPlayerDistance <= 400) {
self.isMovingForward = false;
// Return to goal line
tween(self, {
y: self.baseY
}, {
duration: 800,
easing: tween.easeInOut
});
}
self.lastPlayerDistance = distanceToPlayer;
}
};
return self;
});
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 8;
self.hasBall = false;
self.setColor = function (color) {
playerGraphics.tint = color;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x228b22
});
/****
* Game Code
****/
// Menu system
var menuActive = true;
var gameStarted = false;
// Menu grass background
var menuGrassBackground = game.attachAsset('grass', {
x: 0,
y: 0,
anchorX: 0,
anchorY: 0
});
// Welcome text
var welcomeTxt = new Text2('Welcome', {
size: 80,
fill: 0xFFFFFF
});
welcomeTxt.anchor.set(0.5, 0.5);
welcomeTxt.x = 1024;
welcomeTxt.y = 500;
game.addChild(welcomeTxt);
// Menu title
var titleTxt = new Text2('FFT26', {
size: 120,
fill: 0xFFFFFF
});
titleTxt.anchor.set(0.5, 0.5);
titleTxt.x = 1024;
titleTxt.y = 700;
game.addChild(titleTxt);
// Demo text
var demoTxt = new Text2('Demo', {
size: 80,
fill: 0xFFFFFF
});
demoTxt.anchor.set(0.5, 0.5);
demoTxt.x = 1024;
demoTxt.y = 850;
game.addChild(demoTxt);
// Play button
var playButton = new Text2('PLAY', {
size: 100,
fill: 0x00FF00
});
playButton.anchor.set(0.5, 0.5);
playButton.x = 1024;
playButton.y = 1200;
game.addChild(playButton);
// Exit button
var exitButton = new Text2('EXIT', {
size: 100,
fill: 0xFF0000
});
exitButton.anchor.set(0.5, 0.5);
exitButton.x = 1024;
exitButton.y = 1400;
game.addChild(exitButton);
// Function to start the game
function startGame() {
gameStarted = true;
// Hide menu elements
welcomeTxt.visible = false;
titleTxt.visible = false;
demoTxt.visible = false;
playButton.visible = false;
exitButton.visible = false;
// Show game elements
initializeGameElements();
}
// Function to initialize game elements
function initializeGameElements() {
// Grass background
var grassBackground = game.attachAsset('grass', {
x: 0,
y: 0,
anchorX: 0,
anchorY: 0
});
// Game field setup
var fieldBackground = game.attachAsset('field', {
x: 0,
y: 400,
anchorX: 0,
anchorY: 0
});
// Goal setup - Top goal
goalArea = game.attachAsset('goal', {
x: 1024,
y: 300,
anchorX: 0.5,
anchorY: 0
});
goalArea.alpha = 0.3;
leftGoalPost = game.attachAsset('goalpost', {
x: 824,
y: 300,
anchorX: 0.5,
anchorY: 0
});
rightGoalPost = game.attachAsset('goalpost', {
x: 1224,
y: 300,
anchorX: 0.5,
anchorY: 0
});
// Second goal setup - Bottom goal
goalArea2 = game.attachAsset('goal', {
x: 1024,
y: 1700,
anchorX: 0.5,
anchorY: 0
});
goalArea2.alpha = 0.3;
leftGoalPost2 = game.attachAsset('goalpost', {
x: 824,
y: 1700,
anchorX: 0.5,
anchorY: 0
});
rightGoalPost2 = game.attachAsset('goalpost', {
x: 1224,
y: 1700,
anchorX: 0.5,
anchorY: 0
});
// Center line
var centerLine = game.attachAsset('centerLine', {
x: 0,
y: 1048,
anchorX: 0,
anchorY: 0.5
});
// Center circle
var centerCircle = game.attachAsset('centerCircle', {
x: 1024,
y: 1050,
anchorX: 0.5,
anchorY: 0.5
});
centerCircle.alpha = 0.8;
// Score display
scoreTxt = new Text2('0-0', {
size: 80,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
scoreTxt.y = 50; // Position score text below the top margin
LK.gui.top.addChild(scoreTxt);
// Timer display
timerTxt = new Text2('Time: 90', {
size: 60,
fill: 0xFFFFFF
});
timerTxt.anchor.set(1, 0);
LK.gui.topRight.addChild(timerTxt);
// Game entities
player = game.addChild(new Player());
player.x = 1024;
player.y = 1500;
// Player color is now applied in constructor
ball = game.addChild(new Ball());
ball.x = 1024;
ball.y = 1050;
goalkeeper = game.addChild(new Goalkeeper());
goalkeeper.x = 1024;
goalkeeper.y = 350;
goalkeeper.centerX = 1024;
goalkeeper.baseY = 350; // Set base position for goal line
// Second goalkeeper for bottom goal
goalkeeper2 = game.addChild(new Goalkeeper());
goalkeeper2.x = 1024;
goalkeeper2.y = 1750;
goalkeeper2.centerX = 1024;
goalkeeper2.baseY = 1750; // Set base position for goal line
// Defenders
defenders = [];
for (var i = 0; i < 3; i++) {
var defender = game.addChild(new Defender());
defender.x = 600 + i * 300;
defender.y = 800 + i * 150;
defenders.push(defender);
}
// Game variables
gameTime = 90;
goals = 0;
opponentGoals = 0;
gameActive = true;
dragNode = null;
lastGameTime = 90;
// Game timer
gameTimer = LK.setInterval(function () {
if (gameActive) {
gameTime--;
timerTxt.setText('Time: ' + gameTime);
if (gameTime <= 0) {
gameActive = false;
LK.showGameOver();
}
}
}, 1000);
}
// Function to increase difficulty after each goal
function increaseDifficulty() {
// Increase goalkeeper speed
goalkeeper.speed += 0.5;
goalkeeper2.speed += 0.5;
// Increase defender speed
for (var i = 0; i < defenders.length; i++) {
defenders[i].speed += 0.3;
}
// Add new defender every 3 goals
if (goals % 3 === 0 && defenders.length < 6) {
var newDefender = game.addChild(new Defender());
newDefender.x = 400 + Math.random() * 1200;
newDefender.y = 600 + Math.random() * 600;
defenders.push(newDefender);
}
}
// Declare game variables globally
var goalArea, leftGoalPost, rightGoalPost, goalArea2, leftGoalPost2, rightGoalPost2, scoreTxt, timerTxt;
var player, ball, goalkeeper, goalkeeper2, defenders;
var gameTime, goals, opponentGoals, gameActive, dragNode, lastGameTime, gameTimer;
// Touch controls
game.down = function (x, y, obj) {
if (menuActive) {
// Check if play button was clicked
var distanceToPlay = Math.sqrt((x - playButton.x) * (x - playButton.x) + (y - playButton.y) * (y - playButton.y));
if (distanceToPlay < 150) {
menuActive = false;
startGame();
return;
}
// Check if exit button was clicked
var distanceToExit = Math.sqrt((x - exitButton.x) * (x - exitButton.x) + (y - exitButton.y) * (y - exitButton.y));
if (distanceToExit < 150) {
// Exit functionality - show game over to end the game
LK.showGameOver();
return;
}
return;
}
if (!gameActive || !gameStarted) return;
var distanceToPlayer = Math.sqrt((x - player.x) * (x - player.x) + (y - player.y) * (y - player.y));
var distanceToBall = Math.sqrt((x - ball.x) * (x - ball.x) + (y - ball.y) * (y - ball.y));
if (distanceToPlayer < 100) {
dragNode = player;
} else if (distanceToBall < 80 && player.hasBall) {
// Shoot ball towards tap location
ball.shoot(x, y);
player.hasBall = false;
}
};
game.move = function (x, y, obj) {
if (!gameActive || !gameStarted || menuActive) return;
if (dragNode === player) {
player.x = x;
player.y = y;
// Keep player within field bounds
if (player.x < 100) player.x = 100;
if (player.x > 1948) player.x = 1948;
if (player.y < 400) player.y = 400;
if (player.y > 1850) player.y = 1850;
// Move ball with player if close enough
if (player.hasBall && !ball.isMoving) {
ball.x = player.x;
ball.y = player.y - 50;
}
}
};
game.up = function (x, y, obj) {
if (!gameStarted || menuActive) return;
dragNode = null;
};
// Main game loop
game.update = function () {
if (!gameActive || !gameStarted || menuActive) return;
// Check if player can pick up ball - player always gets ball when touching it
var distanceToBall = Math.sqrt((player.x - ball.x) * (player.x - ball.x) + (player.y - ball.y) * (player.y - ball.y));
if (distanceToBall < 60 && !player.hasBall) {
player.hasBall = true;
ball.velocityX = 0;
ball.velocityY = 0;
ball.isMoving = false;
}
// Update defenders to chase ball
for (var i = 0; i < defenders.length; i++) {
var defender = defenders[i];
defender.targetX = ball.x;
defender.targetY = ball.y;
// Check collision with ball - defender always gets the ball when touching it
var distanceToBall = Math.sqrt((defender.x - ball.x) * (defender.x - ball.x) + (defender.y - ball.y) * (defender.y - ball.y));
if (distanceToBall < 60) {
// Defender always gets the ball when touching it
player.hasBall = false;
ball.velocityX = 0;
ball.velocityY = 0;
ball.isMoving = false;
// Defender gets the ball and shoots towards goal
var goalX = 1024; // Center of goal
var goalY = 300; // Top goal
// If defender is closer to bottom goal, shoot there instead
if (defender.y > 1000) {
goalY = 1750; // Bottom goal
}
ball.shoot(goalX + (Math.random() - 0.5) * 200, goalY);
LK.effects.flashObject(defender, 0x00ff00, 500);
}
// Check collision with player - when defender touches player with ball, defender gets it
var distanceToPlayer = Math.sqrt((defender.x - player.x) * (defender.x - player.x) + (defender.y - player.y) * (defender.y - player.y));
if (distanceToPlayer < 80 && player.hasBall) {
player.hasBall = false;
ball.x = defender.x;
ball.y = defender.y;
// Defender immediately shoots towards goal
var goalX = 1024;
var goalY = 1750; // Shoot towards bottom goal (our goal)
ball.shoot(goalX + (Math.random() - 0.5) * 200, goalY);
LK.effects.flashObject(player, 0xff0000, 500);
}
}
// Check collision with goalkeeper - when player touches goalkeeper, ball goes forward
var distanceToGoalkeeper = Math.sqrt((player.x - goalkeeper.x) * (player.x - goalkeeper.x) + (player.y - goalkeeper.y) * (player.y - goalkeeper.y));
if (distanceToGoalkeeper < 80) {
if (!ball.isMoving) {
// Make ball go forward (towards bottom goal)
ball.shoot(ball.x, ball.y + 300);
player.hasBall = false;
}
}
// Check collision with second goalkeeper - when player touches goalkeeper, ball goes forward
var distanceToGoalkeeper2 = Math.sqrt((player.x - goalkeeper2.x) * (player.x - goalkeeper2.x) + (player.y - goalkeeper2.y) * (player.y - goalkeeper2.y));
if (distanceToGoalkeeper2 < 80) {
if (!ball.isMoving) {
// Make ball go forward (towards top goal)
ball.shoot(ball.x, ball.y - 300);
player.hasBall = false;
}
}
// Check goal scoring - Top goal (we score)
if (ball.x > 844 && ball.x < 1204 && ball.y < 500 && ball.y > 300) {
if (ball.lastY === undefined || ball.lastY > 500) {
goals++;
LK.setScore(goals);
scoreTxt.setText(goals + '-' + opponentGoals);
LK.getSound('goal').play();
LK.effects.flashScreen(0x00ff00, 1000);
// Reset ball and player positions
player.x = 1024;
player.y = 1500;
ball.x = 1024;
ball.y = 1050;
ball.velocityX = 0;
ball.velocityY = 0;
ball.isMoving = false;
player.hasBall = false;
// Increase difficulty after each goal
increaseDifficulty();
// Check win condition
if (goals >= 10) {
gameActive = false;
LK.showYouWin();
}
}
}
// Check goal scoring - Bottom goal (opponent scores against us)
if (ball.x > 844 && ball.x < 1204 && ball.y > 1700 && ball.y < 1900) {
if (ball.lastY === undefined || ball.lastY < 1700) {
opponentGoals++;
scoreTxt.setText(goals + '-' + opponentGoals);
LK.getSound('goal').play();
LK.effects.flashScreen(0xff0000, 1000);
// Reset ball and player positions
player.x = 1024;
player.y = 1500;
ball.x = 1024;
ball.y = 1050;
ball.velocityX = 0;
ball.velocityY = 0;
ball.isMoving = false;
player.hasBall = false;
// Increase difficulty after each goal
increaseDifficulty();
// Check lose condition
if (opponentGoals >= 10) {
gameActive = false;
LK.showGameOver();
}
}
}
ball.lastY = ball.y;
};