User prompt
player can curve the ball, ball will not go only straightforward
User prompt
when the ball touches to the inside white lines which is the goal, player should go the next round and it should count as score
User prompt
even though I am scoring the game does not continue with the next round can you correct this
User prompt
to pass the round one goal is enough, so if the players scores in his first attempt then he goes to the next round without trying his other attempts
User prompt
ball shouldnt stop at all
User prompt
there should not be gravity and the ball should continue to the goal
Code edit (1 edits merged)
Please save this source code
User prompt
Free Kick Master
Initial prompt
I want to make a free kick goal game. Player sees the ball in the down below screen and shoots by arranging the direction and speed of the ball. Player has 3 tries to score a goal. If the player can score within these 3 tries, he will go to the next round. The next round will be harder, it can be either from a further distance or with higher number of men trying to block the ball. Goalkeeper will not move and also other men will not move.
/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); /**** * Classes ****/ var Ball = Container.expand(function () { var self = Container.call(this); var ballGraphics = self.attachAsset('ball', { anchorX: 0.5, anchorY: 0.5 }); self.velocityX = 0; self.velocityY = 0; self.gravity = 0; self.friction = 0.98; self.isMoving = false; self.shoot = function (power, angle) { self.velocityX = Math.cos(angle) * power; self.velocityY = Math.sin(angle) * power; self.isMoving = true; LK.getSound('kick').play(); }; self.reset = function () { self.x = ballStartX; self.y = ballStartY; self.velocityX = 0; self.velocityY = 0; self.isMoving = false; }; self.update = function () { if (self.isMoving) { self.x += self.velocityX; self.y += self.velocityY; // No gravity applied - ball travels in straight line self.velocityX *= self.friction; self.velocityY *= self.friction; // Stop if moving too slowly if (Math.abs(self.velocityX) < 0.1 && Math.abs(self.velocityY) < 0.1) { self.isMoving = false; } // Bounds checking if (self.x < 0 || self.x > 2048 || self.y > 2732) { self.isMoving = false; ballMissed(); } } }; return self; }); var Defender = Container.expand(function () { var self = Container.call(this); var defenderGraphics = self.attachAsset('defender', { anchorX: 0.5, anchorY: 1.0 }); return self; }); var Goalkeeper = Container.expand(function () { var self = Container.call(this); var keeperGraphics = self.attachAsset('goalkeeper', { anchorX: 0.5, anchorY: 1.0 }); return self; }); var TrajectoryDot = Container.expand(function () { var self = Container.call(this); var dotGraphics = self.attachAsset('trajectory', { anchorX: 0.5, anchorY: 0.5 }); dotGraphics.alpha = 0.6; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x4a90e2 }); /**** * Game Code ****/ // Game variables var currentRound = 1; var attemptsLeft = 3; var ballStartX = 1024; var ballStartY = 2400; var goalX = 1024; var goalY = 400; var isAiming = false; var dragStartX = 0; var dragStartY = 0; var trajectoryDots = []; var defenders = []; // Create grass field var grass = game.addChild(LK.getAsset('grass', { anchorX: 0, anchorY: 1.0, x: 0, y: 2732 })); // Create goal posts var leftPost = game.addChild(LK.getAsset('goalPost', { anchorX: 0.5, anchorY: 1.0, x: goalX - 300, y: goalY + 200 })); var rightPost = game.addChild(LK.getAsset('goalPost', { anchorX: 0.5, anchorY: 1.0, x: goalX + 300, y: goalY + 200 })); var crossbar = game.addChild(LK.getAsset('goalCrossbar', { anchorX: 0.5, anchorY: 0.5, x: goalX, y: goalY })); // Create goalkeeper var goalkeeper = game.addChild(new Goalkeeper()); goalkeeper.x = goalX; goalkeeper.y = goalY + 180; // Create ball var ball = game.addChild(new Ball()); ball.reset(); // UI Elements var roundText = new Text2('Round: 1', { size: 80, fill: 0xFFFFFF }); roundText.anchor.set(0.5, 0); LK.gui.top.addChild(roundText); var attemptsText = new Text2('Attempts: 3', { size: 60, fill: 0xFFFFFF }); attemptsText.anchor.set(1.0, 0); LK.gui.topRight.addChild(attemptsText); var powerText = new Text2('', { size: 50, fill: 0xFFFF00 }); powerText.anchor.set(0.5, 0.5); game.addChild(powerText); // Initialize defenders for current round function setupRound() { // Clear existing defenders for (var i = 0; i < defenders.length; i++) { defenders[i].destroy(); } defenders = []; // Add defenders based on round var numDefenders = Math.min(currentRound - 1, 4); for (var i = 0; i < numDefenders; i++) { var defender = game.addChild(new Defender()); var spacing = 120; var startX = goalX - (numDefenders - 1) * spacing / 2; defender.x = startX + i * spacing; defender.y = goalY + 300 + currentRound * 50; defenders.push(defender); } roundText.setText('Round: ' + currentRound); attemptsText.setText('Attempts: ' + attemptsLeft); } // Create trajectory preview function createTrajectoryPreview(power, angle) { // Clear existing dots for (var i = 0; i < trajectoryDots.length; i++) { trajectoryDots[i].destroy(); } trajectoryDots = []; // Calculate trajectory points var steps = 15; var stepTime = 0.8; var tempVelX = Math.cos(angle) * power * 0.7; var tempVelY = Math.sin(angle) * power * 0.7; var tempX = ball.x; var tempY = ball.y; for (var i = 0; i < steps; i++) { tempX += tempVelX * stepTime; tempY += tempVelY * stepTime; // No gravity applied to trajectory preview tempVelX *= 0.98; tempVelY *= 0.98; if (tempY > 2732 || tempX < 0 || tempX > 2048) break; var dot = game.addChild(new TrajectoryDot()); dot.x = tempX; dot.y = tempY; trajectoryDots.push(dot); } } // Clear trajectory preview function clearTrajectoryPreview() { for (var i = 0; i < trajectoryDots.length; i++) { trajectoryDots[i].destroy(); } trajectoryDots = []; } // Ball collision detection function checkCollisions() { // Check goal scoring if (ball.x > goalX - 300 && ball.x < goalX + 300 && ball.y > goalY - 10 && ball.y < goalY + 200) { if (ball.y < goalY + 20) { // Goal scored! ball.isMoving = false; LK.getSound('goal').play(); LK.effects.flashScreen(0x00ff00, 500); currentRound++; attemptsLeft = 3; LK.setTimeout(function () { ball.reset(); setupRound(); }, 1000); return; } } // Check goalkeeper collision if (ball.intersects(goalkeeper)) { ball.isMoving = false; LK.getSound('save').play(); LK.effects.flashObject(goalkeeper, 0xffff00, 500); ballMissed(); return; } // Check defender collisions for (var i = 0; i < defenders.length; i++) { if (ball.intersects(defenders[i])) { ball.isMoving = false; LK.effects.flashObject(defenders[i], 0xff0000, 500); ballMissed(); return; } } // Check goal post collisions if (ball.intersects(leftPost) || ball.intersects(rightPost) || ball.intersects(crossbar)) { ball.isMoving = false; ballMissed(); return; } } function ballMissed() { attemptsLeft--; attemptsText.setText('Attempts: ' + attemptsLeft); if (attemptsLeft <= 0) { LK.effects.flashScreen(0xff0000, 1000); LK.setTimeout(function () { attemptsLeft = 3; ball.reset(); setupRound(); }, 1500); } else { LK.setTimeout(function () { ball.reset(); }, 1000); } } // Game controls game.down = function (x, y, obj) { if (!ball.isMoving && ball.y > 2000) { isAiming = true; dragStartX = x; dragStartY = y; } }; game.move = function (x, y, obj) { if (isAiming && !ball.isMoving) { var deltaX = x - dragStartX; var deltaY = y - dragStartY; var distance = Math.sqrt(deltaX * deltaX + deltaY * deltaY); var power = Math.min(distance / 8, 25); var angle = Math.atan2(-deltaY, deltaX); powerText.x = ball.x; powerText.y = ball.y - 100; powerText.setText('Power: ' + Math.round(power)); createTrajectoryPreview(power, angle); } }; game.up = function (x, y, obj) { if (isAiming && !ball.isMoving) { var deltaX = x - dragStartX; var deltaY = y - dragStartY; var distance = Math.sqrt(deltaX * deltaX + deltaY * deltaY); if (distance > 20) { var power = Math.min(distance / 8, 25); var angle = Math.atan2(-deltaY, deltaX); ball.shoot(power, angle); } isAiming = false; powerText.setText(''); clearTrajectoryPreview(); } }; game.update = function () { if (ball.isMoving) { checkCollisions(); } }; // Initialize first round setupRound();
===================================================================
--- original.js
+++ change.js
@@ -13,9 +13,9 @@
anchorY: 0.5
});
self.velocityX = 0;
self.velocityY = 0;
- self.gravity = 0.5;
+ self.gravity = 0;
self.friction = 0.98;
self.isMoving = false;
self.shoot = function (power, angle) {
self.velocityX = Math.cos(angle) * power;
@@ -33,9 +33,9 @@
self.update = function () {
if (self.isMoving) {
self.x += self.velocityX;
self.y += self.velocityY;
- self.velocityY += self.gravity;
+ // No gravity applied - ball travels in straight line
self.velocityX *= self.friction;
self.velocityY *= self.friction;
// Stop if moving too slowly
if (Math.abs(self.velocityX) < 0.1 && Math.abs(self.velocityY) < 0.1) {
@@ -186,9 +186,9 @@
var tempY = ball.y;
for (var i = 0; i < steps; i++) {
tempX += tempVelX * stepTime;
tempY += tempVelY * stepTime;
- tempVelY += 0.5 * stepTime;
+ // No gravity applied to trajectory preview
tempVelX *= 0.98;
tempVelY *= 0.98;
if (tempY > 2732 || tempX < 0 || tempX > 2048) break;
var dot = game.addChild(new TrajectoryDot());