/**** * Classes ****/ // Note: This is a simplified simulation of a football game where players move randomly and the ball is kicked around randomly.; var Ball = Container.expand(function () { var self = Container.call(this); // Create and attach a ball asset var ballGraphics = self.attachAsset('ball', { width: 30, height: 30, color: 0xffffff, shape: 'ellipse' }); // Set initial speed self.speedX = 0; self.speedY = 0; // Update ball position based on speed and apply gravity and friction self.update = function () { // Since the game has a top-down view, gravity will not affect the ball's Y-axis movement. // Instead, simulate friction to slow down the ball on the field. var fieldFriction = 0.98; // Simulate friction on the field to slow down the ball // Apply field friction self.speedX *= fieldFriction; self.speedY *= fieldFriction; self.x += self.speedX; self.y += self.speedY; // Boundary checks for walls if (self.x < 0 || self.x > 2048) { self.speedX *= -1; self.x = self.x < 0 ? 0 : 2048; } // Boundary checks for floor and ceiling if (self.y < 0) { self.speedY *= -1; self.y = 0; } else if (self.y > 2732) { // Apply ground friction when ball hits the ground self.speedX *= groundFriction; self.speedY *= -groundFriction; self.y = 2732; } }; // Function to kick the ball self.kick = function (speedX, speedY) { self.speedX = speedX; self.speedY = speedY; }; }); // Assets will be automatically created and loaded by the LK engine based on usage in the game code. // Define a class for the football player var Player = Container.expand(function (teamColor) { var self = Container.call(this); // Create and attach a player asset var playerGraphics = self.attachAsset(teamColor === 0xff0000 ? 'playerTeamA' : 'playerTeamB', { width: 50, height: 50, shape: 'ellipse' }); // Set initial speed and direction self.speedX = 0; self.speedY = 0; // Update player position based on speed self.update = function () { var oldX = self.x; var oldY = self.y; self.x += self.speedX; self.y += self.speedY; // Implement basic boundary checks if (self.x < 0 || self.x > 2048) { self.speedX *= -1; } if (self.y < 0 || self.y > 2732) { self.speedY *= -1; } // Removed player-to-player collision logic to focus on goal-oriented behavior // Check for collision with the ball if (distance(self.x, self.y, ball.x, ball.y) < ball.width) { // If collision detected, move back to old position self.x = oldX; self.y = oldY; } }; // Function to change direction based on behavior self.changeDirection = function () { // Adjust player behavior based on ball proximity to goal for more goal-oriented actions // Ensure goal objects are defined in the global scope before referencing them in player actions var goalForTeam; if (self.assetId === 'playerTeamA') { goalForTeam = typeof goalBottom !== 'undefined' ? goalBottom : { x: 1024 - 100, y: 2732 - 20, width: 200, height: 20 }; } else { goalForTeam = typeof goalTop !== 'undefined' ? goalTop : { x: 1024 - 100, y: 0, width: 200, height: 20 }; } var distanceToGoal = distance(ball.x, ball.y, goalForTeam.x + goalForTeam.width / 2, goalForTeam.y + (self.assetId === 'playerTeamA' ? goalForTeam.height : 0)); if (distanceToGoal < 500) { // Decrease the distance to goal for increased urgency // If the ball is very close to the goal, significantly increase urgency to score self.speedX = (Math.random() * 10 - 5) * (goalForTeam.x + goalForTeam.width / 2 - self.x > 0 ? 1 : -1); // Increase speed and direct towards goal more aggressively self.speedY = (Math.random() * 10 - 5) * (goalForTeam.y + (self.assetId === 'playerTeamA' ? goalForTeam.height : 0) - self.y > 0 ? 1 : -1); } else { // Default behavior based on player role if (self.behavior === 'aggressive') { self.speedX = Math.random() * 4 - 2; // Speed range -2 to 2 for less erratic movement self.speedY = Math.random() * 4 - 2; // Speed range -2 to 2 for less erratic movement } else if (self.behavior === 'defensive') { self.speedX = Math.random() * 3 - 1.5; // Speed range -1.5 to 1.5 for slightly more urgency self.speedY = Math.random() * 3 - 1.5; // Speed range -1.5 to 1.5 for slightly more urgency } } }; // Function to kick the ball if it is close enough self.kickBall = function (ball) { // Adjust the distance check to ensure players must be very close to the ball to kick it var dist = distance(self.x, self.y, ball.x, ball.y); if (dist < 100) { // Increase the distance from which a player can kick the ball to allow for more dynamic play // Players need to be within 50 pixels to kick the ball var goal; if (self.assetId === 'playerTeamA') { goal = goalTop; // Red team aims for the top goal } else { goal = goalBottom; // Blue team aims for the bottom goal } var dx = goal.x + goal.width / 2 - self.x; var dy = goal.y + (self.assetId === 'playerTeamA' ? goal.height : 0) - self.y; var angle = Math.atan2(dy, dx); // Prevent players from scoring in their own goal if (self.assetId === 'playerTeamA' && dy > 0 || self.assetId === 'playerTeamB' && dy < 0) { ball.kick(Math.cos(angle) * 10, Math.sin(angle) * 10); } } // Assign roles to players for more strategic gameplay if (self.assetId === 'playerTeamA') { // Divide Team A into defenders and attackers based on their initial position if (self.y < field.height / 2) { // Players on their own half are defenders self.role = 'defender'; self.behavior = 'defensive'; } else { // Players on the opponent's half are attackers self.role = 'attacker'; self.behavior = 'aggressive'; } } else if (self.assetId === 'playerTeamB') { // Divide Team B into defenders and attackers based on their initial position if (self.y > field.height / 2) { // Players on their own half are defenders self.role = 'defender'; self.behavior = 'defensive'; } else { // Players on the opponent's half are attackers self.role = 'attacker'; self.behavior = 'aggressive'; } } // Defenders focus on protecting their goal and clearing the ball from their half // Attackers focus on moving forward to score or assist in scoring }; // Function to pass the ball to a teammate with improved accuracy and decision-making self.passBall = function (ball, teammate) { var dist = distance(self.x, self.y, ball.x, ball.y); if (dist < 150) { // Increased range for passing var dx = teammate.x - self.x; var dy = teammate.y - self.y; var angle = Math.atan2(dy, dx); // Adjust kick strength based on distance to teammate for more realistic passes var kickStrength = Math.min(10, 5 + dist / 30); ball.kick(Math.cos(angle) * kickStrength, Math.sin(angle) * kickStrength); } }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x778899 // Init game with custom background color }); /**** * Game Code ****/ // Display the background image var background = game.addChild(LK.getAsset('background', { x: game.width / 2, y: game.height / 2, anchorX: 0.5, anchorY: 0.5 })); background.scaleX = game.width / background.width; background.scaleY = game.height / background.height; // Define the match duration (in seconds) var matchDuration = 180; // 3 minutes // Define the football field's dimensions and boundaries var field = { width: 2048, height: 2732, goalWidth: 200, goalHeight: 20, penaltyAreaWidth: 400, penaltyAreaHeight: 600, centerCircleRadius: 300 }; // Function to calculate the distance between two points function distance(x1, y1, x2, y2) { var dx = x2 - x1; var dy = y2 - y1; return Math.sqrt(dx * dx + dy * dy); } var teamA = []; var teamB = []; var ball = game.addChild(new Ball()); // Populate teams for (var i = 0; i < 11; i++) { teamA.push(game.addChild(new Player(0xff0000))); // Team A in red teamB.push(game.addChild(new Player(0x0000ff))); // Team B in blue } // Assign different behaviors to the teams teamA.forEach(function (player) { return player.behavior = 'aggressive'; }); teamB.forEach(function (player) { return player.behavior = 'defensive'; }); // Position teams and ball teamA.forEach(function (player, index) { player.x = 300 + index * 150; player.y = 600; player.changeDirection(); }); teamB.forEach(function (player, index) { player.x = 300 + index * 150; player.y = 2100; player.changeDirection(); }); ball.x = 1024; ball.y = 1366; // Create and position goal posts var goalTop = game.addChild(LK.getAsset('goal', { x: 1024 - 100, y: 0 })); var goalBottom = game.addChild(LK.getAsset('goal', { x: 1024 - 100, y: 2732 - 20 })); // Game update function game.update = function () { // Randomly change player directions if (LK.ticks % 120 == 0) { // Every 2 seconds teamA.forEach(function (player) { player.changeDirection(); }); teamB.forEach(function (player) { player.changeDirection(); }); } // Check if a goal is scored if (ball.intersects(goalTop) && ball.y + ball.height <= goalTop.y + goalTop.height) { // Team B scores LK.setScore(LK.getScore() + 1); // Restart play ball.x = field.width / 2; ball.y = field.height / 2; ball.speedX = 0; ball.speedY = 0; // Update team scores teamBScore++; } else if (ball.intersects(goalBottom) && ball.y >= goalBottom.y) { // Team A scores LK.setScore(LK.getScore() + 1); // Restart play ball.x = field.width / 2; ball.y = field.height / 2; ball.speedX = 0; ball.speedY = 0; // Update team scores teamAScore++; } // Enhanced logic for players to decide between kicking or passing the ball teamA.concat(teamB).forEach(function (player) { // Find closest teammate for potential pass var teammates = (player.assetId === 'playerTeamA' ? teamA : teamB).filter(function (teammate) { return teammate !== player; }); var closestTeammate = teammates.reduce(function (closest, teammate) { var currentDist = distance(player.x, player.y, teammate.x, teammate.y); if (currentDist < closest.dist) { return { teammate: teammate, dist: currentDist }; } return closest; }, { teammate: null, dist: Infinity }); // Decide to pass if a teammate is within 200 pixels and not directly aiming for the goal if (closestTeammate.teammate && closestTeammate.dist < 200) { player.passBall(ball, closestTeammate.teammate); } else { // Kick towards the goal if no suitable pass options player.kickBall(ball); } }); // Boundary checks for the players teamA.concat(teamB).forEach(function (player) { if (player.x < 0) { player.x = 0; } if (player.x > field.width) { player.x = field.width; } if (player.y < 0) { player.y = 0; } if (player.y > field.height) { player.y = field.height; } }); // Check if the ball is out of bounds if (ball.x < 0 || ball.x > field.width) { // Ball crossed the sideline, throw-in ball.x = ball.x < 0 ? 0 : field.width; ball.y = Math.random() * field.height; // Random y-coordinate for throw-in ball.speedX = 0; ball.speedY = 0; } else if (ball.y < 0) { // Ball crossed the goal line on team A's side, check for goal or goal kick if (ball.x > goalTop.x && ball.x < goalTop.x + goalTop.width) { // Goal for team B LK.setScore(LK.getScore() + 1); } // Restart play with goal kick ball.x = field.width / 2; ball.y = field.height / 4; // Quarter way down the field for team A's goal kick ball.speedX = 0; ball.speedY = 0; } else if (ball.y > field.height) { // Ball crossed the goal line on team B's side, check for goal or goal kick if (ball.x > goalBottom.x && ball.x < goalBottom.x + goalBottom.width) { // Goal for team A LK.setScore(LK.getScore() + 1); } // Restart play with goal kick ball.x = field.width / 2; ball.y = 3 * field.height / 4; // Quarter way up the field for team B's goal kick ball.speedX = 0; ball.speedY = 0; } // Update match duration matchDuration -= 1 / 60; // Subtract the time passed since the last frame (1/60th of a second) if (matchDuration <= 0) { // End the match when the duration reaches 0 LK.showGameOver(); } }; // Note: This is a simplified simulation of a football game where players move randomly and the ball is kicked around randomly.
/****
* Classes
****/
// Note: This is a simplified simulation of a football game where players move randomly and the ball is kicked around randomly.;
var Ball = Container.expand(function () {
var self = Container.call(this);
// Create and attach a ball asset
var ballGraphics = self.attachAsset('ball', {
width: 30,
height: 30,
color: 0xffffff,
shape: 'ellipse'
});
// Set initial speed
self.speedX = 0;
self.speedY = 0;
// Update ball position based on speed and apply gravity and friction
self.update = function () {
// Since the game has a top-down view, gravity will not affect the ball's Y-axis movement.
// Instead, simulate friction to slow down the ball on the field.
var fieldFriction = 0.98; // Simulate friction on the field to slow down the ball
// Apply field friction
self.speedX *= fieldFriction;
self.speedY *= fieldFriction;
self.x += self.speedX;
self.y += self.speedY;
// Boundary checks for walls
if (self.x < 0 || self.x > 2048) {
self.speedX *= -1;
self.x = self.x < 0 ? 0 : 2048;
}
// Boundary checks for floor and ceiling
if (self.y < 0) {
self.speedY *= -1;
self.y = 0;
} else if (self.y > 2732) {
// Apply ground friction when ball hits the ground
self.speedX *= groundFriction;
self.speedY *= -groundFriction;
self.y = 2732;
}
};
// Function to kick the ball
self.kick = function (speedX, speedY) {
self.speedX = speedX;
self.speedY = speedY;
};
});
// Assets will be automatically created and loaded by the LK engine based on usage in the game code.
// Define a class for the football player
var Player = Container.expand(function (teamColor) {
var self = Container.call(this);
// Create and attach a player asset
var playerGraphics = self.attachAsset(teamColor === 0xff0000 ? 'playerTeamA' : 'playerTeamB', {
width: 50,
height: 50,
shape: 'ellipse'
});
// Set initial speed and direction
self.speedX = 0;
self.speedY = 0;
// Update player position based on speed
self.update = function () {
var oldX = self.x;
var oldY = self.y;
self.x += self.speedX;
self.y += self.speedY;
// Implement basic boundary checks
if (self.x < 0 || self.x > 2048) {
self.speedX *= -1;
}
if (self.y < 0 || self.y > 2732) {
self.speedY *= -1;
}
// Removed player-to-player collision logic to focus on goal-oriented behavior
// Check for collision with the ball
if (distance(self.x, self.y, ball.x, ball.y) < ball.width) {
// If collision detected, move back to old position
self.x = oldX;
self.y = oldY;
}
};
// Function to change direction based on behavior
self.changeDirection = function () {
// Adjust player behavior based on ball proximity to goal for more goal-oriented actions
// Ensure goal objects are defined in the global scope before referencing them in player actions
var goalForTeam;
if (self.assetId === 'playerTeamA') {
goalForTeam = typeof goalBottom !== 'undefined' ? goalBottom : {
x: 1024 - 100,
y: 2732 - 20,
width: 200,
height: 20
};
} else {
goalForTeam = typeof goalTop !== 'undefined' ? goalTop : {
x: 1024 - 100,
y: 0,
width: 200,
height: 20
};
}
var distanceToGoal = distance(ball.x, ball.y, goalForTeam.x + goalForTeam.width / 2, goalForTeam.y + (self.assetId === 'playerTeamA' ? goalForTeam.height : 0));
if (distanceToGoal < 500) {
// Decrease the distance to goal for increased urgency
// If the ball is very close to the goal, significantly increase urgency to score
self.speedX = (Math.random() * 10 - 5) * (goalForTeam.x + goalForTeam.width / 2 - self.x > 0 ? 1 : -1); // Increase speed and direct towards goal more aggressively
self.speedY = (Math.random() * 10 - 5) * (goalForTeam.y + (self.assetId === 'playerTeamA' ? goalForTeam.height : 0) - self.y > 0 ? 1 : -1);
} else {
// Default behavior based on player role
if (self.behavior === 'aggressive') {
self.speedX = Math.random() * 4 - 2; // Speed range -2 to 2 for less erratic movement
self.speedY = Math.random() * 4 - 2; // Speed range -2 to 2 for less erratic movement
} else if (self.behavior === 'defensive') {
self.speedX = Math.random() * 3 - 1.5; // Speed range -1.5 to 1.5 for slightly more urgency
self.speedY = Math.random() * 3 - 1.5; // Speed range -1.5 to 1.5 for slightly more urgency
}
}
};
// Function to kick the ball if it is close enough
self.kickBall = function (ball) {
// Adjust the distance check to ensure players must be very close to the ball to kick it
var dist = distance(self.x, self.y, ball.x, ball.y);
if (dist < 100) {
// Increase the distance from which a player can kick the ball to allow for more dynamic play
// Players need to be within 50 pixels to kick the ball
var goal;
if (self.assetId === 'playerTeamA') {
goal = goalTop; // Red team aims for the top goal
} else {
goal = goalBottom; // Blue team aims for the bottom goal
}
var dx = goal.x + goal.width / 2 - self.x;
var dy = goal.y + (self.assetId === 'playerTeamA' ? goal.height : 0) - self.y;
var angle = Math.atan2(dy, dx);
// Prevent players from scoring in their own goal
if (self.assetId === 'playerTeamA' && dy > 0 || self.assetId === 'playerTeamB' && dy < 0) {
ball.kick(Math.cos(angle) * 10, Math.sin(angle) * 10);
}
}
// Assign roles to players for more strategic gameplay
if (self.assetId === 'playerTeamA') {
// Divide Team A into defenders and attackers based on their initial position
if (self.y < field.height / 2) {
// Players on their own half are defenders
self.role = 'defender';
self.behavior = 'defensive';
} else {
// Players on the opponent's half are attackers
self.role = 'attacker';
self.behavior = 'aggressive';
}
} else if (self.assetId === 'playerTeamB') {
// Divide Team B into defenders and attackers based on their initial position
if (self.y > field.height / 2) {
// Players on their own half are defenders
self.role = 'defender';
self.behavior = 'defensive';
} else {
// Players on the opponent's half are attackers
self.role = 'attacker';
self.behavior = 'aggressive';
}
}
// Defenders focus on protecting their goal and clearing the ball from their half
// Attackers focus on moving forward to score or assist in scoring
};
// Function to pass the ball to a teammate with improved accuracy and decision-making
self.passBall = function (ball, teammate) {
var dist = distance(self.x, self.y, ball.x, ball.y);
if (dist < 150) {
// Increased range for passing
var dx = teammate.x - self.x;
var dy = teammate.y - self.y;
var angle = Math.atan2(dy, dx);
// Adjust kick strength based on distance to teammate for more realistic passes
var kickStrength = Math.min(10, 5 + dist / 30);
ball.kick(Math.cos(angle) * kickStrength, Math.sin(angle) * kickStrength);
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x778899 // Init game with custom background color
});
/****
* Game Code
****/
// Display the background image
var background = game.addChild(LK.getAsset('background', {
x: game.width / 2,
y: game.height / 2,
anchorX: 0.5,
anchorY: 0.5
}));
background.scaleX = game.width / background.width;
background.scaleY = game.height / background.height;
// Define the match duration (in seconds)
var matchDuration = 180; // 3 minutes
// Define the football field's dimensions and boundaries
var field = {
width: 2048,
height: 2732,
goalWidth: 200,
goalHeight: 20,
penaltyAreaWidth: 400,
penaltyAreaHeight: 600,
centerCircleRadius: 300
};
// Function to calculate the distance between two points
function distance(x1, y1, x2, y2) {
var dx = x2 - x1;
var dy = y2 - y1;
return Math.sqrt(dx * dx + dy * dy);
}
var teamA = [];
var teamB = [];
var ball = game.addChild(new Ball());
// Populate teams
for (var i = 0; i < 11; i++) {
teamA.push(game.addChild(new Player(0xff0000))); // Team A in red
teamB.push(game.addChild(new Player(0x0000ff))); // Team B in blue
}
// Assign different behaviors to the teams
teamA.forEach(function (player) {
return player.behavior = 'aggressive';
});
teamB.forEach(function (player) {
return player.behavior = 'defensive';
});
// Position teams and ball
teamA.forEach(function (player, index) {
player.x = 300 + index * 150;
player.y = 600;
player.changeDirection();
});
teamB.forEach(function (player, index) {
player.x = 300 + index * 150;
player.y = 2100;
player.changeDirection();
});
ball.x = 1024;
ball.y = 1366;
// Create and position goal posts
var goalTop = game.addChild(LK.getAsset('goal', {
x: 1024 - 100,
y: 0
}));
var goalBottom = game.addChild(LK.getAsset('goal', {
x: 1024 - 100,
y: 2732 - 20
}));
// Game update function
game.update = function () {
// Randomly change player directions
if (LK.ticks % 120 == 0) {
// Every 2 seconds
teamA.forEach(function (player) {
player.changeDirection();
});
teamB.forEach(function (player) {
player.changeDirection();
});
}
// Check if a goal is scored
if (ball.intersects(goalTop) && ball.y + ball.height <= goalTop.y + goalTop.height) {
// Team B scores
LK.setScore(LK.getScore() + 1);
// Restart play
ball.x = field.width / 2;
ball.y = field.height / 2;
ball.speedX = 0;
ball.speedY = 0;
// Update team scores
teamBScore++;
} else if (ball.intersects(goalBottom) && ball.y >= goalBottom.y) {
// Team A scores
LK.setScore(LK.getScore() + 1);
// Restart play
ball.x = field.width / 2;
ball.y = field.height / 2;
ball.speedX = 0;
ball.speedY = 0;
// Update team scores
teamAScore++;
}
// Enhanced logic for players to decide between kicking or passing the ball
teamA.concat(teamB).forEach(function (player) {
// Find closest teammate for potential pass
var teammates = (player.assetId === 'playerTeamA' ? teamA : teamB).filter(function (teammate) {
return teammate !== player;
});
var closestTeammate = teammates.reduce(function (closest, teammate) {
var currentDist = distance(player.x, player.y, teammate.x, teammate.y);
if (currentDist < closest.dist) {
return {
teammate: teammate,
dist: currentDist
};
}
return closest;
}, {
teammate: null,
dist: Infinity
});
// Decide to pass if a teammate is within 200 pixels and not directly aiming for the goal
if (closestTeammate.teammate && closestTeammate.dist < 200) {
player.passBall(ball, closestTeammate.teammate);
} else {
// Kick towards the goal if no suitable pass options
player.kickBall(ball);
}
});
// Boundary checks for the players
teamA.concat(teamB).forEach(function (player) {
if (player.x < 0) {
player.x = 0;
}
if (player.x > field.width) {
player.x = field.width;
}
if (player.y < 0) {
player.y = 0;
}
if (player.y > field.height) {
player.y = field.height;
}
});
// Check if the ball is out of bounds
if (ball.x < 0 || ball.x > field.width) {
// Ball crossed the sideline, throw-in
ball.x = ball.x < 0 ? 0 : field.width;
ball.y = Math.random() * field.height; // Random y-coordinate for throw-in
ball.speedX = 0;
ball.speedY = 0;
} else if (ball.y < 0) {
// Ball crossed the goal line on team A's side, check for goal or goal kick
if (ball.x > goalTop.x && ball.x < goalTop.x + goalTop.width) {
// Goal for team B
LK.setScore(LK.getScore() + 1);
}
// Restart play with goal kick
ball.x = field.width / 2;
ball.y = field.height / 4; // Quarter way down the field for team A's goal kick
ball.speedX = 0;
ball.speedY = 0;
} else if (ball.y > field.height) {
// Ball crossed the goal line on team B's side, check for goal or goal kick
if (ball.x > goalBottom.x && ball.x < goalBottom.x + goalBottom.width) {
// Goal for team A
LK.setScore(LK.getScore() + 1);
}
// Restart play with goal kick
ball.x = field.width / 2;
ball.y = 3 * field.height / 4; // Quarter way up the field for team B's goal kick
ball.speedX = 0;
ball.speedY = 0;
}
// Update match duration
matchDuration -= 1 / 60; // Subtract the time passed since the last frame (1/60th of a second)
if (matchDuration <= 0) {
// End the match when the duration reaches 0
LK.showGameOver();
}
};
// Note: This is a simplified simulation of a football game where players move randomly and the ball is kicked around randomly.
ЛОГОТИП НА ИГРОКЕ СВЕРХУ ФУТБОЛЬНОГО КЛУБА СПАРТАК. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
ЛОГОТИП НА ИГРОКЕ СВЕРХУ ФУТБОЛЬНОГО КЛУБА ЗЕНИТ. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
МЯЧ ФУТБОЛЬНЫЙ. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
ФУТБОЛЬНОЕ ПОЛЕ С РАЗМЕТКАМИ С ВИДУ СВЕРХУ. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.