User prompt
Ai do sometimes random move
User prompt
Red can move and ai get starter
User prompt
Red is ai controled
User prompt
Please fix the bug: 'ReferenceError: leftGoal is not defined' in or related to this line: 'var inLeftGoal = ball.intersects(leftGoal);' Line Number: 340
User prompt
Remove all horizantal
User prompt
Please fix the bug: 'ReferenceError: leftGoalPostLeft is not defined' in or related to this line: 'var leftLeftNow = ball.intersects(leftGoalPostLeft);' Line Number: 226
User prompt
Make horizontal
User prompt
Make horizon talep
User prompt
2 players can touch the screen
User prompt
Remove ai and the game play 2 players
User prompt
When ball stucks red give up and game finish
User prompt
When ball touch goal post ball Bounce that
User prompt
Get smallee
User prompt
Narrow the goal post and get symetri goal
User prompt
A little bit go big goal post
User prompt
Goal post make slim and near the goal
User prompt
Greater goal post ın goal left and right side
User prompt
Make goal post
User prompt
Ball stop the when the game start but whoever touch the ball, ball cant stop anymore and go Faster
User prompt
Ball cant stop
User prompt
Ai get smartter
User prompt
Ball cant stop
User prompt
When ball stuck he try hit right side
User prompt
Ai target for oppenets side not own
User prompt
Ai cant try score own goal
/**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x1976d2 // Blue field background }); /**** * Game Code ****/ ; // --- Timer --- var timerSeconds = 0; var timerRunning = true; var timerTxt = new Text2('0:00', { size: 90, fill: 0xffffff, font: "Impact, Arial Black, Tahoma" }); timerTxt.anchor.set(0.5, 1); timerTxt.x = LK.gui.width / 2; // Place timer at top center LK.gui.top.addChild(timerTxt); timerTxt.y = 0; ; var draggingPlayer = null; // Convert global (event) coordinates to local game coordinates function toGameCoords(x, y) { // LK events provide coordinates in game space already return { x: x, y: y }; } // Handle touch/mouse down: start dragging if on player game.down = function (x, y, obj) { // Check if the down event is on the blue player var dx = x - player.x; var dy = y - player.y; var r = player.width / 2; if (dx * dx + dy * dy <= r * r) { draggingPlayer = player; } // No-op: Remove red player drag logic, opponent is now AI controlled }; // Handle touch/mouse move: move player if dragging game.move = function (x, y, obj) { if (draggingPlayer) { // Blue player (left side) - only blue player can be dragged if (draggingPlayer === player) { var minX = field.x - field.width / 2 + player.width / 2; var maxX = field.x; // Center line is the max for left player var minY = field.y - field.height / 2 + player.height / 2; var maxY = field.y + field.height / 2 - player.height / 2; draggingPlayer.x = Math.max(minX, Math.min(maxX - player.width / 2, x)); draggingPlayer.y = Math.max(minY, Math.min(maxY, y)); } } // Remove red player drag logic: opponent is now AI controlled }; // Handle touch/mouse up: stop dragging game.up = function (x, y, obj) { draggingPlayer = null; }; ; ; // Track last intersection state between player and ball var lastPlayerBallIntersecting = false; // Update loop to check for player touching ball game.update = function () { // --- Timer update --- if (timerRunning) { if (typeof game._lastTimerTick === "undefined") { game._lastTimerTick = Date.now(); } var now = Date.now(); if (now - game._lastTimerTick >= 1000) { timerSeconds += 1; game._lastTimerTick += 1000; // Format as M:SS var min = Math.floor(timerSeconds / 60); var sec = timerSeconds % 60; timerTxt.setText(min + ":" + (sec < 10 ? "0" : "") + sec); } } // --- AI for red opponent --- if (typeof opponent.aiTargetX === "undefined") { opponent.aiTargetX = opponent.x; opponent.aiTargetY = opponent.y; } var aiSpeed = 18; var minX2 = field.x; // Center line is the min for right player var maxX2 = field.x + field.width / 2 - opponent.width / 2; var minY2 = field.y - field.height / 2 + opponent.height / 2; var maxY2 = field.y + field.height / 2 - opponent.height / 2; // --- Smarter AI: Predict ball trajectory, intercept, angle shots, avoid obstacles --- // Predict where the ball will be in the near future (simple linear prediction) var predictFrames = 18; var predictedBallX = ball.x + (typeof ball.vx === "number" ? ball.vx : 0) * predictFrames; var predictedBallY = ball.y + (typeof ball.vy === "number" ? ball.vy : 0) * predictFrames; // Clamp prediction to field predictedBallX = Math.max(minX2, Math.min(maxX2, predictedBallX)); predictedBallY = Math.max(minY2, Math.min(maxY2, predictedBallY)); // AI logic: decide between attacking and defending var rightGoalX = rightGoal.x; var rightGoalY = rightGoal.y; var leftGoalX = leftGoal.x; var leftGoalY = leftGoal.y; var defendZoneX = field.x + field.width * 0.18; // 18% from center, a bit in front of goal // If the ball is close to the AI's goal, defend. Otherwise, try to attack. var ballIsThreatening = ball.x > field.x + field.width * 0.35; // Ball is deep in AI's half var aiHasBall = Math.abs(ball.x - opponent.x) < 200 && Math.abs(ball.y - opponent.y) < 200; // Avoid blocking own goal: if too close to rightGoal, move up/down to clear var avoidOwnGoal = false; if (Math.abs(opponent.x - rightGoalX) < 80 && Math.abs(opponent.y - rightGoalY) < rightGoal.height / 2) { avoidOwnGoal = true; // Move up or down to clear the goal area if (opponent.y < rightGoalY) { opponent.aiTargetX = rightGoalX - 60; opponent.aiTargetY = rightGoalY - rightGoal.height / 2 + opponent.height; } else { opponent.aiTargetX = rightGoalX - 60; opponent.aiTargetY = rightGoalY + rightGoal.height / 2 - opponent.height; } } else if (ballIsThreatening) { // DEFEND: Place between ball and right goal, but not too far from goal var defendX = Math.max(defendZoneX, Math.min(maxX2, ball.x - 80)); var defendY = Math.max(minY2, Math.min(maxY2, ball.y)); opponent.aiTargetX = defendX; opponent.aiTargetY = defendY; } else if (aiHasBall || ball.x > field.x) { // ATTACK: Move toward ball, then toward left goal if close to ball if (aiHasBall) { // If close to ball, try to "shoot" toward the opponent's goal (left goal), never own goal // Add angle: aim for a random spot in the left goal, not just center var goalTargetY = leftGoalY + (Math.random() - 0.5) * (leftGoal.height * 0.7); // If player is blocking, try to shoot around them var playerBlock = false; if (Math.abs(player.x - ball.x) < 180 && Math.abs(player.y - ball.y) < 180) { playerBlock = true; } var offsetX = 60; var offsetY = (Math.random() - 0.5) * 100; if (playerBlock) { // Try to shoot above or below the player if (ball.y < player.y) { goalTargetY = Math.max(leftGoalY - leftGoal.height / 2 + 60, player.y - 120); } else { goalTargetY = Math.min(leftGoalY + leftGoal.height / 2 - 60, player.y + 120); } offsetY += (Math.random() - 0.5) * 60; } opponent.aiTargetX = Math.max(minX2, Math.min(maxX2, leftGoalX + offsetX)); opponent.aiTargetY = Math.max(minY2, Math.min(maxY2, goalTargetY + offsetY)); } else { // Move toward predicted ball position, not just current opponent.aiTargetX = Math.max(minX2, Math.min(maxX2, predictedBallX)); opponent.aiTargetY = Math.max(minY2, Math.min(maxY2, predictedBallY)); } } else { // Idle near center of right half opponent.aiTargetX = field.x + field.width * 0.25; opponent.aiTargetY = field.y; } // Move opponent toward target var odx = opponent.aiTargetX - opponent.x; var ody = opponent.aiTargetY - opponent.y; var odist = Math.sqrt(odx * odx + ody * ody) || 1; if (odist > 1) { opponent.x += odx / odist * Math.min(aiSpeed, odist); opponent.y += ody / odist * Math.min(aiSpeed, odist); } // Check if player is touching the ball (collision/intersection) var isIntersecting = player.intersects(ball); // Detect the exact moment player starts touching the ball if (!lastPlayerBallIntersecting && isIntersecting) { // Calculate direction from player to ball var dx = ball.x - player.x; var dy = ball.y - player.y; var dist = Math.sqrt(dx * dx + dy * dy) || 1; // Give the ball a velocity away from the player (EVEN FASTER than before) ball.vx = dx / dist * 70; ball.vy = dy / dist * 70; LK.effects.flashObject(ball, 0x1976d2, 200); } // AI: Red player can also kick the ball if touching, from any side if (opponent.intersects(ball)) { // Check if ball is in a corner var minBallX = field.x - field.width / 2 + ball.width / 2; var maxBallX = field.x + field.width / 2 - ball.width / 2; var minBallY = field.y - field.height / 2 + ball.height / 2; var maxBallY = field.y + field.height / 2 - ball.height / 2; var atLeft = ball.x <= minBallX + 1; var atRight = ball.x >= maxBallX - 1; var atTop = ball.y <= minBallY + 1; var atBottom = ball.y >= maxBallY - 1; // --- Ball stuck detection --- if (typeof ball.stuckFrames === "undefined") ball.stuckFrames = 0; if (typeof ball.lastStuckX === "undefined") ball.lastStuckX = ball.x; if (typeof ball.lastStuckY === "undefined") ball.lastStuckY = ball.y; // If the ball hasn't moved much in 10 frames, consider it stuck var stuckThreshold = 6; if (Math.abs(ball.x - ball.lastStuckX) < stuckThreshold && Math.abs(ball.y - ball.lastStuckY) < stuckThreshold) { ball.stuckFrames++; } else { ball.stuckFrames = 0; ball.lastStuckX = ball.x; ball.lastStuckY = ball.y; } var dxr, dyr; if ((atLeft || atRight) && (atTop || atBottom)) { // Ball is in a corner, AI tries to hit it toward the center of the field dxr = field.x - ball.x; dyr = field.y - ball.y; } else if (ball.stuckFrames > 12) { // Ball is stuck: AI tries to hit the right side of the ball // Always aim to the right (positive X direction) dxr = 1; dyr = 0; // Add a little bias toward the field center to avoid infinite right wall hits dxr += (field.x - ball.x) * 0.005; dyr += (field.y - ball.y) * 0.01; } else { // Normal: hit from AI to ball direction dxr = ball.x - opponent.x; dyr = ball.y - opponent.y; } var distr = Math.sqrt(dxr * dxr + dyr * dyr) || 1; ball.vx = dxr / distr * 50; ball.vy = dyr / distr * 50; LK.effects.flashObject(ball, 0xd32f2f, 200); } // Ball activation: Ball is stopped at start, but after first touch, it never stops and goes faster if (typeof ball.activated === "undefined") ball.activated = false; if (!ball.activated && (player.intersects(ball) || opponent.intersects(ball))) { ball.activated = true; // Give a small nudge in a random direction if ball is perfectly still if (ball.vx === 0 && ball.vy === 0) { var angle = Math.random() * Math.PI * 2; ball.vx = Math.cos(angle) * 18; ball.vy = Math.sin(angle) * 18; } } // Move the ball if it has velocity if (typeof ball.vx === "number" && typeof ball.vy === "number") { // Track lastX for side detection if (typeof ball.lastX === "undefined") ball.lastX = ball.x; // Detect crossing from left to right side (player to opponent) var centerX = field.x; if (ball.lastX <= centerX && ball.x > centerX) { // Ball just crossed to right side, speed up! ball.vx *= 1.25; ball.vy *= 1.25; } // Detect crossing from right to left side (opponent to player) if (ball.lastX >= centerX && ball.x < centerX) { // Ball just crossed to left side, speed up! ball.vx *= 1.25; ball.vy *= 1.25; } if (ball.activated) { ball.x += ball.vx; ball.y += ball.vy; } // Friction ball.vx *= 0.96; ball.vy *= 0.96; // Clamp to field bounds and bounce off corners var minBallX = field.x - field.width / 2 + ball.width / 2; var maxBallX = field.x + field.width / 2 - ball.width / 2; var minBallY = field.y - field.height / 2 + ball.height / 2; var maxBallY = field.y + field.height / 2 - ball.height / 2; // Detect if ball is in a corner (within 1px of both X and Y bounds) var atLeft = ball.x <= minBallX + 1; var atRight = ball.x >= maxBallX - 1; var atTop = ball.y <= minBallY + 1; var atBottom = ball.y >= maxBallY - 1; // Bounce off corners: if at a corner, reflect both vx and vy if (atLeft && atTop || atLeft && atBottom || atRight && atTop || atRight && atBottom) { // Snap to corner ball.x = atLeft ? minBallX : maxBallX; ball.y = atTop ? minBallY : maxBallY; ball.vx *= -0.5; ball.vy *= -0.5; } else { // Bounce off left/right walls if (ball.x < minBallX) { ball.x = minBallX; ball.vx *= -0.5; } if (ball.x > maxBallX) { ball.x = maxBallX; ball.vx *= -0.5; } // Bounce off top/bottom walls if (ball.y < minBallY) { ball.y = minBallY; ball.vy *= -0.5; } if (ball.y > maxBallY) { ball.y = maxBallY; ball.vy *= -0.5; } } // Prevent the ball from ever stopping: enforce a minimum velocity if it's moving var minBallSpeed = ball.activated ? 8 : 1.2; if (typeof ball.vx === "number" && typeof ball.vy === "number") { var speed = Math.sqrt(ball.vx * ball.vx + ball.vy * ball.vy); if (speed < minBallSpeed) { if (speed === 0) { // If completely stopped, nudge in a random direction var angle = Math.random() * Math.PI * 2; ball.vx = Math.cos(angle) * minBallSpeed; ball.vy = Math.sin(angle) * minBallSpeed; } else { // If moving but too slow, scale up to minimum speed ball.vx = ball.vx / (speed || 1) * minBallSpeed; ball.vy = ball.vy / (speed || 1) * minBallSpeed; } } } // Never allow the ball to stop: if both vx and vy are 0, nudge in a random direction if (ball.activated && typeof ball.vx === "number" && typeof ball.vy === "number" && ball.vx === 0 && ball.vy === 0) { var angle = Math.random() * Math.PI * 2; ball.vx = Math.cos(angle) * minBallSpeed; ball.vy = Math.sin(angle) * minBallSpeed; } // Update lastX for next frame ball.lastX = ball.x; // --- Goal detection --- // If ball intersects leftGoal or rightGoal, score! // Only trigger on the exact frame the ball enters the goal (use lastBallInLeftGoal/lastBallInRightGoal) if (typeof ball.lastInLeftGoal === "undefined") ball.lastInLeftGoal = false; if (typeof ball.lastInRightGoal === "undefined") ball.lastInRightGoal = false; var inLeftGoal = ball.intersects(leftGoal); var inRightGoal = ball.intersects(rightGoal); // Score for blue (player) if ball enters right goal if (!ball.lastInRightGoal && inRightGoal) { timerRunning = false; LK.effects.flashScreen(0x00ff00, 600); LK.showYouWin(); return; } // Score for red (opponent) if ball enters left goal if (!ball.lastInLeftGoal && inLeftGoal) { timerRunning = false; LK.effects.flashScreen(0xff0000, 600); LK.showGameOver(); return; } ball.lastInLeftGoal = inLeftGoal; ball.lastInRightGoal = inRightGoal; } // Update last intersection state lastPlayerBallIntersecting = isIntersecting; }; // Futsal ball (white) // Center circle (white, ellipse) // Center line (white, box) // Main field (futsal green) // Goal area (penalty box, white) // Goal (yellow, box) // Player (blue, ellipse) var field = LK.getAsset('field', { anchorX: 0.5, anchorY: 0.5, x: 2048 / 2, y: 2732 / 2 }); game.addChild(field); // Draw center line var centerLine = LK.getAsset('centerLine', { anchorX: 0.5, anchorY: 0.5, width: 10, // thin vertical line height: 1200, // match field height x: 2048 / 2, y: 2732 / 2 }); game.addChild(centerLine); // Draw center circle var centerCircle = LK.getAsset('centerCircle', { anchorX: 0.5, anchorY: 0.5, x: 2048 / 2, y: 2732 / 2 }); game.addChild(centerCircle); // Draw left goal (real-life: tall, slim, at field edge, centered vertically) var leftGoal = LK.getAsset('goal', { anchorX: 0.5, // center anchor for easier placement anchorY: 0.5, width: 40, // slim width height: 400, // tall height x: (2048 - 1900) / 2 + 20, // flush with left field edge y: 2732 / 2 }); game.addChild(leftGoal); // Left goal posts (top and bottom) - slim and close to the goal var leftGoalPostTop = LK.getAsset('goal', { anchorX: 0.5, anchorY: 1.0, width: 28, height: 110, x: leftGoal.x - leftGoal.width / 2 + 14, y: leftGoal.y - leftGoal.height / 2 // near the top of the goal }); game.addChild(leftGoalPostTop); var leftGoalPostBottom = LK.getAsset('goal', { anchorX: 0.5, anchorY: 0.0, width: 28, height: 110, x: leftGoal.x - leftGoal.width / 2 + 14, y: leftGoal.y + leftGoal.height / 2 // near the bottom of the goal }); game.addChild(leftGoalPostBottom); // Draw right goal (real-life: tall, slim, at field edge, centered vertically) var rightGoal = LK.getAsset('goal', { anchorX: 0.5, anchorY: 0.5, width: 40, height: 400, x: (2048 + 1900) / 2 - 20, // flush with right field edge y: 2732 / 2 }); game.addChild(rightGoal); // Right goal posts (top and bottom) - slim and close to the goal var rightGoalPostTop = LK.getAsset('goal', { anchorX: 0.5, anchorY: 1.0, width: 28, height: 110, x: rightGoal.x + rightGoal.width / 2 - 14, y: rightGoal.y - rightGoal.height / 2 // near the top of the goal }); game.addChild(rightGoalPostTop); var rightGoalPostBottom = LK.getAsset('goal', { anchorX: 0.5, anchorY: 0.0, width: 28, height: 110, x: rightGoal.x + rightGoal.width / 2 - 14, y: rightGoal.y + rightGoal.height / 2 // near the bottom of the goal }); game.addChild(rightGoalPostBottom); // Draw ball at center var ball = LK.getAsset('ball', { anchorX: 0.5, anchorY: 0.5, x: 2048 / 2, y: 2732 / 2 }); ball.vx = 0; ball.vy = 0; game.addChild(ball); // Draw player at left side (blue) var player = LK.getAsset('playerBlue', { anchorX: 0.5, anchorY: 0.5, x: 2048 / 2 - 500, y: 2732 / 2 }); game.addChild(player); // Draw opponent at right side (red) var opponent = LK.getAsset('playerRed', { anchorX: 0.5, anchorY: 0.5, x: 2048 / 2 + 500, y: 2732 / 2 }); game.addChild(opponent); ;
===================================================================
--- original.js
+++ change.js
@@ -405,20 +405,20 @@
// Left goal posts (top and bottom) - slim and close to the goal
var leftGoalPostTop = LK.getAsset('goal', {
anchorX: 0.5,
anchorY: 1.0,
- width: 18,
- height: 70,
- x: leftGoal.x - leftGoal.width / 2 + 9,
+ width: 28,
+ height: 110,
+ x: leftGoal.x - leftGoal.width / 2 + 14,
y: leftGoal.y - leftGoal.height / 2 // near the top of the goal
});
game.addChild(leftGoalPostTop);
var leftGoalPostBottom = LK.getAsset('goal', {
anchorX: 0.5,
anchorY: 0.0,
- width: 18,
- height: 70,
- x: leftGoal.x - leftGoal.width / 2 + 9,
+ width: 28,
+ height: 110,
+ x: leftGoal.x - leftGoal.width / 2 + 14,
y: leftGoal.y + leftGoal.height / 2 // near the bottom of the goal
});
game.addChild(leftGoalPostBottom);
// Draw right goal (real-life: tall, slim, at field edge, centered vertically)
@@ -435,20 +435,20 @@
// Right goal posts (top and bottom) - slim and close to the goal
var rightGoalPostTop = LK.getAsset('goal', {
anchorX: 0.5,
anchorY: 1.0,
- width: 18,
- height: 70,
- x: rightGoal.x + rightGoal.width / 2 - 9,
+ width: 28,
+ height: 110,
+ x: rightGoal.x + rightGoal.width / 2 - 14,
y: rightGoal.y - rightGoal.height / 2 // near the top of the goal
});
game.addChild(rightGoalPostTop);
var rightGoalPostBottom = LK.getAsset('goal', {
anchorX: 0.5,
anchorY: 0.0,
- width: 18,
- height: 70,
- x: rightGoal.x + rightGoal.width / 2 - 9,
+ width: 28,
+ height: 110,
+ x: rightGoal.x + rightGoal.width / 2 - 14,
y: rightGoal.y + rightGoal.height / 2 // near the bottom of the goal
});
game.addChild(rightGoalPostBottom);
// Draw ball at center