/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.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.96;
self.update = function () {
self.x += self.velocityX;
self.y += self.velocityY;
self.velocityX *= self.friction;
self.velocityY *= self.friction;
// Boundary checks
if (self.x < 30) {
self.x = 30;
self.velocityX = -self.velocityX * 0.7;
}
if (self.x > 2018) {
self.x = 2018;
self.velocityX = -self.velocityX * 0.7;
}
if (self.y < 30) {
self.y = 30;
self.velocityY = -self.velocityY * 0.7;
}
if (self.y > 2702) {
self.y = 2702;
self.velocityY = -self.velocityY * 0.7;
}
};
self.kick = function (forceX, forceY) {
self.velocityX += forceX;
self.velocityY += forceY;
var kickSound = LK.getSound('kick');
kickSound.volume = soundVolume;
kickSound.play();
};
return self;
});
var MenuButton = Container.expand(function (text, color) {
var self = Container.call(this);
var bg = self.attachAsset('menuButton', {
anchorX: 0.5,
anchorY: 0.5
});
if (color) {
bg.tint = color;
}
var buttonText = new Text2(text, {
size: 40,
fill: 0xFFFFFF
});
buttonText.anchor.set(0.5, 0.5);
self.addChild(buttonText);
self.down = function (x, y, obj) {
tween(self, {
scaleX: 0.95,
scaleY: 0.95
}, {
duration: 100
});
};
self.up = function (x, y, obj) {
tween(self, {
scaleX: 1,
scaleY: 1
}, {
duration: 100
});
if (self.onPress) {
self.onPress();
}
};
return self;
});
var Player = Container.expand(function (teamLogo, fallbackColor) {
var self = Container.call(this);
var playerGraphics;
if (teamLogo) {
// Create a circular background for the team logo
var circleBackground = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
if (fallbackColor) {
circleBackground.tint = fallbackColor;
} else {
circleBackground.tint = 0xFFFFFF;
}
// Add team logo on top, scaled down to fit inside circle
var logoGraphics = self.attachAsset(teamLogo, {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 0.6,
scaleY: 0.6
});
playerGraphics = circleBackground; // Keep reference to main graphics
} else {
playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
if (fallbackColor) {
playerGraphics.tint = fallbackColor;
}
}
self.isAI = false;
self.targetX = 0;
self.targetY = 0;
self.lastX = 0;
self.lastY = 0;
self.velocityX = 0;
self.velocityY = 0;
self.update = function () {
// Calculate velocity based on position change
self.velocityX = self.x - self.lastX;
self.velocityY = self.y - self.lastY;
self.lastX = self.x;
self.lastY = self.y;
if (self.isAI && ball) {
// AI behavior focused on scoring goals
var dx = ball.x - self.x;
var dy = ball.y - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
// Determine opponent's goal position based on AI's starting position
var opponentGoalX = 1024;
var opponentGoalY = self.y < 1366 ? 2650 : 82; // If AI is in top half, target bottom goal, else target top goal
// AI strategy: Always try to get the ball and move it toward opponent's goal
var targetX, targetY;
if (distance < 150) {
// Close to ball - position to kick toward opponent's goal
var ballToGoalX = opponentGoalX - ball.x;
var ballToGoalY = opponentGoalY - ball.y;
var goalDistance = Math.sqrt(ballToGoalX * ballToGoalX + ballToGoalY * ballToGoalY);
// Position behind the ball relative to the goal
var behindBallX = ball.x - ballToGoalX / goalDistance * 60;
var behindBallY = ball.y - ballToGoalY / goalDistance * 60;
targetX = behindBallX;
targetY = behindBallY;
} else {
// Far from ball - move directly toward the ball
targetX = ball.x;
targetY = ball.y;
}
// Simple movement with increased speed toward goal
var moveDistance = Math.sqrt((targetX - self.x) * (targetX - self.x) + (targetY - self.y) * (targetY - self.y));
if (moveDistance > 5) {
var moveSpeed = distance < 150 ? 18 : 15; // Faster speed when close to ball (18) or when chasing (15)
var moveX = (targetX - self.x) / moveDistance * moveSpeed;
var moveY = (targetY - self.y) / moveDistance * moveSpeed;
self.x += moveX;
self.y += moveY;
}
// Goal-oriented kicking
if (distance < 80) {
// Calculate direction to opponent's goal
var ballToGoalX = opponentGoalX - ball.x;
var ballToGoalY = opponentGoalY - ball.y;
var goalDistance = Math.sqrt(ballToGoalX * ballToGoalX + ballToGoalY * ballToGoalY);
// Kick with moderate force toward the goal
var speedMultiplier = 0.04; // Moderate kick strength
var kickForceX = ballToGoalX / goalDistance * speedMultiplier;
var kickForceY = ballToGoalY / goalDistance * speedMultiplier;
ball.kick(kickForceX, kickForceY);
}
}
};
self.down = function (x, y, obj) {
if (!self.isAI) {
self.targetX = x;
self.targetY = y;
}
};
// Move method removed - now handled by global game event handlers
return self;
});
var SettingsButton = Container.expand(function (text, color) {
var self = Container.call(this);
var bg = self.attachAsset('menuButton', {
anchorX: 0.5,
anchorY: 0.5
});
if (color) {
bg.tint = color;
}
var buttonText = new Text2(text, {
size: 40,
fill: 0xFFFFFF
});
buttonText.anchor.set(0.5, 0.5);
self.addChild(buttonText);
self.down = function (x, y, obj) {
tween(self, {
scaleX: 0.95,
scaleY: 0.95
}, {
duration: 100
});
};
self.up = function (x, y, obj) {
tween(self, {
scaleX: 1,
scaleY: 1
}, {
duration: 100
});
if (self.onPress) {
self.onPress();
}
};
return self;
});
var TeamButton = Container.expand(function (teamName, color) {
var self = Container.call(this);
var bg = self.attachAsset('teamButton', {
anchorX: 0.5,
anchorY: 0.5
});
if (color) {
bg.tint = color;
}
var buttonText = new Text2(teamName, {
size: 32,
fill: 0xFFFFFF
});
buttonText.anchor.set(0.5, 0.5);
self.addChild(buttonText);
self.teamName = teamName;
self.teamColor = color;
self.down = function (x, y, obj) {
tween(self, {
scaleX: 0.95,
scaleY: 0.95
}, {
duration: 100
});
};
self.up = function (x, y, obj) {
tween(self, {
scaleX: 1,
scaleY: 1
}, {
duration: 100
});
if (self.onPress) {
self.onPress();
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x004400
});
/****
* Game Code
****/
// Team logo assets
// Game state variables
var gameState = 'mainMenu'; // mainMenu, leagueSelect, teamSelect, playing
var gameMode = ''; // 'twoPlayer' or 'vsAI'
var selectedLeague = '';
var team1Name = '';
var team2Name = '';
var team1Color = 0xFF0000;
var team2Color = 0x0000FF;
var team1Logo = '';
var team2Logo = '';
var score1 = 0;
var score2 = 0;
// Game objects
var ball = null;
var players = [];
var field = null;
var goals = [];
var scoreboard = null;
// Team data
var superLigTeams = [{
name: 'Galatasaray',
color: 0xFFFF00,
logo: 'galatasaray'
}, {
name: 'Fenerbahçe',
color: 0x000080,
logo: 'fenerbahce'
}, {
name: 'Beşiktaş',
color: 0x000000,
logo: 'besiktas'
}, {
name: 'Trabzonspor',
color: 0x800000,
logo: 'trabzonspor'
}, {
name: 'Başakşehir',
color: 0xFF8000,
logo: 'basaksehir'
}];
var laLigaTeams = [{
name: 'Real Madrid',
color: 0xFFFFFF,
logo: 'realmadrid'
}, {
name: 'Barcelona',
color: 0x004A9F,
logo: 'barcelona'
}];
// UI elements
var menuContainer = new Container();
var leagueContainer = new Container();
var teamContainer = new Container();
var settingsContainer = new Container();
// Settings variables
var soundVolume = storage.soundVolume || 1.0;
var settingsOpen = false;
function showMainMenu() {
gameState = 'mainMenu';
clearGame();
var title = new Text2('Futbol Pro', {
size: 80,
fill: 0xFFFFFF
});
title.anchor.set(0.5, 0.5);
title.x = 1024;
title.y = 800;
menuContainer.addChild(title);
var subtitle = new Text2('LaLiga vs Süper Lig', {
size: 50,
fill: 0xCCCCCC
});
subtitle.anchor.set(0.5, 0.5);
subtitle.x = 1024;
subtitle.y = 900;
menuContainer.addChild(subtitle);
var twoPlayerBtn = new MenuButton('İki Kişi Oyna', 0x0066CC);
twoPlayerBtn.x = 1024;
twoPlayerBtn.y = 1200;
twoPlayerBtn.onPress = function () {
gameMode = 'twoPlayer';
showLeagueSelect();
};
menuContainer.addChild(twoPlayerBtn);
var vsAIBtn = new MenuButton('Yapay Zeka ile Oyna', 0x009900);
vsAIBtn.x = 1024;
vsAIBtn.y = 1350;
vsAIBtn.onPress = function () {
gameMode = 'vsAI';
showLeagueSelect();
};
menuContainer.addChild(vsAIBtn);
game.addChild(menuContainer);
}
function showLeagueSelect() {
gameState = 'leagueSelect';
menuContainer.removeChildren();
var title = new Text2('Liga Seçin', {
size: 60,
fill: 0xFFFFFF
});
title.anchor.set(0.5, 0.5);
title.x = 1024;
title.y = 800;
leagueContainer.addChild(title);
var superLigBtn = new MenuButton('Süper Lig', 0xCC0000);
superLigBtn.x = 1024;
superLigBtn.y = 1100;
superLigBtn.onPress = function () {
selectedLeague = 'superlig';
showTeamSelect();
};
leagueContainer.addChild(superLigBtn);
var laLigaBtn = new MenuButton('LaLiga', 0x0066CC);
laLigaBtn.x = 1024;
laLigaBtn.y = 1250;
laLigaBtn.onPress = function () {
selectedLeague = 'laliga';
showTeamSelect();
};
leagueContainer.addChild(laLigaBtn);
game.addChild(leagueContainer);
}
function showTeamSelect() {
gameState = 'teamSelect';
leagueContainer.removeChildren();
var teams = selectedLeague === 'superlig' ? superLigTeams : laLigaTeams;
var selectedTeams = [];
var title = new Text2('Takım Seçin', {
size: 60,
fill: 0xFFFFFF
});
title.anchor.set(0.5, 0.5);
title.x = 1024;
title.y = 400;
teamContainer.addChild(title);
var startY = 600;
for (var i = 0; i < teams.length; i++) {
var teamBtn = new TeamButton(teams[i].name, teams[i].color);
teamBtn.x = 1024;
teamBtn.y = startY + i * 120;
teamBtn.onPress = function (team) {
return function () {
selectedTeams.push(team);
if (selectedTeams.length === 1) {
team1Name = team.name;
team1Color = team.color;
team1Logo = team.logo;
if (gameMode === 'vsAI') {
// Auto-select second team for AI
var aiTeam = teams[Math.floor(Math.random() * teams.length)];
team2Name = aiTeam.name + ' (AI)';
team2Color = aiTeam.color;
team2Logo = aiTeam.logo;
startGame();
} else {
title.setText('İkinci Takımı Seçin');
}
} else if (selectedTeams.length === 2) {
team2Name = team.name;
team2Color = team.color;
team2Logo = team.logo;
startGame();
}
};
}(teams[i]);
teamContainer.addChild(teamBtn);
}
game.addChild(teamContainer);
}
function startGame() {
gameState = 'playing';
teamContainer.removeChildren();
// Create field
field = game.attachAsset('field', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
y: 1366
});
// Create goals
var topGoal = game.attachAsset('goal', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
y: 166
});
var bottomGoal = game.attachAsset('goal', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
y: 2566
});
goals.push(topGoal, bottomGoal);
// Create goal posts for top goal
var topLeftPost = game.attachAsset('goalPost', {
anchorX: 0.5,
anchorY: 0.5,
x: 824,
y: 166
});
var topRightPost = game.attachAsset('goalPost', {
anchorX: 0.5,
anchorY: 0.5,
x: 1224,
y: 166
});
// Create goal posts for bottom goal
var bottomLeftPost = game.attachAsset('goalPost', {
anchorX: 0.5,
anchorY: 0.5,
x: 824,
y: 2566
});
var bottomRightPost = game.attachAsset('goalPost', {
anchorX: 0.5,
anchorY: 0.5,
x: 1224,
y: 2566
});
// Create center line
var centerLine = game.attachAsset('centerLine', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
y: 1366
});
// Create penalty areas
var topPenaltyArea = game.attachAsset('penaltyArea', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
y: 316
});
topPenaltyArea.alpha = 0.3;
var bottomPenaltyArea = game.attachAsset('penaltyArea', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
y: 2416
});
bottomPenaltyArea.alpha = 0.3;
// Create center circle
var centerCircle = game.attachAsset('centerCircle', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
y: 1366
});
centerCircle.alpha = 0.3;
// Create ball
ball = new Ball();
ball.x = 1024;
ball.y = 1366;
game.addChild(ball);
// Create players
var player1 = new Player(team1Logo, team1Color);
player1.x = 1024;
player1.y = 1800;
players.push(player1);
game.addChild(player1);
var player2 = new Player(team2Logo, team2Color);
player2.x = 1024;
player2.y = 900;
if (gameMode === 'vsAI') {
player2.isAI = true;
}
players.push(player2);
game.addChild(player2);
// Create scoreboard
createScoreboard();
score1 = 0;
score2 = 0;
}
function createScoreboard() {
var scoreText = new Text2(team1Name + ' 0 - 0 ' + team2Name, {
size: 40,
fill: 0xFFFFFF
});
scoreText.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreText);
scoreboard = scoreText;
updateScoreboard();
}
function updateScoreboard() {
if (scoreboard) {
scoreboard.setText(team1Name + ' ' + score1 + ' - ' + score2 + ' ' + team2Name);
}
}
function checkGoal() {
if (!ball) return;
// Initialize lastY if not set
if (ball.lastY === undefined) {
ball.lastY = ball.y;
return;
}
// Check top goal (team 1 scores) - ball must enter from front (from below), not sides
if (ball.y < 166 && ball.x > 824 && ball.x < 1224 && ball.y > 91) {
// Only count as goal if ball entered from below (lastY was greater than goal line)
if (ball.lastY >= 166) {
score1++;
var goalSound = LK.getSound('goal');
goalSound.volume = soundVolume;
goalSound.play();
resetBallPosition();
updateScoreboard();
}
}
// Check bottom goal (team 2 scores) - ball must enter from front (from above), not sides
if (ball.y > 2566 && ball.x > 824 && ball.x < 1224 && ball.y < 2641) {
// Only count as goal if ball entered from above (lastY was less than goal line)
if (ball.lastY <= 2566) {
score2++;
var goalSound = LK.getSound('goal');
goalSound.volume = soundVolume;
goalSound.play();
resetBallPosition();
updateScoreboard();
}
}
// Update lastY for next frame
ball.lastY = ball.y;
}
function resetBallPosition() {
if (ball) {
ball.x = 1024;
ball.y = 1366;
ball.velocityX = 0;
ball.velocityY = 0;
ball.lastY = 1366; // Reset lastY tracking
}
}
function clearGame() {
if (ball) {
ball.destroy();
ball = null;
}
for (var i = 0; i < players.length; i++) {
players[i].destroy();
}
players = [];
if (field) {
field.destroy();
field = null;
}
for (var i = 0; i < goals.length; i++) {
goals[i].destroy();
}
goals = [];
if (scoreboard) {
scoreboard.destroy();
scoreboard = null;
}
}
// Global variables for tracking player control
var activePlayer = null;
var player1Active = false;
var player2Active = false;
// Game event handlers
game.down = function (x, y, obj) {
if (gameState !== 'playing') return;
// Determine which player should be controlled based on touch position
if (players.length >= 2) {
var distToPlayer1 = Math.sqrt((x - players[0].x) * (x - players[0].x) + (y - players[0].y) * (y - players[0].y));
var distToPlayer2 = Math.sqrt((x - players[1].x) * (x - players[1].x) + (y - players[1].y) * (y - players[1].y));
if (gameMode === 'twoPlayer') {
// In two player mode, closest player gets controlled
if (distToPlayer1 < distToPlayer2) {
activePlayer = players[0];
player1Active = true;
player2Active = false;
} else {
activePlayer = players[1];
player1Active = false;
player2Active = true;
}
} else {
// In vs AI mode, only player 1 can be controlled
if (!players[0].isAI) {
activePlayer = players[0];
player1Active = true;
player2Active = false;
}
}
if (activePlayer && !activePlayer.isAI) {
activePlayer.targetX = x;
activePlayer.targetY = y;
}
}
};
game.move = function (x, y, obj) {
if (gameState !== 'playing' || !activePlayer || activePlayer.isAI) return;
activePlayer.x = x;
activePlayer.y = y;
// Kick ball if close
if (ball) {
var dx = ball.x - activePlayer.x;
var dy = ball.y - activePlayer.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < 80) {
// Calculate player speed for kick force
var playerSpeed = Math.sqrt(activePlayer.velocityX * activePlayer.velocityX + activePlayer.velocityY * activePlayer.velocityY);
// Base kick force + speed multiplier (min 0.3, max 1.2)
var kickMultiplier = Math.max(0.3, Math.min(1.2, 0.3 + playerSpeed * 0.05));
ball.kick(dx * kickMultiplier, dy * kickMultiplier);
}
}
};
game.up = function (x, y, obj) {
activePlayer = null;
player1Active = false;
player2Active = false;
};
game.update = function () {
if (gameState === 'playing') {
checkGoal();
}
};
// Create menu button for top right corner
var menuBtn = new MenuButton('Menü', 0x0066CC);
menuBtn.x = 1900; // Position in top right, leaving space from edge
menuBtn.y = 150;
menuBtn.onPress = function () {
// Clear current game
clearGame();
// Clear all containers
menuContainer.removeChildren();
leagueContainer.removeChildren();
teamContainer.removeChildren();
// Remove containers from game
if (game.children.indexOf(menuContainer) !== -1) game.removeChild(menuContainer);
if (game.children.indexOf(leagueContainer) !== -1) game.removeChild(leagueContainer);
if (game.children.indexOf(teamContainer) !== -1) game.removeChild(teamContainer);
// Show game mode selection menu
showGameModeMenu();
};
LK.gui.topRight.addChild(menuBtn);
// Create menu button for top left corner
var leftMenuBtn = new MenuButton('Menü', 0x0066CC);
leftMenuBtn.x = 250; // Position in top left, leaving space from edge
leftMenuBtn.y = 150;
leftMenuBtn.onPress = function () {
// Clear current game
clearGame();
// Clear all containers
menuContainer.removeChildren();
leagueContainer.removeChildren();
teamContainer.removeChildren();
// Remove containers from game
if (game.children.indexOf(menuContainer) !== -1) game.removeChild(menuContainer);
if (game.children.indexOf(leagueContainer) !== -1) game.removeChild(leagueContainer);
if (game.children.indexOf(teamContainer) !== -1) game.removeChild(teamContainer);
// Show main menu
showMainMenu();
};
LK.gui.topLeft.addChild(leftMenuBtn);
// Add new function for game mode menu
function showGameModeMenu() {
gameState = 'gameModeSelect';
var title = new Text2('Oyun Modu Seçin', {
size: 60,
fill: 0xFFFFFF
});
title.anchor.set(0.5, 0.5);
title.x = 1024;
title.y = 800;
menuContainer.addChild(title);
var twoPlayerBtn = new MenuButton('İki Kişi Oyna', 0x0066CC);
twoPlayerBtn.x = 1024;
twoPlayerBtn.y = 1200;
twoPlayerBtn.onPress = function () {
gameMode = 'twoPlayer';
showLeagueSelect();
};
menuContainer.addChild(twoPlayerBtn);
var vsAIBtn = new MenuButton('Yapay Zeka ile Oyna', 0x009900);
vsAIBtn.x = 1024;
vsAIBtn.y = 1350;
vsAIBtn.onPress = function () {
gameMode = 'vsAI';
showLeagueSelect();
};
menuContainer.addChild(vsAIBtn);
game.addChild(menuContainer);
}
function showSettings() {
settingsOpen = true;
settingsContainer.removeChildren();
var title = new Text2('Ayarlar', {
size: 60,
fill: 0xFFFFFF
});
title.anchor.set(0.5, 0.5);
title.x = 1024;
title.y = 800;
settingsContainer.addChild(title);
var soundText = new Text2('Ses Seviyesi: ' + Math.round(soundVolume * 100) + '%', {
size: 40,
fill: 0xFFFFFF
});
soundText.anchor.set(0.5, 0.5);
soundText.x = 1024;
soundText.y = 1000;
settingsContainer.addChild(soundText);
var decreaseBtn = new SettingsButton('-', 0xCC0000);
decreaseBtn.x = 800;
decreaseBtn.y = 1150;
decreaseBtn.onPress = function () {
soundVolume = Math.max(0, soundVolume - 0.1);
storage.soundVolume = soundVolume;
soundText.setText('Ses Seviyesi: ' + Math.round(soundVolume * 100) + '%');
};
settingsContainer.addChild(decreaseBtn);
var increaseBtn = new SettingsButton('+', 0x00CC00);
increaseBtn.x = 1248;
increaseBtn.y = 1150;
increaseBtn.onPress = function () {
soundVolume = Math.min(1, soundVolume + 0.1);
storage.soundVolume = soundVolume;
soundText.setText('Ses Seviyesi: ' + Math.round(soundVolume * 100) + '%');
};
settingsContainer.addChild(increaseBtn);
var closeBtn = new SettingsButton('Kapat', 0x666666);
closeBtn.x = 1024;
closeBtn.y = 1400;
closeBtn.onPress = function () {
hideSettings();
};
settingsContainer.addChild(closeBtn);
game.addChild(settingsContainer);
}
function hideSettings() {
settingsOpen = false;
settingsContainer.removeChildren();
if (game.children.indexOf(settingsContainer) !== -1) {
game.removeChild(settingsContainer);
}
}
// Create settings button for bottom right corner
var settingsBtn = new SettingsButton('Ayarlar', 0x666666);
settingsBtn.x = 1800;
settingsBtn.y = 2600;
settingsBtn.onPress = function () {
if (settingsOpen) {
hideSettings();
} else {
showSettings();
}
};
LK.gui.bottomRight.addChild(settingsBtn);
// Initialize game
showMainMenu();
; /****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.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.96;
self.update = function () {
self.x += self.velocityX;
self.y += self.velocityY;
self.velocityX *= self.friction;
self.velocityY *= self.friction;
// Boundary checks
if (self.x < 30) {
self.x = 30;
self.velocityX = -self.velocityX * 0.7;
}
if (self.x > 2018) {
self.x = 2018;
self.velocityX = -self.velocityX * 0.7;
}
if (self.y < 30) {
self.y = 30;
self.velocityY = -self.velocityY * 0.7;
}
if (self.y > 2702) {
self.y = 2702;
self.velocityY = -self.velocityY * 0.7;
}
};
self.kick = function (forceX, forceY) {
self.velocityX += forceX;
self.velocityY += forceY;
var kickSound = LK.getSound('kick');
kickSound.volume = soundVolume;
kickSound.play();
};
return self;
});
var MenuButton = Container.expand(function (text, color) {
var self = Container.call(this);
var bg = self.attachAsset('menuButton', {
anchorX: 0.5,
anchorY: 0.5
});
if (color) {
bg.tint = color;
}
var buttonText = new Text2(text, {
size: 40,
fill: 0xFFFFFF
});
buttonText.anchor.set(0.5, 0.5);
self.addChild(buttonText);
self.down = function (x, y, obj) {
tween(self, {
scaleX: 0.95,
scaleY: 0.95
}, {
duration: 100
});
};
self.up = function (x, y, obj) {
tween(self, {
scaleX: 1,
scaleY: 1
}, {
duration: 100
});
if (self.onPress) {
self.onPress();
}
};
return self;
});
var Player = Container.expand(function (teamLogo, fallbackColor) {
var self = Container.call(this);
var playerGraphics;
if (teamLogo) {
// Create a circular background for the team logo
var circleBackground = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
if (fallbackColor) {
circleBackground.tint = fallbackColor;
} else {
circleBackground.tint = 0xFFFFFF;
}
// Add team logo on top, scaled down to fit inside circle
var logoGraphics = self.attachAsset(teamLogo, {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 0.6,
scaleY: 0.6
});
playerGraphics = circleBackground; // Keep reference to main graphics
} else {
playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
if (fallbackColor) {
playerGraphics.tint = fallbackColor;
}
}
self.isAI = false;
self.targetX = 0;
self.targetY = 0;
self.lastX = 0;
self.lastY = 0;
self.velocityX = 0;
self.velocityY = 0;
self.update = function () {
// Calculate velocity based on position change
self.velocityX = self.x - self.lastX;
self.velocityY = self.y - self.lastY;
self.lastX = self.x;
self.lastY = self.y;
if (self.isAI && ball) {
// AI behavior focused on scoring goals
var dx = ball.x - self.x;
var dy = ball.y - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
// Determine opponent's goal position based on AI's starting position
var opponentGoalX = 1024;
var opponentGoalY = self.y < 1366 ? 2650 : 82; // If AI is in top half, target bottom goal, else target top goal
// AI strategy: Always try to get the ball and move it toward opponent's goal
var targetX, targetY;
if (distance < 150) {
// Close to ball - position to kick toward opponent's goal
var ballToGoalX = opponentGoalX - ball.x;
var ballToGoalY = opponentGoalY - ball.y;
var goalDistance = Math.sqrt(ballToGoalX * ballToGoalX + ballToGoalY * ballToGoalY);
// Position behind the ball relative to the goal
var behindBallX = ball.x - ballToGoalX / goalDistance * 60;
var behindBallY = ball.y - ballToGoalY / goalDistance * 60;
targetX = behindBallX;
targetY = behindBallY;
} else {
// Far from ball - move directly toward the ball
targetX = ball.x;
targetY = ball.y;
}
// Simple movement with increased speed toward goal
var moveDistance = Math.sqrt((targetX - self.x) * (targetX - self.x) + (targetY - self.y) * (targetY - self.y));
if (moveDistance > 5) {
var moveSpeed = distance < 150 ? 18 : 15; // Faster speed when close to ball (18) or when chasing (15)
var moveX = (targetX - self.x) / moveDistance * moveSpeed;
var moveY = (targetY - self.y) / moveDistance * moveSpeed;
self.x += moveX;
self.y += moveY;
}
// Goal-oriented kicking
if (distance < 80) {
// Calculate direction to opponent's goal
var ballToGoalX = opponentGoalX - ball.x;
var ballToGoalY = opponentGoalY - ball.y;
var goalDistance = Math.sqrt(ballToGoalX * ballToGoalX + ballToGoalY * ballToGoalY);
// Kick with moderate force toward the goal
var speedMultiplier = 0.04; // Moderate kick strength
var kickForceX = ballToGoalX / goalDistance * speedMultiplier;
var kickForceY = ballToGoalY / goalDistance * speedMultiplier;
ball.kick(kickForceX, kickForceY);
}
}
};
self.down = function (x, y, obj) {
if (!self.isAI) {
self.targetX = x;
self.targetY = y;
}
};
// Move method removed - now handled by global game event handlers
return self;
});
var SettingsButton = Container.expand(function (text, color) {
var self = Container.call(this);
var bg = self.attachAsset('menuButton', {
anchorX: 0.5,
anchorY: 0.5
});
if (color) {
bg.tint = color;
}
var buttonText = new Text2(text, {
size: 40,
fill: 0xFFFFFF
});
buttonText.anchor.set(0.5, 0.5);
self.addChild(buttonText);
self.down = function (x, y, obj) {
tween(self, {
scaleX: 0.95,
scaleY: 0.95
}, {
duration: 100
});
};
self.up = function (x, y, obj) {
tween(self, {
scaleX: 1,
scaleY: 1
}, {
duration: 100
});
if (self.onPress) {
self.onPress();
}
};
return self;
});
var TeamButton = Container.expand(function (teamName, color) {
var self = Container.call(this);
var bg = self.attachAsset('teamButton', {
anchorX: 0.5,
anchorY: 0.5
});
if (color) {
bg.tint = color;
}
var buttonText = new Text2(teamName, {
size: 32,
fill: 0xFFFFFF
});
buttonText.anchor.set(0.5, 0.5);
self.addChild(buttonText);
self.teamName = teamName;
self.teamColor = color;
self.down = function (x, y, obj) {
tween(self, {
scaleX: 0.95,
scaleY: 0.95
}, {
duration: 100
});
};
self.up = function (x, y, obj) {
tween(self, {
scaleX: 1,
scaleY: 1
}, {
duration: 100
});
if (self.onPress) {
self.onPress();
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x004400
});
/****
* Game Code
****/
// Team logo assets
// Game state variables
var gameState = 'mainMenu'; // mainMenu, leagueSelect, teamSelect, playing
var gameMode = ''; // 'twoPlayer' or 'vsAI'
var selectedLeague = '';
var team1Name = '';
var team2Name = '';
var team1Color = 0xFF0000;
var team2Color = 0x0000FF;
var team1Logo = '';
var team2Logo = '';
var score1 = 0;
var score2 = 0;
// Game objects
var ball = null;
var players = [];
var field = null;
var goals = [];
var scoreboard = null;
// Team data
var superLigTeams = [{
name: 'Galatasaray',
color: 0xFFFF00,
logo: 'galatasaray'
}, {
name: 'Fenerbahçe',
color: 0x000080,
logo: 'fenerbahce'
}, {
name: 'Beşiktaş',
color: 0x000000,
logo: 'besiktas'
}, {
name: 'Trabzonspor',
color: 0x800000,
logo: 'trabzonspor'
}, {
name: 'Başakşehir',
color: 0xFF8000,
logo: 'basaksehir'
}];
var laLigaTeams = [{
name: 'Real Madrid',
color: 0xFFFFFF,
logo: 'realmadrid'
}, {
name: 'Barcelona',
color: 0x004A9F,
logo: 'barcelona'
}];
// UI elements
var menuContainer = new Container();
var leagueContainer = new Container();
var teamContainer = new Container();
var settingsContainer = new Container();
// Settings variables
var soundVolume = storage.soundVolume || 1.0;
var settingsOpen = false;
function showMainMenu() {
gameState = 'mainMenu';
clearGame();
var title = new Text2('Futbol Pro', {
size: 80,
fill: 0xFFFFFF
});
title.anchor.set(0.5, 0.5);
title.x = 1024;
title.y = 800;
menuContainer.addChild(title);
var subtitle = new Text2('LaLiga vs Süper Lig', {
size: 50,
fill: 0xCCCCCC
});
subtitle.anchor.set(0.5, 0.5);
subtitle.x = 1024;
subtitle.y = 900;
menuContainer.addChild(subtitle);
var twoPlayerBtn = new MenuButton('İki Kişi Oyna', 0x0066CC);
twoPlayerBtn.x = 1024;
twoPlayerBtn.y = 1200;
twoPlayerBtn.onPress = function () {
gameMode = 'twoPlayer';
showLeagueSelect();
};
menuContainer.addChild(twoPlayerBtn);
var vsAIBtn = new MenuButton('Yapay Zeka ile Oyna', 0x009900);
vsAIBtn.x = 1024;
vsAIBtn.y = 1350;
vsAIBtn.onPress = function () {
gameMode = 'vsAI';
showLeagueSelect();
};
menuContainer.addChild(vsAIBtn);
game.addChild(menuContainer);
}
function showLeagueSelect() {
gameState = 'leagueSelect';
menuContainer.removeChildren();
var title = new Text2('Liga Seçin', {
size: 60,
fill: 0xFFFFFF
});
title.anchor.set(0.5, 0.5);
title.x = 1024;
title.y = 800;
leagueContainer.addChild(title);
var superLigBtn = new MenuButton('Süper Lig', 0xCC0000);
superLigBtn.x = 1024;
superLigBtn.y = 1100;
superLigBtn.onPress = function () {
selectedLeague = 'superlig';
showTeamSelect();
};
leagueContainer.addChild(superLigBtn);
var laLigaBtn = new MenuButton('LaLiga', 0x0066CC);
laLigaBtn.x = 1024;
laLigaBtn.y = 1250;
laLigaBtn.onPress = function () {
selectedLeague = 'laliga';
showTeamSelect();
};
leagueContainer.addChild(laLigaBtn);
game.addChild(leagueContainer);
}
function showTeamSelect() {
gameState = 'teamSelect';
leagueContainer.removeChildren();
var teams = selectedLeague === 'superlig' ? superLigTeams : laLigaTeams;
var selectedTeams = [];
var title = new Text2('Takım Seçin', {
size: 60,
fill: 0xFFFFFF
});
title.anchor.set(0.5, 0.5);
title.x = 1024;
title.y = 400;
teamContainer.addChild(title);
var startY = 600;
for (var i = 0; i < teams.length; i++) {
var teamBtn = new TeamButton(teams[i].name, teams[i].color);
teamBtn.x = 1024;
teamBtn.y = startY + i * 120;
teamBtn.onPress = function (team) {
return function () {
selectedTeams.push(team);
if (selectedTeams.length === 1) {
team1Name = team.name;
team1Color = team.color;
team1Logo = team.logo;
if (gameMode === 'vsAI') {
// Auto-select second team for AI
var aiTeam = teams[Math.floor(Math.random() * teams.length)];
team2Name = aiTeam.name + ' (AI)';
team2Color = aiTeam.color;
team2Logo = aiTeam.logo;
startGame();
} else {
title.setText('İkinci Takımı Seçin');
}
} else if (selectedTeams.length === 2) {
team2Name = team.name;
team2Color = team.color;
team2Logo = team.logo;
startGame();
}
};
}(teams[i]);
teamContainer.addChild(teamBtn);
}
game.addChild(teamContainer);
}
function startGame() {
gameState = 'playing';
teamContainer.removeChildren();
// Create field
field = game.attachAsset('field', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
y: 1366
});
// Create goals
var topGoal = game.attachAsset('goal', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
y: 166
});
var bottomGoal = game.attachAsset('goal', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
y: 2566
});
goals.push(topGoal, bottomGoal);
// Create goal posts for top goal
var topLeftPost = game.attachAsset('goalPost', {
anchorX: 0.5,
anchorY: 0.5,
x: 824,
y: 166
});
var topRightPost = game.attachAsset('goalPost', {
anchorX: 0.5,
anchorY: 0.5,
x: 1224,
y: 166
});
// Create goal posts for bottom goal
var bottomLeftPost = game.attachAsset('goalPost', {
anchorX: 0.5,
anchorY: 0.5,
x: 824,
y: 2566
});
var bottomRightPost = game.attachAsset('goalPost', {
anchorX: 0.5,
anchorY: 0.5,
x: 1224,
y: 2566
});
// Create center line
var centerLine = game.attachAsset('centerLine', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
y: 1366
});
// Create penalty areas
var topPenaltyArea = game.attachAsset('penaltyArea', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
y: 316
});
topPenaltyArea.alpha = 0.3;
var bottomPenaltyArea = game.attachAsset('penaltyArea', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
y: 2416
});
bottomPenaltyArea.alpha = 0.3;
// Create center circle
var centerCircle = game.attachAsset('centerCircle', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
y: 1366
});
centerCircle.alpha = 0.3;
// Create ball
ball = new Ball();
ball.x = 1024;
ball.y = 1366;
game.addChild(ball);
// Create players
var player1 = new Player(team1Logo, team1Color);
player1.x = 1024;
player1.y = 1800;
players.push(player1);
game.addChild(player1);
var player2 = new Player(team2Logo, team2Color);
player2.x = 1024;
player2.y = 900;
if (gameMode === 'vsAI') {
player2.isAI = true;
}
players.push(player2);
game.addChild(player2);
// Create scoreboard
createScoreboard();
score1 = 0;
score2 = 0;
}
function createScoreboard() {
var scoreText = new Text2(team1Name + ' 0 - 0 ' + team2Name, {
size: 40,
fill: 0xFFFFFF
});
scoreText.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreText);
scoreboard = scoreText;
updateScoreboard();
}
function updateScoreboard() {
if (scoreboard) {
scoreboard.setText(team1Name + ' ' + score1 + ' - ' + score2 + ' ' + team2Name);
}
}
function checkGoal() {
if (!ball) return;
// Initialize lastY if not set
if (ball.lastY === undefined) {
ball.lastY = ball.y;
return;
}
// Check top goal (team 1 scores) - ball must enter from front (from below), not sides
if (ball.y < 166 && ball.x > 824 && ball.x < 1224 && ball.y > 91) {
// Only count as goal if ball entered from below (lastY was greater than goal line)
if (ball.lastY >= 166) {
score1++;
var goalSound = LK.getSound('goal');
goalSound.volume = soundVolume;
goalSound.play();
resetBallPosition();
updateScoreboard();
}
}
// Check bottom goal (team 2 scores) - ball must enter from front (from above), not sides
if (ball.y > 2566 && ball.x > 824 && ball.x < 1224 && ball.y < 2641) {
// Only count as goal if ball entered from above (lastY was less than goal line)
if (ball.lastY <= 2566) {
score2++;
var goalSound = LK.getSound('goal');
goalSound.volume = soundVolume;
goalSound.play();
resetBallPosition();
updateScoreboard();
}
}
// Update lastY for next frame
ball.lastY = ball.y;
}
function resetBallPosition() {
if (ball) {
ball.x = 1024;
ball.y = 1366;
ball.velocityX = 0;
ball.velocityY = 0;
ball.lastY = 1366; // Reset lastY tracking
}
}
function clearGame() {
if (ball) {
ball.destroy();
ball = null;
}
for (var i = 0; i < players.length; i++) {
players[i].destroy();
}
players = [];
if (field) {
field.destroy();
field = null;
}
for (var i = 0; i < goals.length; i++) {
goals[i].destroy();
}
goals = [];
if (scoreboard) {
scoreboard.destroy();
scoreboard = null;
}
}
// Global variables for tracking player control
var activePlayer = null;
var player1Active = false;
var player2Active = false;
// Game event handlers
game.down = function (x, y, obj) {
if (gameState !== 'playing') return;
// Determine which player should be controlled based on touch position
if (players.length >= 2) {
var distToPlayer1 = Math.sqrt((x - players[0].x) * (x - players[0].x) + (y - players[0].y) * (y - players[0].y));
var distToPlayer2 = Math.sqrt((x - players[1].x) * (x - players[1].x) + (y - players[1].y) * (y - players[1].y));
if (gameMode === 'twoPlayer') {
// In two player mode, closest player gets controlled
if (distToPlayer1 < distToPlayer2) {
activePlayer = players[0];
player1Active = true;
player2Active = false;
} else {
activePlayer = players[1];
player1Active = false;
player2Active = true;
}
} else {
// In vs AI mode, only player 1 can be controlled
if (!players[0].isAI) {
activePlayer = players[0];
player1Active = true;
player2Active = false;
}
}
if (activePlayer && !activePlayer.isAI) {
activePlayer.targetX = x;
activePlayer.targetY = y;
}
}
};
game.move = function (x, y, obj) {
if (gameState !== 'playing' || !activePlayer || activePlayer.isAI) return;
activePlayer.x = x;
activePlayer.y = y;
// Kick ball if close
if (ball) {
var dx = ball.x - activePlayer.x;
var dy = ball.y - activePlayer.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < 80) {
// Calculate player speed for kick force
var playerSpeed = Math.sqrt(activePlayer.velocityX * activePlayer.velocityX + activePlayer.velocityY * activePlayer.velocityY);
// Base kick force + speed multiplier (min 0.3, max 1.2)
var kickMultiplier = Math.max(0.3, Math.min(1.2, 0.3 + playerSpeed * 0.05));
ball.kick(dx * kickMultiplier, dy * kickMultiplier);
}
}
};
game.up = function (x, y, obj) {
activePlayer = null;
player1Active = false;
player2Active = false;
};
game.update = function () {
if (gameState === 'playing') {
checkGoal();
}
};
// Create menu button for top right corner
var menuBtn = new MenuButton('Menü', 0x0066CC);
menuBtn.x = 1900; // Position in top right, leaving space from edge
menuBtn.y = 150;
menuBtn.onPress = function () {
// Clear current game
clearGame();
// Clear all containers
menuContainer.removeChildren();
leagueContainer.removeChildren();
teamContainer.removeChildren();
// Remove containers from game
if (game.children.indexOf(menuContainer) !== -1) game.removeChild(menuContainer);
if (game.children.indexOf(leagueContainer) !== -1) game.removeChild(leagueContainer);
if (game.children.indexOf(teamContainer) !== -1) game.removeChild(teamContainer);
// Show game mode selection menu
showGameModeMenu();
};
LK.gui.topRight.addChild(menuBtn);
// Create menu button for top left corner
var leftMenuBtn = new MenuButton('Menü', 0x0066CC);
leftMenuBtn.x = 250; // Position in top left, leaving space from edge
leftMenuBtn.y = 150;
leftMenuBtn.onPress = function () {
// Clear current game
clearGame();
// Clear all containers
menuContainer.removeChildren();
leagueContainer.removeChildren();
teamContainer.removeChildren();
// Remove containers from game
if (game.children.indexOf(menuContainer) !== -1) game.removeChild(menuContainer);
if (game.children.indexOf(leagueContainer) !== -1) game.removeChild(leagueContainer);
if (game.children.indexOf(teamContainer) !== -1) game.removeChild(teamContainer);
// Show main menu
showMainMenu();
};
LK.gui.topLeft.addChild(leftMenuBtn);
// Add new function for game mode menu
function showGameModeMenu() {
gameState = 'gameModeSelect';
var title = new Text2('Oyun Modu Seçin', {
size: 60,
fill: 0xFFFFFF
});
title.anchor.set(0.5, 0.5);
title.x = 1024;
title.y = 800;
menuContainer.addChild(title);
var twoPlayerBtn = new MenuButton('İki Kişi Oyna', 0x0066CC);
twoPlayerBtn.x = 1024;
twoPlayerBtn.y = 1200;
twoPlayerBtn.onPress = function () {
gameMode = 'twoPlayer';
showLeagueSelect();
};
menuContainer.addChild(twoPlayerBtn);
var vsAIBtn = new MenuButton('Yapay Zeka ile Oyna', 0x009900);
vsAIBtn.x = 1024;
vsAIBtn.y = 1350;
vsAIBtn.onPress = function () {
gameMode = 'vsAI';
showLeagueSelect();
};
menuContainer.addChild(vsAIBtn);
game.addChild(menuContainer);
}
function showSettings() {
settingsOpen = true;
settingsContainer.removeChildren();
var title = new Text2('Ayarlar', {
size: 60,
fill: 0xFFFFFF
});
title.anchor.set(0.5, 0.5);
title.x = 1024;
title.y = 800;
settingsContainer.addChild(title);
var soundText = new Text2('Ses Seviyesi: ' + Math.round(soundVolume * 100) + '%', {
size: 40,
fill: 0xFFFFFF
});
soundText.anchor.set(0.5, 0.5);
soundText.x = 1024;
soundText.y = 1000;
settingsContainer.addChild(soundText);
var decreaseBtn = new SettingsButton('-', 0xCC0000);
decreaseBtn.x = 800;
decreaseBtn.y = 1150;
decreaseBtn.onPress = function () {
soundVolume = Math.max(0, soundVolume - 0.1);
storage.soundVolume = soundVolume;
soundText.setText('Ses Seviyesi: ' + Math.round(soundVolume * 100) + '%');
};
settingsContainer.addChild(decreaseBtn);
var increaseBtn = new SettingsButton('+', 0x00CC00);
increaseBtn.x = 1248;
increaseBtn.y = 1150;
increaseBtn.onPress = function () {
soundVolume = Math.min(1, soundVolume + 0.1);
storage.soundVolume = soundVolume;
soundText.setText('Ses Seviyesi: ' + Math.round(soundVolume * 100) + '%');
};
settingsContainer.addChild(increaseBtn);
var closeBtn = new SettingsButton('Kapat', 0x666666);
closeBtn.x = 1024;
closeBtn.y = 1400;
closeBtn.onPress = function () {
hideSettings();
};
settingsContainer.addChild(closeBtn);
game.addChild(settingsContainer);
}
function hideSettings() {
settingsOpen = false;
settingsContainer.removeChildren();
if (game.children.indexOf(settingsContainer) !== -1) {
game.removeChild(settingsContainer);
}
}
// Create settings button for bottom right corner
var settingsBtn = new SettingsButton('Ayarlar', 0x666666);
settingsBtn.x = 1800;
settingsBtn.y = 2600;
settingsBtn.onPress = function () {
if (settingsOpen) {
hideSettings();
} else {
showSettings();
}
};
LK.gui.bottomRight.addChild(settingsBtn);
// Initialize game
showMainMenu();
;