/****
* 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.update = function () {
if (gameState === 'playing') {
self.x += self.velocityX;
self.y += self.velocityY;
self.velocityX *= self.friction;
self.velocityY *= self.friction;
// Bounce off pitch boundaries
if (self.x < 100 || self.x > 1950) {
self.velocityX *= -0.8;
self.x = Math.max(100, Math.min(1950, self.x));
}
if (self.y < 200 || self.y > 2500) {
self.velocityY *= -0.8;
self.y = Math.max(200, Math.min(2500, self.y));
}
// Check for goals - updated for centered goals
if (self.y < 250 && self.x > 824 && self.x < 1224 || self.y > 2450 && self.x > 824 && self.x < 1224) {
if (self.y < 250) {
playerScore++;
} else {
aiScore++;
}
resetBall();
updateScore();
}
}
};
return self;
});
var Player = Container.expand(function (isAI, teamName) {
var self = Container.call(this);
var isAIPlayer = isAI || false;
// Use team logo asset if team name is provided, otherwise fallback to default
var assetName = teamName || 'Manchester City';
// Use smaller AI-specific asset for AI players
if (isAIPlayer) {
assetName = assetName + 'AI';
}
var playerGraphics = self.attachAsset(assetName, {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = isAIPlayer ? 4 : 3;
self.hasBall = false;
self.targetX = 0;
self.targetY = 0;
self.update = function () {
if (isAIPlayer && gameState === 'playing') {
// AI behavior - always try to move ball towards opponent's goal
var dx = ball.x - self.x;
var dy = ball.y - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
// Primary objective: move towards ball when far away
if (distance > 100) {
self.x += dx / distance * self.speed;
self.y += dy / distance * self.speed;
} else {
// Secondary objective: when close to ball, aim towards opponent's goal (bottom goal)
var goalX = 1024; // Center of goal
var goalY = 2566; // Bottom goal Y position
var goalDx = goalX - ball.x;
var goalDy = goalY - ball.y;
var goalDistance = Math.sqrt(goalDx * goalDx + goalDy * goalDy);
// Move towards ball but in direction of goal
var combinedDx = (dx + goalDx / goalDistance * 200) / 2;
var combinedDy = (dy + goalDy / goalDistance * 200) / 2;
var combinedDistance = Math.sqrt(combinedDx * combinedDx + combinedDy * combinedDy);
if (combinedDistance > 0) {
self.x += combinedDx / combinedDistance * self.speed;
self.y += combinedDy / combinedDistance * self.speed;
}
}
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x1a1a1a
});
/****
* Game Code
****/
// Game state variables
var gameState = 'menu'; // 'menu', 'leagueSelect', 'teamSelect', 'aiTeamSelect', 'playing', 'paused'
var selectedLeague = '';
var selectedTeam = '';
var selectedAITeam = '';
var matchTime = 90;
var playerScore = 0;
var aiScore = 0;
var players = [];
var aiPlayers = [];
var ball = null;
var draggedPlayer = null;
var playerVelocityX = 0;
var playerVelocityY = 0;
var lastPlayerX = 0;
var lastPlayerY = 0;
// League and team data
var leagues = {
'Premier League': ['Manchester City', 'Manchester United', 'Tottenham', 'Arsenal'],
'Süper Lig': ['Galatasaray', 'Fenerbahçe', 'Beşiktaş', 'Trabzonspor', 'Göztepe', 'Başakşehir'],
'Trendyol 1. Lig': ['Erzurumspor', 'Adana Spor'],
'LaLiga': ['Real Madrid', 'Barcelona', 'Atletico Madrid', 'Real Betis']
};
// Team colors mapping
var teamColors = {
'Manchester City': 0x6CABDD,
'Manchester United': 0xDA020E,
'Tottenham': 0x132257,
'Arsenal': 0xEF0107,
'Galatasaray': 0xFFCC00,
'Fenerbahçe': 0x003399,
'Beşiktaş': 0x000000,
'Trabzonspor': 0x800080,
'Göztepe': 0xFF0000,
'Başakşehir': 0xFF8000,
'Erzurumspor': 0x0066CC,
'Adana Spor': 0xFF6600,
'Real Madrid': 0xFFFFFF,
'Barcelona': 0x004D98,
'Atletico Madrid': 0xCE3524,
'Real Betis': 0x00A650
};
// UI Elements
var startButton = null;
var leagueButtons = [];
var teamButtons = [];
var aiTeamButtons = [];
var menuButton = null;
var timerText = null;
var scoreText = null;
var titleText = null;
// Initialize main menu
function initMainMenu() {
clearScreen();
gameState = 'menu';
titleText = new Text2('Football League Manager', {
size: 80,
fill: 0xFFFFFF
});
titleText.anchor.set(0.5, 0.5);
titleText.x = 1024;
titleText.y = 800;
game.addChild(titleText);
var subtitleText = new Text2('AI Championship', {
size: 60,
fill: 0xCCCCCC
});
subtitleText.anchor.set(0.5, 0.5);
subtitleText.x = 1024;
subtitleText.y = 900;
game.addChild(subtitleText);
startButton = game.addChild(LK.getAsset('startButton', {
x: 1024,
y: 1200,
anchorX: 0.5,
anchorY: 0.5
}));
var startText = new Text2('Oyuna Başla', {
size: 40,
fill: 0xFFFFFF
});
startText.anchor.set(0.5, 0.5);
startText.x = 1024;
startText.y = 1200;
game.addChild(startText);
}
// Initialize league selection
function initLeagueSelect() {
clearScreen();
gameState = 'leagueSelect';
var titleText = new Text2('Liga Seçin', {
size: 70,
fill: 0xFFFFFF
});
titleText.anchor.set(0.5, 0.5);
titleText.x = 1024;
titleText.y = 600;
game.addChild(titleText);
var leagueNames = Object.keys(leagues);
for (var i = 0; i < leagueNames.length; i++) {
var button = game.addChild(LK.getAsset('leagueButton', {
x: 1024,
y: 800 + i * 120,
anchorX: 0.5,
anchorY: 0.5
}));
button.leagueName = leagueNames[i];
leagueButtons.push(button);
var buttonText = new Text2(leagueNames[i], {
size: 35,
fill: 0xFFFFFF
});
buttonText.anchor.set(0.5, 0.5);
buttonText.x = 1024;
buttonText.y = 800 + i * 120;
game.addChild(buttonText);
}
}
// Initialize team selection
function initTeamSelect() {
clearScreen();
gameState = 'teamSelect';
var titleText = new Text2('Takımınızı Seçin', {
size: 70,
fill: 0xFFFFFF
});
titleText.anchor.set(0.5, 0.5);
titleText.x = 1024;
titleText.y = 500;
game.addChild(titleText);
var leagueText = new Text2(selectedLeague, {
size: 50,
fill: 0xCCCCCC
});
leagueText.anchor.set(0.5, 0.5);
leagueText.x = 1024;
leagueText.y = 600;
game.addChild(leagueText);
var teams = leagues[selectedLeague];
for (var i = 0; i < teams.length; i++) {
var button = game.addChild(LK.getAsset('teamButton', {
x: 1024,
y: 750 + i * 100,
anchorX: 0.5,
anchorY: 0.5
}));
button.teamName = teams[i];
teamButtons.push(button);
var buttonText = new Text2(teams[i], {
size: 30,
fill: 0xFFFFFF
});
buttonText.anchor.set(0.5, 0.5);
buttonText.x = 1024;
buttonText.y = 750 + i * 100;
game.addChild(buttonText);
}
}
// Initialize AI team selection
function initAITeamSelect() {
clearScreen();
gameState = 'aiTeamSelect';
var titleText = new Text2('Rakip Takımı Seçin', {
size: 70,
fill: 0xFFFFFF
});
titleText.anchor.set(0.5, 0.5);
titleText.x = 1024;
titleText.y = 500;
game.addChild(titleText);
var yourTeamText = new Text2('Takımınız: ' + selectedTeam, {
size: 40,
fill: 0x00FF00
});
yourTeamText.anchor.set(0.5, 0.5);
yourTeamText.x = 1024;
yourTeamText.y = 600;
game.addChild(yourTeamText);
var teams = leagues[selectedLeague];
for (var i = 0; i < teams.length; i++) {
if (teams[i] !== selectedTeam) {
var button = game.addChild(LK.getAsset('teamButton', {
x: 1024,
y: 750 + aiTeamButtons.length * 100,
anchorX: 0.5,
anchorY: 0.5
}));
button.teamName = teams[i];
aiTeamButtons.push(button);
var buttonText = new Text2(teams[i], {
size: 30,
fill: 0xFFFFFF
});
buttonText.anchor.set(0.5, 0.5);
buttonText.x = 1024;
buttonText.y = 750 + (aiTeamButtons.length - 1) * 100;
game.addChild(buttonText);
}
}
}
// Initialize game field
function initGameField() {
clearScreen();
gameState = 'playing';
matchTime = 90;
playerScore = 0;
aiScore = 0;
// Create pitch
var pitch = game.addChild(LK.getAsset('pitch', {
x: 74,
y: 166,
anchorX: 0,
anchorY: 0
}));
// Center line
var centerLine = game.addChild(LK.getAsset('centerLine', {
x: 74,
y: 1362,
anchorX: 0,
anchorY: 0.5
}));
// Center circle
var centerCircle = game.addChild(LK.getAsset('centerCircle', {
x: 1024,
y: 1366,
anchorX: 0.5,
anchorY: 0.5
}));
// Field boundary lines
// Left sideline
var leftSideLine = game.addChild(LK.getAsset('sideLine', {
x: 74,
y: 166,
anchorX: 0,
anchorY: 0
}));
// Right sideline
var rightSideLine = game.addChild(LK.getAsset('sideLine', {
x: 1974,
y: 166,
anchorX: 1,
anchorY: 0
}));
// Top touchline
var topTouchLine = game.addChild(LK.getAsset('touchLine', {
x: 74,
y: 166,
anchorX: 0,
anchorY: 0
}));
// Bottom touchline
var bottomTouchLine = game.addChild(LK.getAsset('touchLine', {
x: 74,
y: 2566,
anchorX: 0,
anchorY: 1
}));
// Penalty areas removed
// Goal areas (6-yard boxes) - removed
// Goals - centered on pitch (1024 is center of 2048 screen width)
var topGoal = game.addChild(LK.getAsset('goal', {
x: 1024,
y: 166,
anchorX: 0.5,
anchorY: 1
}));
var bottomGoal = game.addChild(LK.getAsset('goal', {
x: 1024,
y: 2566,
anchorX: 0.5,
anchorY: 0
}));
// Goal posts - centered with goals
var topLeftPost = game.addChild(LK.getAsset('goalPost', {
x: 824,
y: 166,
anchorX: 0.5,
anchorY: 1
}));
var topRightPost = game.addChild(LK.getAsset('goalPost', {
x: 1224,
y: 166,
anchorX: 0.5,
anchorY: 1
}));
var bottomLeftPost = game.addChild(LK.getAsset('goalPost', {
x: 824,
y: 2566,
anchorX: 0.5,
anchorY: 0
}));
var bottomRightPost = game.addChild(LK.getAsset('goalPost', {
x: 1224,
y: 2566,
anchorX: 0.5,
anchorY: 0
}));
// Create player - single player positioned in lower field
var player = new Player(false, selectedTeam);
player.x = 1024;
player.y = 2000;
players.push(player);
game.addChild(player);
// Create AI player - single AI player positioned in upper field
var aiPlayer = new Player(true, selectedAITeam);
aiPlayer.x = 1024;
aiPlayer.y = 700;
aiPlayers.push(aiPlayer);
game.addChild(aiPlayer);
// Create ball
ball = new Ball();
ball.x = 1024;
ball.y = 1366;
game.addChild(ball);
// Create UI elements
menuButton = game.addChild(LK.getAsset('menuButton', {
x: 100,
y: 100,
anchorX: 0.5,
anchorY: 0.5
}));
timerText = new Text2('90', {
size: 60,
fill: 0xFFFFFF
});
timerText.anchor.set(1, 0);
LK.gui.topRight.addChild(timerText);
// Create scoreboard above top goal
var centerScoreboard = game.addChild(LK.getAsset('scoreboard', {
x: 1024,
y: 100,
anchorX: 0.5,
anchorY: 0.5
}));
// Scoreboard text above top goal
var centerScoreText = new Text2(selectedTeam + ' 0 - 0 ' + selectedAITeam, {
size: 40,
fill: 0xFFFFFF
});
centerScoreText.anchor.set(0.5, 0.5);
centerScoreText.x = 1024;
centerScoreText.y = 100;
game.addChild(centerScoreText);
// Store references for updates
scoreText = centerScoreText;
var bottomScoreTextRef = null;
}
function clearScreen() {
// Remove all children from game
while (game.children.length > 0) {
game.removeChild(game.children[0]);
}
// Clear UI elements
while (LK.gui.top.children.length > 0) {
LK.gui.top.removeChild(LK.gui.top.children[0]);
}
while (LK.gui.topRight.children.length > 0) {
LK.gui.topRight.removeChild(LK.gui.topRight.children[0]);
}
while (LK.gui.right.children.length > 0) {
LK.gui.right.removeChild(LK.gui.right.children[0]);
}
// Clear arrays
leagueButtons = [];
teamButtons = [];
aiTeamButtons = [];
players = [];
aiPlayers = [];
// Clear scoreboard references
if (typeof bottomScoreTextRef !== 'undefined') {
bottomScoreTextRef = null;
}
}
function resetBall() {
ball.x = 1024;
ball.y = 1366;
ball.velocityX = 0;
ball.velocityY = 0;
}
function updateScore() {
if (scoreText) {
// Create simple text format - Text2 doesn't support HTML formatting
var formattedText = selectedTeam + ' ' + playerScore + ' - ' + aiScore + ' ' + selectedAITeam;
scoreText.setText(formattedText);
}
}
function checkGameEnd() {
if (matchTime <= 0) {
if (playerScore > aiScore) {
LK.showYouWin();
} else if (aiScore > playerScore) {
LK.showGameOver();
} else {
LK.showGameOver(); // Draw also triggers game over for now
}
}
}
// Game timer
var gameTimer = LK.setInterval(function () {
if (gameState === 'playing') {
matchTime--;
if (timerText) {
timerText.setText(matchTime.toString());
}
checkGameEnd();
}
}, 1000);
// Event handlers
game.down = function (x, y, obj) {
if (gameState === 'menu' && startButton) {
// Check if click is within start button bounds
var buttonLeft = startButton.x - 150; // half of button width (300/2)
var buttonRight = startButton.x + 150;
var buttonTop = startButton.y - 50; // half of button height (100/2)
var buttonBottom = startButton.y + 50;
if (x >= buttonLeft && x <= buttonRight && y >= buttonTop && y <= buttonBottom) {
initLeagueSelect();
}
} else if (gameState === 'leagueSelect') {
for (var i = 0; i < leagueButtons.length; i++) {
var button = leagueButtons[i];
var buttonLeft = button.x - 200; // half of button width (400/2)
var buttonRight = button.x + 200;
var buttonTop = button.y - 40; // half of button height (80/2)
var buttonBottom = button.y + 40;
if (x >= buttonLeft && x <= buttonRight && y >= buttonTop && y <= buttonBottom) {
selectedLeague = button.leagueName;
initTeamSelect();
break;
}
}
} else if (gameState === 'teamSelect') {
for (var i = 0; i < teamButtons.length; i++) {
var button = teamButtons[i];
var buttonLeft = button.x - 175; // half of button width (350/2)
var buttonRight = button.x + 175;
var buttonTop = button.y - 35; // half of button height (70/2)
var buttonBottom = button.y + 35;
if (x >= buttonLeft && x <= buttonRight && y >= buttonTop && y <= buttonBottom) {
selectedTeam = button.teamName;
initAITeamSelect();
break;
}
}
} else if (gameState === 'aiTeamSelect') {
for (var i = 0; i < aiTeamButtons.length; i++) {
var button = aiTeamButtons[i];
var buttonLeft = button.x - 175; // half of button width (350/2)
var buttonRight = button.x + 175;
var buttonTop = button.y - 35; // half of button height (70/2)
var buttonBottom = button.y + 35;
if (x >= buttonLeft && x <= buttonRight && y >= buttonTop && y <= buttonBottom) {
selectedAITeam = button.teamName;
initGameField();
break;
}
}
} else if (gameState === 'playing') {
if (menuButton) {
var buttonLeft = menuButton.x - 100; // half of button width (200/2)
var buttonRight = menuButton.x + 100;
var buttonTop = menuButton.y - 40; // half of button height (80/2)
var buttonBottom = menuButton.y + 40;
if (x >= buttonLeft && x <= buttonRight && y >= buttonTop && y <= buttonBottom) {
gameState = 'paused';
return;
}
}
// Check if clicked on a player
for (var i = 0; i < players.length; i++) {
var player = players[i];
var dx = x - player.x;
var dy = y - player.y;
if (Math.sqrt(dx * dx + dy * dy) < 87.5) {
draggedPlayer = player;
lastPlayerX = player.x;
lastPlayerY = player.y;
playerVelocityX = 0;
playerVelocityY = 0;
break;
}
}
}
};
game.move = function (x, y, obj) {
if (gameState === 'playing' && draggedPlayer) {
playerVelocityX = x - lastPlayerX;
playerVelocityY = y - lastPlayerY;
lastPlayerX = x;
lastPlayerY = y;
draggedPlayer.x = x;
draggedPlayer.y = y;
}
};
game.up = function (x, y, obj) {
draggedPlayer = null;
};
game.update = function () {
// Update game objects based on state
if (gameState === 'playing') {
// Update players
for (var i = 0; i < players.length; i++) {
if (players[i].update) {
players[i].update();
}
}
// Update AI players
for (var i = 0; i < aiPlayers.length; i++) {
if (aiPlayers[i].update) {
aiPlayers[i].update();
}
}
// Update ball
if (ball && ball.update) {
ball.update();
}
// Player ball interaction - direct hitting
for (var i = 0; i < players.length; i++) {
var player = players[i];
var dx = ball.x - player.x;
var dy = ball.y - player.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < 87.5) {
// Calculate hit force based on player velocity - responsive to drag speed
var velocityMagnitude = Math.sqrt(playerVelocityX * playerVelocityX + playerVelocityY * playerVelocityY);
// Scale velocity magnitude to create more responsive ball speed
var hitForce = Math.max(2, Math.min(35, velocityMagnitude * 0.8));
// Hit the ball away from player
var directionX = dx / distance;
var directionY = dy / distance;
ball.velocityX = directionX * hitForce;
ball.velocityY = directionY * hitForce;
}
}
// AI player ball interaction
for (var i = 0; i < aiPlayers.length; i++) {
var aiPlayer = aiPlayers[i];
var dx = ball.x - aiPlayer.x;
var dy = ball.y - aiPlayer.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < 85) {
// AI always kicks towards opponent's goal (bottom goal)
var goalX = 1024; // Center of goal
var goalY = 2566; // Bottom goal Y position
var goalDx = goalX - ball.x;
var goalDy = goalY - ball.y;
var goalDistance = Math.sqrt(goalDx * goalDx + goalDy * goalDy);
// Kick ball towards goal with increased force
var hitForce = 15;
ball.velocityX = goalDx / goalDistance * hitForce;
ball.velocityY = goalDy / goalDistance * hitForce;
}
}
}
};
// Initialize the game
initMainMenu(); /****
* 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.update = function () {
if (gameState === 'playing') {
self.x += self.velocityX;
self.y += self.velocityY;
self.velocityX *= self.friction;
self.velocityY *= self.friction;
// Bounce off pitch boundaries
if (self.x < 100 || self.x > 1950) {
self.velocityX *= -0.8;
self.x = Math.max(100, Math.min(1950, self.x));
}
if (self.y < 200 || self.y > 2500) {
self.velocityY *= -0.8;
self.y = Math.max(200, Math.min(2500, self.y));
}
// Check for goals - updated for centered goals
if (self.y < 250 && self.x > 824 && self.x < 1224 || self.y > 2450 && self.x > 824 && self.x < 1224) {
if (self.y < 250) {
playerScore++;
} else {
aiScore++;
}
resetBall();
updateScore();
}
}
};
return self;
});
var Player = Container.expand(function (isAI, teamName) {
var self = Container.call(this);
var isAIPlayer = isAI || false;
// Use team logo asset if team name is provided, otherwise fallback to default
var assetName = teamName || 'Manchester City';
// Use smaller AI-specific asset for AI players
if (isAIPlayer) {
assetName = assetName + 'AI';
}
var playerGraphics = self.attachAsset(assetName, {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = isAIPlayer ? 4 : 3;
self.hasBall = false;
self.targetX = 0;
self.targetY = 0;
self.update = function () {
if (isAIPlayer && gameState === 'playing') {
// AI behavior - always try to move ball towards opponent's goal
var dx = ball.x - self.x;
var dy = ball.y - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
// Primary objective: move towards ball when far away
if (distance > 100) {
self.x += dx / distance * self.speed;
self.y += dy / distance * self.speed;
} else {
// Secondary objective: when close to ball, aim towards opponent's goal (bottom goal)
var goalX = 1024; // Center of goal
var goalY = 2566; // Bottom goal Y position
var goalDx = goalX - ball.x;
var goalDy = goalY - ball.y;
var goalDistance = Math.sqrt(goalDx * goalDx + goalDy * goalDy);
// Move towards ball but in direction of goal
var combinedDx = (dx + goalDx / goalDistance * 200) / 2;
var combinedDy = (dy + goalDy / goalDistance * 200) / 2;
var combinedDistance = Math.sqrt(combinedDx * combinedDx + combinedDy * combinedDy);
if (combinedDistance > 0) {
self.x += combinedDx / combinedDistance * self.speed;
self.y += combinedDy / combinedDistance * self.speed;
}
}
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x1a1a1a
});
/****
* Game Code
****/
// Game state variables
var gameState = 'menu'; // 'menu', 'leagueSelect', 'teamSelect', 'aiTeamSelect', 'playing', 'paused'
var selectedLeague = '';
var selectedTeam = '';
var selectedAITeam = '';
var matchTime = 90;
var playerScore = 0;
var aiScore = 0;
var players = [];
var aiPlayers = [];
var ball = null;
var draggedPlayer = null;
var playerVelocityX = 0;
var playerVelocityY = 0;
var lastPlayerX = 0;
var lastPlayerY = 0;
// League and team data
var leagues = {
'Premier League': ['Manchester City', 'Manchester United', 'Tottenham', 'Arsenal'],
'Süper Lig': ['Galatasaray', 'Fenerbahçe', 'Beşiktaş', 'Trabzonspor', 'Göztepe', 'Başakşehir'],
'Trendyol 1. Lig': ['Erzurumspor', 'Adana Spor'],
'LaLiga': ['Real Madrid', 'Barcelona', 'Atletico Madrid', 'Real Betis']
};
// Team colors mapping
var teamColors = {
'Manchester City': 0x6CABDD,
'Manchester United': 0xDA020E,
'Tottenham': 0x132257,
'Arsenal': 0xEF0107,
'Galatasaray': 0xFFCC00,
'Fenerbahçe': 0x003399,
'Beşiktaş': 0x000000,
'Trabzonspor': 0x800080,
'Göztepe': 0xFF0000,
'Başakşehir': 0xFF8000,
'Erzurumspor': 0x0066CC,
'Adana Spor': 0xFF6600,
'Real Madrid': 0xFFFFFF,
'Barcelona': 0x004D98,
'Atletico Madrid': 0xCE3524,
'Real Betis': 0x00A650
};
// UI Elements
var startButton = null;
var leagueButtons = [];
var teamButtons = [];
var aiTeamButtons = [];
var menuButton = null;
var timerText = null;
var scoreText = null;
var titleText = null;
// Initialize main menu
function initMainMenu() {
clearScreen();
gameState = 'menu';
titleText = new Text2('Football League Manager', {
size: 80,
fill: 0xFFFFFF
});
titleText.anchor.set(0.5, 0.5);
titleText.x = 1024;
titleText.y = 800;
game.addChild(titleText);
var subtitleText = new Text2('AI Championship', {
size: 60,
fill: 0xCCCCCC
});
subtitleText.anchor.set(0.5, 0.5);
subtitleText.x = 1024;
subtitleText.y = 900;
game.addChild(subtitleText);
startButton = game.addChild(LK.getAsset('startButton', {
x: 1024,
y: 1200,
anchorX: 0.5,
anchorY: 0.5
}));
var startText = new Text2('Oyuna Başla', {
size: 40,
fill: 0xFFFFFF
});
startText.anchor.set(0.5, 0.5);
startText.x = 1024;
startText.y = 1200;
game.addChild(startText);
}
// Initialize league selection
function initLeagueSelect() {
clearScreen();
gameState = 'leagueSelect';
var titleText = new Text2('Liga Seçin', {
size: 70,
fill: 0xFFFFFF
});
titleText.anchor.set(0.5, 0.5);
titleText.x = 1024;
titleText.y = 600;
game.addChild(titleText);
var leagueNames = Object.keys(leagues);
for (var i = 0; i < leagueNames.length; i++) {
var button = game.addChild(LK.getAsset('leagueButton', {
x: 1024,
y: 800 + i * 120,
anchorX: 0.5,
anchorY: 0.5
}));
button.leagueName = leagueNames[i];
leagueButtons.push(button);
var buttonText = new Text2(leagueNames[i], {
size: 35,
fill: 0xFFFFFF
});
buttonText.anchor.set(0.5, 0.5);
buttonText.x = 1024;
buttonText.y = 800 + i * 120;
game.addChild(buttonText);
}
}
// Initialize team selection
function initTeamSelect() {
clearScreen();
gameState = 'teamSelect';
var titleText = new Text2('Takımınızı Seçin', {
size: 70,
fill: 0xFFFFFF
});
titleText.anchor.set(0.5, 0.5);
titleText.x = 1024;
titleText.y = 500;
game.addChild(titleText);
var leagueText = new Text2(selectedLeague, {
size: 50,
fill: 0xCCCCCC
});
leagueText.anchor.set(0.5, 0.5);
leagueText.x = 1024;
leagueText.y = 600;
game.addChild(leagueText);
var teams = leagues[selectedLeague];
for (var i = 0; i < teams.length; i++) {
var button = game.addChild(LK.getAsset('teamButton', {
x: 1024,
y: 750 + i * 100,
anchorX: 0.5,
anchorY: 0.5
}));
button.teamName = teams[i];
teamButtons.push(button);
var buttonText = new Text2(teams[i], {
size: 30,
fill: 0xFFFFFF
});
buttonText.anchor.set(0.5, 0.5);
buttonText.x = 1024;
buttonText.y = 750 + i * 100;
game.addChild(buttonText);
}
}
// Initialize AI team selection
function initAITeamSelect() {
clearScreen();
gameState = 'aiTeamSelect';
var titleText = new Text2('Rakip Takımı Seçin', {
size: 70,
fill: 0xFFFFFF
});
titleText.anchor.set(0.5, 0.5);
titleText.x = 1024;
titleText.y = 500;
game.addChild(titleText);
var yourTeamText = new Text2('Takımınız: ' + selectedTeam, {
size: 40,
fill: 0x00FF00
});
yourTeamText.anchor.set(0.5, 0.5);
yourTeamText.x = 1024;
yourTeamText.y = 600;
game.addChild(yourTeamText);
var teams = leagues[selectedLeague];
for (var i = 0; i < teams.length; i++) {
if (teams[i] !== selectedTeam) {
var button = game.addChild(LK.getAsset('teamButton', {
x: 1024,
y: 750 + aiTeamButtons.length * 100,
anchorX: 0.5,
anchorY: 0.5
}));
button.teamName = teams[i];
aiTeamButtons.push(button);
var buttonText = new Text2(teams[i], {
size: 30,
fill: 0xFFFFFF
});
buttonText.anchor.set(0.5, 0.5);
buttonText.x = 1024;
buttonText.y = 750 + (aiTeamButtons.length - 1) * 100;
game.addChild(buttonText);
}
}
}
// Initialize game field
function initGameField() {
clearScreen();
gameState = 'playing';
matchTime = 90;
playerScore = 0;
aiScore = 0;
// Create pitch
var pitch = game.addChild(LK.getAsset('pitch', {
x: 74,
y: 166,
anchorX: 0,
anchorY: 0
}));
// Center line
var centerLine = game.addChild(LK.getAsset('centerLine', {
x: 74,
y: 1362,
anchorX: 0,
anchorY: 0.5
}));
// Center circle
var centerCircle = game.addChild(LK.getAsset('centerCircle', {
x: 1024,
y: 1366,
anchorX: 0.5,
anchorY: 0.5
}));
// Field boundary lines
// Left sideline
var leftSideLine = game.addChild(LK.getAsset('sideLine', {
x: 74,
y: 166,
anchorX: 0,
anchorY: 0
}));
// Right sideline
var rightSideLine = game.addChild(LK.getAsset('sideLine', {
x: 1974,
y: 166,
anchorX: 1,
anchorY: 0
}));
// Top touchline
var topTouchLine = game.addChild(LK.getAsset('touchLine', {
x: 74,
y: 166,
anchorX: 0,
anchorY: 0
}));
// Bottom touchline
var bottomTouchLine = game.addChild(LK.getAsset('touchLine', {
x: 74,
y: 2566,
anchorX: 0,
anchorY: 1
}));
// Penalty areas removed
// Goal areas (6-yard boxes) - removed
// Goals - centered on pitch (1024 is center of 2048 screen width)
var topGoal = game.addChild(LK.getAsset('goal', {
x: 1024,
y: 166,
anchorX: 0.5,
anchorY: 1
}));
var bottomGoal = game.addChild(LK.getAsset('goal', {
x: 1024,
y: 2566,
anchorX: 0.5,
anchorY: 0
}));
// Goal posts - centered with goals
var topLeftPost = game.addChild(LK.getAsset('goalPost', {
x: 824,
y: 166,
anchorX: 0.5,
anchorY: 1
}));
var topRightPost = game.addChild(LK.getAsset('goalPost', {
x: 1224,
y: 166,
anchorX: 0.5,
anchorY: 1
}));
var bottomLeftPost = game.addChild(LK.getAsset('goalPost', {
x: 824,
y: 2566,
anchorX: 0.5,
anchorY: 0
}));
var bottomRightPost = game.addChild(LK.getAsset('goalPost', {
x: 1224,
y: 2566,
anchorX: 0.5,
anchorY: 0
}));
// Create player - single player positioned in lower field
var player = new Player(false, selectedTeam);
player.x = 1024;
player.y = 2000;
players.push(player);
game.addChild(player);
// Create AI player - single AI player positioned in upper field
var aiPlayer = new Player(true, selectedAITeam);
aiPlayer.x = 1024;
aiPlayer.y = 700;
aiPlayers.push(aiPlayer);
game.addChild(aiPlayer);
// Create ball
ball = new Ball();
ball.x = 1024;
ball.y = 1366;
game.addChild(ball);
// Create UI elements
menuButton = game.addChild(LK.getAsset('menuButton', {
x: 100,
y: 100,
anchorX: 0.5,
anchorY: 0.5
}));
timerText = new Text2('90', {
size: 60,
fill: 0xFFFFFF
});
timerText.anchor.set(1, 0);
LK.gui.topRight.addChild(timerText);
// Create scoreboard above top goal
var centerScoreboard = game.addChild(LK.getAsset('scoreboard', {
x: 1024,
y: 100,
anchorX: 0.5,
anchorY: 0.5
}));
// Scoreboard text above top goal
var centerScoreText = new Text2(selectedTeam + ' 0 - 0 ' + selectedAITeam, {
size: 40,
fill: 0xFFFFFF
});
centerScoreText.anchor.set(0.5, 0.5);
centerScoreText.x = 1024;
centerScoreText.y = 100;
game.addChild(centerScoreText);
// Store references for updates
scoreText = centerScoreText;
var bottomScoreTextRef = null;
}
function clearScreen() {
// Remove all children from game
while (game.children.length > 0) {
game.removeChild(game.children[0]);
}
// Clear UI elements
while (LK.gui.top.children.length > 0) {
LK.gui.top.removeChild(LK.gui.top.children[0]);
}
while (LK.gui.topRight.children.length > 0) {
LK.gui.topRight.removeChild(LK.gui.topRight.children[0]);
}
while (LK.gui.right.children.length > 0) {
LK.gui.right.removeChild(LK.gui.right.children[0]);
}
// Clear arrays
leagueButtons = [];
teamButtons = [];
aiTeamButtons = [];
players = [];
aiPlayers = [];
// Clear scoreboard references
if (typeof bottomScoreTextRef !== 'undefined') {
bottomScoreTextRef = null;
}
}
function resetBall() {
ball.x = 1024;
ball.y = 1366;
ball.velocityX = 0;
ball.velocityY = 0;
}
function updateScore() {
if (scoreText) {
// Create simple text format - Text2 doesn't support HTML formatting
var formattedText = selectedTeam + ' ' + playerScore + ' - ' + aiScore + ' ' + selectedAITeam;
scoreText.setText(formattedText);
}
}
function checkGameEnd() {
if (matchTime <= 0) {
if (playerScore > aiScore) {
LK.showYouWin();
} else if (aiScore > playerScore) {
LK.showGameOver();
} else {
LK.showGameOver(); // Draw also triggers game over for now
}
}
}
// Game timer
var gameTimer = LK.setInterval(function () {
if (gameState === 'playing') {
matchTime--;
if (timerText) {
timerText.setText(matchTime.toString());
}
checkGameEnd();
}
}, 1000);
// Event handlers
game.down = function (x, y, obj) {
if (gameState === 'menu' && startButton) {
// Check if click is within start button bounds
var buttonLeft = startButton.x - 150; // half of button width (300/2)
var buttonRight = startButton.x + 150;
var buttonTop = startButton.y - 50; // half of button height (100/2)
var buttonBottom = startButton.y + 50;
if (x >= buttonLeft && x <= buttonRight && y >= buttonTop && y <= buttonBottom) {
initLeagueSelect();
}
} else if (gameState === 'leagueSelect') {
for (var i = 0; i < leagueButtons.length; i++) {
var button = leagueButtons[i];
var buttonLeft = button.x - 200; // half of button width (400/2)
var buttonRight = button.x + 200;
var buttonTop = button.y - 40; // half of button height (80/2)
var buttonBottom = button.y + 40;
if (x >= buttonLeft && x <= buttonRight && y >= buttonTop && y <= buttonBottom) {
selectedLeague = button.leagueName;
initTeamSelect();
break;
}
}
} else if (gameState === 'teamSelect') {
for (var i = 0; i < teamButtons.length; i++) {
var button = teamButtons[i];
var buttonLeft = button.x - 175; // half of button width (350/2)
var buttonRight = button.x + 175;
var buttonTop = button.y - 35; // half of button height (70/2)
var buttonBottom = button.y + 35;
if (x >= buttonLeft && x <= buttonRight && y >= buttonTop && y <= buttonBottom) {
selectedTeam = button.teamName;
initAITeamSelect();
break;
}
}
} else if (gameState === 'aiTeamSelect') {
for (var i = 0; i < aiTeamButtons.length; i++) {
var button = aiTeamButtons[i];
var buttonLeft = button.x - 175; // half of button width (350/2)
var buttonRight = button.x + 175;
var buttonTop = button.y - 35; // half of button height (70/2)
var buttonBottom = button.y + 35;
if (x >= buttonLeft && x <= buttonRight && y >= buttonTop && y <= buttonBottom) {
selectedAITeam = button.teamName;
initGameField();
break;
}
}
} else if (gameState === 'playing') {
if (menuButton) {
var buttonLeft = menuButton.x - 100; // half of button width (200/2)
var buttonRight = menuButton.x + 100;
var buttonTop = menuButton.y - 40; // half of button height (80/2)
var buttonBottom = menuButton.y + 40;
if (x >= buttonLeft && x <= buttonRight && y >= buttonTop && y <= buttonBottom) {
gameState = 'paused';
return;
}
}
// Check if clicked on a player
for (var i = 0; i < players.length; i++) {
var player = players[i];
var dx = x - player.x;
var dy = y - player.y;
if (Math.sqrt(dx * dx + dy * dy) < 87.5) {
draggedPlayer = player;
lastPlayerX = player.x;
lastPlayerY = player.y;
playerVelocityX = 0;
playerVelocityY = 0;
break;
}
}
}
};
game.move = function (x, y, obj) {
if (gameState === 'playing' && draggedPlayer) {
playerVelocityX = x - lastPlayerX;
playerVelocityY = y - lastPlayerY;
lastPlayerX = x;
lastPlayerY = y;
draggedPlayer.x = x;
draggedPlayer.y = y;
}
};
game.up = function (x, y, obj) {
draggedPlayer = null;
};
game.update = function () {
// Update game objects based on state
if (gameState === 'playing') {
// Update players
for (var i = 0; i < players.length; i++) {
if (players[i].update) {
players[i].update();
}
}
// Update AI players
for (var i = 0; i < aiPlayers.length; i++) {
if (aiPlayers[i].update) {
aiPlayers[i].update();
}
}
// Update ball
if (ball && ball.update) {
ball.update();
}
// Player ball interaction - direct hitting
for (var i = 0; i < players.length; i++) {
var player = players[i];
var dx = ball.x - player.x;
var dy = ball.y - player.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < 87.5) {
// Calculate hit force based on player velocity - responsive to drag speed
var velocityMagnitude = Math.sqrt(playerVelocityX * playerVelocityX + playerVelocityY * playerVelocityY);
// Scale velocity magnitude to create more responsive ball speed
var hitForce = Math.max(2, Math.min(35, velocityMagnitude * 0.8));
// Hit the ball away from player
var directionX = dx / distance;
var directionY = dy / distance;
ball.velocityX = directionX * hitForce;
ball.velocityY = directionY * hitForce;
}
}
// AI player ball interaction
for (var i = 0; i < aiPlayers.length; i++) {
var aiPlayer = aiPlayers[i];
var dx = ball.x - aiPlayer.x;
var dy = ball.y - aiPlayer.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < 85) {
// AI always kicks towards opponent's goal (bottom goal)
var goalX = 1024; // Center of goal
var goalY = 2566; // Bottom goal Y position
var goalDx = goalX - ball.x;
var goalDy = goalY - ball.y;
var goalDistance = Math.sqrt(goalDx * goalDx + goalDy * goalDy);
// Kick ball towards goal with increased force
var hitForce = 15;
ball.velocityX = goalDx / goalDistance * hitForce;
ball.velocityY = goalDy / goalDistance * hitForce;
}
}
}
};
// Initialize the game
initMainMenu();