User prompt
Make a scoreboard for the goal count
User prompt
Add 2 mutual soccer goal
Code edit (1 edits merged)
Please save this source code
User prompt
Do it same game again
User prompt
Football Flick: Pass & Shoot
User prompt
Make a football game with shot and pass buttons and add a joystick
Initial prompt
Make a football game
/**** * Classes ****/ // Ball class var Ball = Container.expand(function () { var self = Container.call(this); var gfx = self.attachAsset('ball', { anchorX: 0.5, anchorY: 0.5 }); self.radius = gfx.width * 0.5; self.vx = 0; self.vy = 0; self.update = function () { // Ball moves down by default if (self.vy === 0) { self.vy = 8; } self.x += self.vx; self.y += self.vy; // Friction self.vx *= 0.96; self.vy *= 0.96; // Clamp to field if (self.x < 100 + self.radius) { self.x = 100 + self.radius; } if (self.x > 2048 - 100 - self.radius) { self.x = 2048 - 100 - self.radius; } if (self.y < 200 + self.radius) { self.y = 200 + self.radius; } if (self.y > 2732 - 200 - self.radius) { self.y = 2732 - 200 - self.radius; } }; return self; }); // --- UI: Joystick and Buttons --- // Joystick // --- FOOTBALL GAME MAIN OBJECTS --- // Footballer class var Footballer = Container.expand(function () { var self = Container.call(this); var gfx = self.attachAsset('footballer', { anchorX: 0.5, anchorY: 0.5 }); self.radius = gfx.width * 0.5; self.hasBall = true; self.update = function () {}; return self; }); // Soccer Goal class var SoccerGoal = Container.expand(function () { var self = Container.call(this); // width: 400px, height: 40px, color: white, box var gfx = self.attachAsset('centerCircle', { width: 400, height: 40, color: 0xffffff, anchorX: 0.5, anchorY: 0.5 }); self.width = 400; self.height = 40; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 }); /**** * Game Code ****/ // --- FOOTBALL FIELD MAP --- // Draw green field background var fieldBg = LK.getAsset('centerCircle', { width: 2048 - 200, height: 2732 - 400, color: 0x2e8b57, anchorX: 0.5, anchorY: 0.5, x: 2048 / 2, y: 2732 / 2 }); game.addChildAt(fieldBg, 0); // Draw center line var centerLine = LK.getAsset('centerCircle', { width: 2048 - 200, height: 16, color: 0xffffff, anchorX: 0.5, anchorY: 0.5, x: 2048 / 2, y: 2732 / 2 }); game.addChild(centerLine); // Draw center circle var centerCircle = LK.getAsset('centerCircle', { width: 400, height: 400, color: 0xffffff, anchorX: 0.5, anchorY: 0.5, x: 2048 / 2, y: 2732 / 2 }); centerCircle.alpha = 0.25; game.addChild(centerCircle); // Draw penalty boxes (top and bottom) var penaltyBoxTop = LK.getAsset('centerCircle', { width: 800, height: 180, color: 0xffffff, anchorX: 0.5, anchorY: 0, x: 2048 / 2, y: 120 }); penaltyBoxTop.alpha = 0.18; game.addChild(penaltyBoxTop); var penaltyBoxBottom = LK.getAsset('centerCircle', { width: 800, height: 180, color: 0xffffff, anchorX: 0.5, anchorY: 1, x: 2048 / 2, y: 2732 - 120 }); penaltyBoxBottom.alpha = 0.18; game.addChild(penaltyBoxBottom); // --- UI: Joystick and Buttons --- // Joystick var joystick = LK.gui.bottomLeft.addChild(LK.getAsset('joystick_base', { anchorX: 0.5, anchorY: 0.5, x: 220, y: -220, scaleX: 2, scaleY: 2 })); var joystickKnob = LK.gui.bottomLeft.addChild(LK.getAsset('joystick_knob', { anchorX: 0.5, anchorY: 0.5, x: 220, y: -220, scaleX: 1.2, scaleY: 1.2 })); var joystickActive = false; var joystickStart = { x: 0, y: 0 }; var joystickDir = { x: 0, y: 0 }; // Pass Button var passBtn = LK.gui.bottomRight.addChild(LK.getAsset('pass_btn', { anchorX: 0.5, anchorY: 0.5, x: -220, y: -320, scaleX: 2, scaleY: 2 })); // Shoot Button var shootBtn = LK.gui.bottomRight.addChild(LK.getAsset('shoot_btn', { anchorX: 0.5, anchorY: 0.5, x: -220, y: -120, scaleX: 2, scaleY: 2 })); // --- GAME OBJECTS --- // Left Goal (bottom) var leftGoal = new SoccerGoal(); leftGoal.x = 2048 / 2; leftGoal.y = 2732 - 120; game.addChild(leftGoal); // Right Goal (top) var rightGoal = new SoccerGoal(); rightGoal.x = 2048 / 2; rightGoal.y = 120; game.addChild(rightGoal); var footballer = new Footballer(); footballer.x = 2048 / 2; footballer.y = 2732 - 500; game.addChild(footballer); var ball = new Ball(); ball.x = footballer.x; ball.y = footballer.y - footballer.radius - 40; game.addChild(ball); // --- GAME STATE --- var canShoot = true; var canPass = true; // --- SCOREBOARD --- var leftScore = 0; var rightScore = 0; // Create a group container for the scoreboard and goals text var scoreboardGroup = new Container(); // Scoreboard asset (background) var scoreboardBg = LK.getAsset('centerCircle', { width: 600, height: 180, color: 0x222222, anchorX: 0.5, anchorY: 0, alpha: 0.7 }); scoreboardBg.x = 0; scoreboardBg.y = 0; scoreboardGroup.addChild(scoreboardBg); // Goals text label var goalsLabel = new Text2('GOALS', { size: 60, fill: 0xFFD700 }); goalsLabel.anchor.set(0.5, 0); goalsLabel.x = 0; goalsLabel.y = 10; scoreboardGroup.addChild(goalsLabel); // Score text on top of scoreboard asset var scoreTxt = new Text2('0 : 0', { size: 120, fill: 0xFFFFFF }); scoreTxt.anchor.set(0.5, 0); scoreTxt.x = 0; scoreTxt.y = 70; scoreboardGroup.addChild(scoreTxt); // Add the group to the GUI top center LK.gui.top.addChild(scoreboardGroup); // Position the group at top center scoreboardGroup.x = 0; scoreboardGroup.y = 0; function updateScoreboard() { scoreTxt.setText(leftScore + " : " + rightScore); } updateScoreboard(); // --- JOYSTICK EVENTS --- joystickKnob.down = function (x, y, obj) { joystickActive = true; joystickStart.x = x; joystickStart.y = y; }; joystickKnob.up = function (x, y, obj) { joystickActive = false; joystickKnob.x = joystick.x; joystickKnob.y = joystick.y; joystickDir.x = 0; joystickDir.y = 0; }; joystickKnob.move = function (x, y, obj) { if (!joystickActive) { return; } var dx = x - joystick.x; var dy = y - joystick.y; var dist = Math.sqrt(dx * dx + dy * dy); var maxDist = 120; if (dist > maxDist) { dx = dx * maxDist / dist; dy = dy * maxDist / dist; } joystickKnob.x = joystick.x + dx; joystickKnob.y = joystick.y + dy; joystickDir.x = dx / maxDist; joystickDir.y = dy / maxDist; }; // --- BUTTON EVENTS --- passBtn.down = function (x, y, obj) { if (footballer.hasBall && canPass) { // Pass: direction depends on joystick var passSpeed = 40; var dirY = joystickDir.y; // If joystick is pushed down, pass down, else pass up if (dirY > 0.5) { ball.vx = 0; ball.vy = passSpeed; } else { ball.vx = 0; ball.vy = -passSpeed; } footballer.hasBall = false; canPass = false; LK.setTimeout(function () { canPass = true; }, 400); } }; shootBtn.down = function (x, y, obj) { if (footballer.hasBall && canShoot) { // Shoot: direction depends on joystick var shootSpeed = 80; var dirY = joystickDir.y; // If joystick is pushed down, shoot down, else shoot up if (dirY > 0.5) { ball.vx = 0; ball.vy = shootSpeed; } else if (dirY < -0.5) { ball.vx = 0; ball.vy = -shootSpeed; } else { // If joystick is not pushed up or down, shoot forward (up by default) ball.vx = 0; ball.vy = -shootSpeed; } footballer.hasBall = false; canShoot = false; LK.setTimeout(function () { canShoot = true; }, 800); } }; // --- GAME MOVE HANDLER --- game.move = function (x, y, obj) { // Track last move position for touch detection game.lastMoveX = x; game.lastMoveY = y; // Forward joystick events if (joystickActive) { joystickKnob.move(x, y, obj); } }; // --- GAME UPDATE --- game.update = function () { // Move footballer with joystick if (joystickDir.x !== 0 || joystickDir.y !== 0) { var speed = 18; footballer.x += joystickDir.x * speed; footballer.y += joystickDir.y * speed; // Clamp to field if (footballer.x < 100 + footballer.radius) { footballer.x = 100 + footballer.radius; } if (footballer.x > 2048 - 100 - footballer.radius) { footballer.x = 2048 - 100 - footballer.radius; } if (footballer.y < 200 + footballer.radius) { footballer.y = 200 + footballer.radius; } if (footballer.y > 2732 - 200 - footballer.radius) { footballer.y = 2732 - 200 - footballer.radius; } } // Ball always moves independently // If footballer does NOT have the ball, ball moves by its own update if (!footballer.hasBall) { ball.update(); } else { // If footballer has the ball, allow left/right and up/down movement of the ball with joystick // Ball can move in front of or below the footballer, depending on joystick direction var offsetX = joystickDir.x * 80; // max 80px left/right var offsetY; if (joystickDir.y > 0.5) { // If joystick is pushed down, let the ball go below the footballer offsetY = -(footballer.radius + 40 + joystickDir.y * 80); ball.x = footballer.x + offsetX; ball.y = footballer.y - offsetY; } else { // Default: ball in front of footballer, allow up/down movement offsetY = footballer.radius + 40 - joystickDir.y * 80; ball.x = footballer.x + offsetX; ball.y = footballer.y - offsetY; } // Ball velocity is zero while attached ball.vx = 0; ball.vy = 0; } // Regain possession if close var dx = footballer.x - ball.x; var dy = footballer.y - ball.y; var dist = Math.sqrt(dx * dx + dy * dy); if (dist < footballer.radius + ball.radius + 20 && !footballer.hasBall) { footballer.hasBall = true; } else if (dist < footballer.radius + ball.radius + 20 && footballer.hasBall === false) { // This block is for clarity, but will never be reached since hasBall is set above. } // --- BALL TOUCH DETECTION (not by footballer) --- if (ball.lastWasTouched === undefined) ball.lastWasTouched = false; var isTouched = false; // Defensive: ignore, as we don't get here from a down event if (!footballer.hasBall) { // Check if the ball is being touched (by a finger, not by footballer) // We use LK.gui or game.move events for touch, but since we don't have a direct event, let's check if the ball is under the last move position // We'll use the last known move position if available if (typeof game.lastMoveX !== "undefined" && typeof game.lastMoveY !== "undefined") { var bx = ball.x, by = ball.y, br = ball.radius; var mx = game.lastMoveX, my = game.lastMoveY; var distTouch = Math.sqrt((bx - mx) * (bx - mx) + (by - my) * (by - my)); if (distTouch < br) { isTouched = true; } } } if (!ball.lastWasTouched && isTouched) { // Only add point if footballer does NOT have the ball rightScore += 1; updateScoreboard(); } ball.lastWasTouched = isTouched; // --- GOAL DETECTION --- // Add 1 point to right scoreboard when ball touches the up side goal (top edge) if (ball.lastY !== undefined && ( // Ball enters rightGoal (top) from below ball.lastY > rightGoal.y && ball.y <= rightGoal.y || // Ball touches the top edge of the field ball.lastY >= 200 + ball.radius && ball.y < 200 + ball.radius) && Math.abs(ball.x - rightGoal.x) < rightGoal.width * 0.5 && footballer.hasBall === false) { rightScore += 1; updateScoreboard(); // Reset ball and footballer footballer.x = 2048 / 2; footballer.y = 2732 - 500; ball.x = footballer.x; ball.y = footballer.y - footballer.radius - 40; footballer.hasBall = true; ball.vx = 0; ball.vy = 0; } // Add 1 point to left scoreboard when ball touches the bottom goal net (bottom edge) if (ball.lastY !== undefined && ( // Ball enters leftGoal (bottom) from above ball.lastY < leftGoal.y && ball.y >= leftGoal.y || // Ball touches the bottom edge of the field ball.lastY <= 2732 - 200 - ball.radius && ball.y > 2732 - 200 - ball.radius) && Math.abs(ball.x - leftGoal.x) < leftGoal.width * 0.5 && footballer.hasBall === false) { leftScore += 1; updateScoreboard(); // Reset ball and footballer footballer.x = 2048 / 2; footballer.y = 2732 - 500; ball.x = footballer.x; ball.y = footballer.y - footballer.radius - 40; footballer.hasBall = true; ball.vx = 0; ball.vy = 0; } ball.lastY = ball.y; };
/****
* Classes
****/
// Ball class
var Ball = Container.expand(function () {
var self = Container.call(this);
var gfx = self.attachAsset('ball', {
anchorX: 0.5,
anchorY: 0.5
});
self.radius = gfx.width * 0.5;
self.vx = 0;
self.vy = 0;
self.update = function () {
// Ball moves down by default
if (self.vy === 0) {
self.vy = 8;
}
self.x += self.vx;
self.y += self.vy;
// Friction
self.vx *= 0.96;
self.vy *= 0.96;
// Clamp to field
if (self.x < 100 + self.radius) {
self.x = 100 + self.radius;
}
if (self.x > 2048 - 100 - self.radius) {
self.x = 2048 - 100 - self.radius;
}
if (self.y < 200 + self.radius) {
self.y = 200 + self.radius;
}
if (self.y > 2732 - 200 - self.radius) {
self.y = 2732 - 200 - self.radius;
}
};
return self;
});
// --- UI: Joystick and Buttons ---
// Joystick
// --- FOOTBALL GAME MAIN OBJECTS ---
// Footballer class
var Footballer = Container.expand(function () {
var self = Container.call(this);
var gfx = self.attachAsset('footballer', {
anchorX: 0.5,
anchorY: 0.5
});
self.radius = gfx.width * 0.5;
self.hasBall = true;
self.update = function () {};
return self;
});
// Soccer Goal class
var SoccerGoal = Container.expand(function () {
var self = Container.call(this);
// width: 400px, height: 40px, color: white, box
var gfx = self.attachAsset('centerCircle', {
width: 400,
height: 40,
color: 0xffffff,
anchorX: 0.5,
anchorY: 0.5
});
self.width = 400;
self.height = 40;
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000
});
/****
* Game Code
****/
// --- FOOTBALL FIELD MAP ---
// Draw green field background
var fieldBg = LK.getAsset('centerCircle', {
width: 2048 - 200,
height: 2732 - 400,
color: 0x2e8b57,
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2,
y: 2732 / 2
});
game.addChildAt(fieldBg, 0);
// Draw center line
var centerLine = LK.getAsset('centerCircle', {
width: 2048 - 200,
height: 16,
color: 0xffffff,
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2,
y: 2732 / 2
});
game.addChild(centerLine);
// Draw center circle
var centerCircle = LK.getAsset('centerCircle', {
width: 400,
height: 400,
color: 0xffffff,
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2,
y: 2732 / 2
});
centerCircle.alpha = 0.25;
game.addChild(centerCircle);
// Draw penalty boxes (top and bottom)
var penaltyBoxTop = LK.getAsset('centerCircle', {
width: 800,
height: 180,
color: 0xffffff,
anchorX: 0.5,
anchorY: 0,
x: 2048 / 2,
y: 120
});
penaltyBoxTop.alpha = 0.18;
game.addChild(penaltyBoxTop);
var penaltyBoxBottom = LK.getAsset('centerCircle', {
width: 800,
height: 180,
color: 0xffffff,
anchorX: 0.5,
anchorY: 1,
x: 2048 / 2,
y: 2732 - 120
});
penaltyBoxBottom.alpha = 0.18;
game.addChild(penaltyBoxBottom);
// --- UI: Joystick and Buttons ---
// Joystick
var joystick = LK.gui.bottomLeft.addChild(LK.getAsset('joystick_base', {
anchorX: 0.5,
anchorY: 0.5,
x: 220,
y: -220,
scaleX: 2,
scaleY: 2
}));
var joystickKnob = LK.gui.bottomLeft.addChild(LK.getAsset('joystick_knob', {
anchorX: 0.5,
anchorY: 0.5,
x: 220,
y: -220,
scaleX: 1.2,
scaleY: 1.2
}));
var joystickActive = false;
var joystickStart = {
x: 0,
y: 0
};
var joystickDir = {
x: 0,
y: 0
};
// Pass Button
var passBtn = LK.gui.bottomRight.addChild(LK.getAsset('pass_btn', {
anchorX: 0.5,
anchorY: 0.5,
x: -220,
y: -320,
scaleX: 2,
scaleY: 2
}));
// Shoot Button
var shootBtn = LK.gui.bottomRight.addChild(LK.getAsset('shoot_btn', {
anchorX: 0.5,
anchorY: 0.5,
x: -220,
y: -120,
scaleX: 2,
scaleY: 2
}));
// --- GAME OBJECTS ---
// Left Goal (bottom)
var leftGoal = new SoccerGoal();
leftGoal.x = 2048 / 2;
leftGoal.y = 2732 - 120;
game.addChild(leftGoal);
// Right Goal (top)
var rightGoal = new SoccerGoal();
rightGoal.x = 2048 / 2;
rightGoal.y = 120;
game.addChild(rightGoal);
var footballer = new Footballer();
footballer.x = 2048 / 2;
footballer.y = 2732 - 500;
game.addChild(footballer);
var ball = new Ball();
ball.x = footballer.x;
ball.y = footballer.y - footballer.radius - 40;
game.addChild(ball);
// --- GAME STATE ---
var canShoot = true;
var canPass = true;
// --- SCOREBOARD ---
var leftScore = 0;
var rightScore = 0;
// Create a group container for the scoreboard and goals text
var scoreboardGroup = new Container();
// Scoreboard asset (background)
var scoreboardBg = LK.getAsset('centerCircle', {
width: 600,
height: 180,
color: 0x222222,
anchorX: 0.5,
anchorY: 0,
alpha: 0.7
});
scoreboardBg.x = 0;
scoreboardBg.y = 0;
scoreboardGroup.addChild(scoreboardBg);
// Goals text label
var goalsLabel = new Text2('GOALS', {
size: 60,
fill: 0xFFD700
});
goalsLabel.anchor.set(0.5, 0);
goalsLabel.x = 0;
goalsLabel.y = 10;
scoreboardGroup.addChild(goalsLabel);
// Score text on top of scoreboard asset
var scoreTxt = new Text2('0 : 0', {
size: 120,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
scoreTxt.x = 0;
scoreTxt.y = 70;
scoreboardGroup.addChild(scoreTxt);
// Add the group to the GUI top center
LK.gui.top.addChild(scoreboardGroup);
// Position the group at top center
scoreboardGroup.x = 0;
scoreboardGroup.y = 0;
function updateScoreboard() {
scoreTxt.setText(leftScore + " : " + rightScore);
}
updateScoreboard();
// --- JOYSTICK EVENTS ---
joystickKnob.down = function (x, y, obj) {
joystickActive = true;
joystickStart.x = x;
joystickStart.y = y;
};
joystickKnob.up = function (x, y, obj) {
joystickActive = false;
joystickKnob.x = joystick.x;
joystickKnob.y = joystick.y;
joystickDir.x = 0;
joystickDir.y = 0;
};
joystickKnob.move = function (x, y, obj) {
if (!joystickActive) {
return;
}
var dx = x - joystick.x;
var dy = y - joystick.y;
var dist = Math.sqrt(dx * dx + dy * dy);
var maxDist = 120;
if (dist > maxDist) {
dx = dx * maxDist / dist;
dy = dy * maxDist / dist;
}
joystickKnob.x = joystick.x + dx;
joystickKnob.y = joystick.y + dy;
joystickDir.x = dx / maxDist;
joystickDir.y = dy / maxDist;
};
// --- BUTTON EVENTS ---
passBtn.down = function (x, y, obj) {
if (footballer.hasBall && canPass) {
// Pass: direction depends on joystick
var passSpeed = 40;
var dirY = joystickDir.y;
// If joystick is pushed down, pass down, else pass up
if (dirY > 0.5) {
ball.vx = 0;
ball.vy = passSpeed;
} else {
ball.vx = 0;
ball.vy = -passSpeed;
}
footballer.hasBall = false;
canPass = false;
LK.setTimeout(function () {
canPass = true;
}, 400);
}
};
shootBtn.down = function (x, y, obj) {
if (footballer.hasBall && canShoot) {
// Shoot: direction depends on joystick
var shootSpeed = 80;
var dirY = joystickDir.y;
// If joystick is pushed down, shoot down, else shoot up
if (dirY > 0.5) {
ball.vx = 0;
ball.vy = shootSpeed;
} else if (dirY < -0.5) {
ball.vx = 0;
ball.vy = -shootSpeed;
} else {
// If joystick is not pushed up or down, shoot forward (up by default)
ball.vx = 0;
ball.vy = -shootSpeed;
}
footballer.hasBall = false;
canShoot = false;
LK.setTimeout(function () {
canShoot = true;
}, 800);
}
};
// --- GAME MOVE HANDLER ---
game.move = function (x, y, obj) {
// Track last move position for touch detection
game.lastMoveX = x;
game.lastMoveY = y;
// Forward joystick events
if (joystickActive) {
joystickKnob.move(x, y, obj);
}
};
// --- GAME UPDATE ---
game.update = function () {
// Move footballer with joystick
if (joystickDir.x !== 0 || joystickDir.y !== 0) {
var speed = 18;
footballer.x += joystickDir.x * speed;
footballer.y += joystickDir.y * speed;
// Clamp to field
if (footballer.x < 100 + footballer.radius) {
footballer.x = 100 + footballer.radius;
}
if (footballer.x > 2048 - 100 - footballer.radius) {
footballer.x = 2048 - 100 - footballer.radius;
}
if (footballer.y < 200 + footballer.radius) {
footballer.y = 200 + footballer.radius;
}
if (footballer.y > 2732 - 200 - footballer.radius) {
footballer.y = 2732 - 200 - footballer.radius;
}
}
// Ball always moves independently
// If footballer does NOT have the ball, ball moves by its own update
if (!footballer.hasBall) {
ball.update();
} else {
// If footballer has the ball, allow left/right and up/down movement of the ball with joystick
// Ball can move in front of or below the footballer, depending on joystick direction
var offsetX = joystickDir.x * 80; // max 80px left/right
var offsetY;
if (joystickDir.y > 0.5) {
// If joystick is pushed down, let the ball go below the footballer
offsetY = -(footballer.radius + 40 + joystickDir.y * 80);
ball.x = footballer.x + offsetX;
ball.y = footballer.y - offsetY;
} else {
// Default: ball in front of footballer, allow up/down movement
offsetY = footballer.radius + 40 - joystickDir.y * 80;
ball.x = footballer.x + offsetX;
ball.y = footballer.y - offsetY;
}
// Ball velocity is zero while attached
ball.vx = 0;
ball.vy = 0;
}
// Regain possession if close
var dx = footballer.x - ball.x;
var dy = footballer.y - ball.y;
var dist = Math.sqrt(dx * dx + dy * dy);
if (dist < footballer.radius + ball.radius + 20 && !footballer.hasBall) {
footballer.hasBall = true;
} else if (dist < footballer.radius + ball.radius + 20 && footballer.hasBall === false) {
// This block is for clarity, but will never be reached since hasBall is set above.
}
// --- BALL TOUCH DETECTION (not by footballer) ---
if (ball.lastWasTouched === undefined) ball.lastWasTouched = false;
var isTouched = false;
// Defensive: ignore, as we don't get here from a down event
if (!footballer.hasBall) {
// Check if the ball is being touched (by a finger, not by footballer)
// We use LK.gui or game.move events for touch, but since we don't have a direct event, let's check if the ball is under the last move position
// We'll use the last known move position if available
if (typeof game.lastMoveX !== "undefined" && typeof game.lastMoveY !== "undefined") {
var bx = ball.x,
by = ball.y,
br = ball.radius;
var mx = game.lastMoveX,
my = game.lastMoveY;
var distTouch = Math.sqrt((bx - mx) * (bx - mx) + (by - my) * (by - my));
if (distTouch < br) {
isTouched = true;
}
}
}
if (!ball.lastWasTouched && isTouched) {
// Only add point if footballer does NOT have the ball
rightScore += 1;
updateScoreboard();
}
ball.lastWasTouched = isTouched;
// --- GOAL DETECTION ---
// Add 1 point to right scoreboard when ball touches the up side goal (top edge)
if (ball.lastY !== undefined && (
// Ball enters rightGoal (top) from below
ball.lastY > rightGoal.y && ball.y <= rightGoal.y ||
// Ball touches the top edge of the field
ball.lastY >= 200 + ball.radius && ball.y < 200 + ball.radius) && Math.abs(ball.x - rightGoal.x) < rightGoal.width * 0.5 && footballer.hasBall === false) {
rightScore += 1;
updateScoreboard();
// Reset ball and footballer
footballer.x = 2048 / 2;
footballer.y = 2732 - 500;
ball.x = footballer.x;
ball.y = footballer.y - footballer.radius - 40;
footballer.hasBall = true;
ball.vx = 0;
ball.vy = 0;
}
// Add 1 point to left scoreboard when ball touches the bottom goal net (bottom edge)
if (ball.lastY !== undefined && (
// Ball enters leftGoal (bottom) from above
ball.lastY < leftGoal.y && ball.y >= leftGoal.y ||
// Ball touches the bottom edge of the field
ball.lastY <= 2732 - 200 - ball.radius && ball.y > 2732 - 200 - ball.radius) && Math.abs(ball.x - leftGoal.x) < leftGoal.width * 0.5 && footballer.hasBall === false) {
leftScore += 1;
updateScoreboard();
// Reset ball and footballer
footballer.x = 2048 / 2;
footballer.y = 2732 - 500;
ball.x = footballer.x;
ball.y = footballer.y - footballer.radius - 40;
footballer.hasBall = true;
ball.vx = 0;
ball.vy = 0;
}
ball.lastY = ball.y;
};
Football ball. In-Game asset. 2d. High contrast. No shadows
Cristiano ronaldo. In-Game asset. 2d. High contrast. No shadows
Button with says shoot. In-Game asset. 2d. High contrast. No shadows
A button that says pass. In-Game asset. 2d. High contrast. No shadows
Circle. In-Game asset. 2d. High contrast. No shadows
Little circle. In-Game asset. 2d. High contrast. No shadows