/**** * 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.3; self.moving = false; self.update = function () { if (self.moving) { self.x += self.velocityX; self.y += self.velocityY; self.velocityY += self.gravity; // Ground collision if (self.y >= ballStartY) { self.y = ballStartY; self.velocityY *= -0.4; self.velocityX *= 0.8; if (Math.abs(self.velocityX) < 1 && Math.abs(self.velocityY) < 1) { self.moving = false; self.velocityX = 0; self.velocityY = 0; } } } }; self.shoot = function (targetX, targetY, power) { var distance = Math.sqrt((targetX - self.x) * (targetX - self.x) + (targetY - self.y) * (targetY - self.y)); self.velocityX = (targetX - self.x) / distance * power; self.velocityY = (targetY - self.y) / distance * power; self.moving = true; LK.getSound('kick').play(); }; return self; }); var Goalkeeper = Container.expand(function () { var self = Container.call(this); var keeperGraphics = self.attachAsset('goalkeeper', { anchorX: 0.5, anchorY: 1.0 }); self.speed = 2 + LK.getScore() * 0.3; self.direction = 1; self.reactionTime = 0; self.maxReactionTime = 30 - LK.getScore() * 2; if (self.maxReactionTime < 10) self.maxReactionTime = 10; self.update = function () { // Random movement if (Math.random() < 0.02) { self.direction *= -1; } self.x += self.direction * self.speed; // Keep within goal bounds if (self.x <= goalX + 50) { self.x = goalX + 50; self.direction = 1; } if (self.x >= goalX + goalWidth - 50) { self.x = goalX + goalWidth - 50; self.direction = -1; } // React to ball when it's moving toward goal if (ball.moving && ball.y < goalY + 100) { self.reactionTime++; if (self.reactionTime > self.maxReactionTime) { if (ball.x < self.x) { self.direction = -1; } else { self.direction = 1; } } } else { self.reactionTime = 0; } }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x87CEEB }); /**** * Game Code ****/ // Game dimensions and positions var fieldY = 2732 - 600; var goalWidth = 800; var goalHeight = 400; var goalX = (2048 - goalWidth) / 2; var goalY = fieldY - goalHeight; var ballStartX = 2048 / 2; var ballStartY = fieldY - 40; // Game state var isAiming = false; var aimStartX = 0; var aimStartY = 0; var gameState = 'ready'; // 'ready', 'aiming', 'shooting', 'reset' // Create field var field = game.addChild(LK.getAsset('field', { anchorX: 0, anchorY: 0, x: 0, y: fieldY })); // Create goal var goal = game.addChild(LK.getAsset('goal', { anchorX: 0, anchorY: 0, x: goalX, y: goalY })); // Create goal posts (visual) var leftPost = game.addChild(LK.getAsset('aimLine', { anchorX: 0.5, anchorY: 0, x: goalX, y: goalY, width: 12, height: goalHeight, color: 0xffffff })); var rightPost = game.addChild(LK.getAsset('aimLine', { anchorX: 0.5, anchorY: 0, x: goalX + goalWidth, y: goalY, width: 12, height: goalHeight, color: 0xffffff })); var crossbar = game.addChild(LK.getAsset('aimLine', { anchorX: 0, anchorY: 0.5, x: goalX, y: goalY, width: goalWidth, height: 12, color: 0xffffff })); // Create ball var ball = game.addChild(new Ball()); ball.x = ballStartX; ball.y = ballStartY; // Create goalkeeper var goalkeeper = game.addChild(new Goalkeeper()); goalkeeper.x = goalX + goalWidth / 2; goalkeeper.y = goalY + goalHeight; // Create aim line (initially hidden) var aimLine = game.addChild(LK.getAsset('aimLine', { anchorX: 0, anchorY: 0.5, alpha: 0 })); // Create score display var scoreTxt = new Text2('Score: 0', { size: 80, fill: 0xFFFFFF }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); // Create instruction text var instructionTxt = new Text2('Drag to aim, release to shoot!', { size: 60, fill: 0xFFFFFF }); instructionTxt.anchor.set(0.5, 1); instructionTxt.y = -50; LK.gui.bottom.addChild(instructionTxt); // Game event handlers game.down = function (x, y, obj) { if (gameState === 'ready') { gameState = 'aiming'; isAiming = true; aimStartX = x; aimStartY = y; aimLine.alpha = 1; } }; game.move = function (x, y, obj) { if (isAiming && gameState === 'aiming') { var dx = x - ball.x; var dy = y - ball.y; var distance = Math.sqrt(dx * dx + dy * dy); if (distance > 0) { aimLine.x = ball.x; aimLine.y = ball.y; aimLine.width = Math.min(distance, 300); aimLine.rotation = Math.atan2(dy, dx); } } }; game.up = function (x, y, obj) { if (isAiming && gameState === 'aiming') { isAiming = false; aimLine.alpha = 0; gameState = 'shooting'; var dx = x - ball.x; var dy = y - ball.y; var distance = Math.sqrt(dx * dx + dy * dy); var power = Math.min(distance / 10, 20); ball.shoot(x, y, power); } }; // Reset function function resetGame() { ball.x = ballStartX; ball.y = ballStartY; ball.moving = false; ball.velocityX = 0; ball.velocityY = 0; gameState = 'ready'; goalkeeper.reactionTime = 0; goalkeeper.speed = 2 + LK.getScore() * 0.3; goalkeeper.maxReactionTime = 30 - LK.getScore() * 2; if (goalkeeper.maxReactionTime < 10) goalkeeper.maxReactionTime = 10; } // Game update loop game.update = function () { // Check for goal if (ball.moving && ball.x > goalX && ball.x < goalX + goalWidth && ball.y > goalY && ball.y < goalY + goalHeight && ball.lastY !== undefined && ball.lastY >= goalY) { // Check if goalkeeper blocked it if (!ball.intersects(goalkeeper)) { LK.setScore(LK.getScore() + 1); scoreTxt.setText('Score: ' + LK.getScore()); LK.getSound('goal').play(); LK.effects.flashScreen(0x00ff00, 500); LK.setTimeout(function () { resetGame(); }, 1000); } } // Check for goalkeeper block if (ball.moving && ball.intersects(goalkeeper)) { LK.getSound('block').play(); LK.effects.flashScreen(0xff0000, 500); LK.showGameOver(); } // Check if ball went off screen or stopped moving for too long if (ball.moving && (ball.x < -100 || ball.x > 2148 || ball.y > fieldY + 100)) { LK.showGameOver(); } // Check if ball stopped moving and didn't score if (!ball.moving && gameState === 'shooting') { if (!(ball.x > goalX && ball.x < goalX + goalWidth && ball.y > goalY && ball.y < goalY + goalHeight)) { LK.setTimeout(function () { LK.showGameOver(); }, 1000); } } // Track ball position for collision detection ball.lastY = ball.y; };
===================================================================
--- original.js
+++ change.js
@@ -1,6 +1,266 @@
-/****
+/****
+* 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.3;
+ self.moving = false;
+ self.update = function () {
+ if (self.moving) {
+ self.x += self.velocityX;
+ self.y += self.velocityY;
+ self.velocityY += self.gravity;
+ // Ground collision
+ if (self.y >= ballStartY) {
+ self.y = ballStartY;
+ self.velocityY *= -0.4;
+ self.velocityX *= 0.8;
+ if (Math.abs(self.velocityX) < 1 && Math.abs(self.velocityY) < 1) {
+ self.moving = false;
+ self.velocityX = 0;
+ self.velocityY = 0;
+ }
+ }
+ }
+ };
+ self.shoot = function (targetX, targetY, power) {
+ var distance = Math.sqrt((targetX - self.x) * (targetX - self.x) + (targetY - self.y) * (targetY - self.y));
+ self.velocityX = (targetX - self.x) / distance * power;
+ self.velocityY = (targetY - self.y) / distance * power;
+ self.moving = true;
+ LK.getSound('kick').play();
+ };
+ return self;
+});
+var Goalkeeper = Container.expand(function () {
+ var self = Container.call(this);
+ var keeperGraphics = self.attachAsset('goalkeeper', {
+ anchorX: 0.5,
+ anchorY: 1.0
+ });
+ self.speed = 2 + LK.getScore() * 0.3;
+ self.direction = 1;
+ self.reactionTime = 0;
+ self.maxReactionTime = 30 - LK.getScore() * 2;
+ if (self.maxReactionTime < 10) self.maxReactionTime = 10;
+ self.update = function () {
+ // Random movement
+ if (Math.random() < 0.02) {
+ self.direction *= -1;
+ }
+ self.x += self.direction * self.speed;
+ // Keep within goal bounds
+ if (self.x <= goalX + 50) {
+ self.x = goalX + 50;
+ self.direction = 1;
+ }
+ if (self.x >= goalX + goalWidth - 50) {
+ self.x = goalX + goalWidth - 50;
+ self.direction = -1;
+ }
+ // React to ball when it's moving toward goal
+ if (ball.moving && ball.y < goalY + 100) {
+ self.reactionTime++;
+ if (self.reactionTime > self.maxReactionTime) {
+ if (ball.x < self.x) {
+ self.direction = -1;
+ } else {
+ self.direction = 1;
+ }
+ }
+ } else {
+ self.reactionTime = 0;
+ }
+ };
+ return self;
+});
+
+/****
* Initialize Game
-****/
+****/
var game = new LK.Game({
- backgroundColor: 0x000000
-});
\ No newline at end of file
+ backgroundColor: 0x87CEEB
+});
+
+/****
+* Game Code
+****/
+// Game dimensions and positions
+var fieldY = 2732 - 600;
+var goalWidth = 800;
+var goalHeight = 400;
+var goalX = (2048 - goalWidth) / 2;
+var goalY = fieldY - goalHeight;
+var ballStartX = 2048 / 2;
+var ballStartY = fieldY - 40;
+// Game state
+var isAiming = false;
+var aimStartX = 0;
+var aimStartY = 0;
+var gameState = 'ready'; // 'ready', 'aiming', 'shooting', 'reset'
+// Create field
+var field = game.addChild(LK.getAsset('field', {
+ anchorX: 0,
+ anchorY: 0,
+ x: 0,
+ y: fieldY
+}));
+// Create goal
+var goal = game.addChild(LK.getAsset('goal', {
+ anchorX: 0,
+ anchorY: 0,
+ x: goalX,
+ y: goalY
+}));
+// Create goal posts (visual)
+var leftPost = game.addChild(LK.getAsset('aimLine', {
+ anchorX: 0.5,
+ anchorY: 0,
+ x: goalX,
+ y: goalY,
+ width: 12,
+ height: goalHeight,
+ color: 0xffffff
+}));
+var rightPost = game.addChild(LK.getAsset('aimLine', {
+ anchorX: 0.5,
+ anchorY: 0,
+ x: goalX + goalWidth,
+ y: goalY,
+ width: 12,
+ height: goalHeight,
+ color: 0xffffff
+}));
+var crossbar = game.addChild(LK.getAsset('aimLine', {
+ anchorX: 0,
+ anchorY: 0.5,
+ x: goalX,
+ y: goalY,
+ width: goalWidth,
+ height: 12,
+ color: 0xffffff
+}));
+// Create ball
+var ball = game.addChild(new Ball());
+ball.x = ballStartX;
+ball.y = ballStartY;
+// Create goalkeeper
+var goalkeeper = game.addChild(new Goalkeeper());
+goalkeeper.x = goalX + goalWidth / 2;
+goalkeeper.y = goalY + goalHeight;
+// Create aim line (initially hidden)
+var aimLine = game.addChild(LK.getAsset('aimLine', {
+ anchorX: 0,
+ anchorY: 0.5,
+ alpha: 0
+}));
+// Create score display
+var scoreTxt = new Text2('Score: 0', {
+ size: 80,
+ fill: 0xFFFFFF
+});
+scoreTxt.anchor.set(0.5, 0);
+LK.gui.top.addChild(scoreTxt);
+// Create instruction text
+var instructionTxt = new Text2('Drag to aim, release to shoot!', {
+ size: 60,
+ fill: 0xFFFFFF
+});
+instructionTxt.anchor.set(0.5, 1);
+instructionTxt.y = -50;
+LK.gui.bottom.addChild(instructionTxt);
+// Game event handlers
+game.down = function (x, y, obj) {
+ if (gameState === 'ready') {
+ gameState = 'aiming';
+ isAiming = true;
+ aimStartX = x;
+ aimStartY = y;
+ aimLine.alpha = 1;
+ }
+};
+game.move = function (x, y, obj) {
+ if (isAiming && gameState === 'aiming') {
+ var dx = x - ball.x;
+ var dy = y - ball.y;
+ var distance = Math.sqrt(dx * dx + dy * dy);
+ if (distance > 0) {
+ aimLine.x = ball.x;
+ aimLine.y = ball.y;
+ aimLine.width = Math.min(distance, 300);
+ aimLine.rotation = Math.atan2(dy, dx);
+ }
+ }
+};
+game.up = function (x, y, obj) {
+ if (isAiming && gameState === 'aiming') {
+ isAiming = false;
+ aimLine.alpha = 0;
+ gameState = 'shooting';
+ var dx = x - ball.x;
+ var dy = y - ball.y;
+ var distance = Math.sqrt(dx * dx + dy * dy);
+ var power = Math.min(distance / 10, 20);
+ ball.shoot(x, y, power);
+ }
+};
+// Reset function
+function resetGame() {
+ ball.x = ballStartX;
+ ball.y = ballStartY;
+ ball.moving = false;
+ ball.velocityX = 0;
+ ball.velocityY = 0;
+ gameState = 'ready';
+ goalkeeper.reactionTime = 0;
+ goalkeeper.speed = 2 + LK.getScore() * 0.3;
+ goalkeeper.maxReactionTime = 30 - LK.getScore() * 2;
+ if (goalkeeper.maxReactionTime < 10) goalkeeper.maxReactionTime = 10;
+}
+// Game update loop
+game.update = function () {
+ // Check for goal
+ if (ball.moving && ball.x > goalX && ball.x < goalX + goalWidth && ball.y > goalY && ball.y < goalY + goalHeight && ball.lastY !== undefined && ball.lastY >= goalY) {
+ // Check if goalkeeper blocked it
+ if (!ball.intersects(goalkeeper)) {
+ LK.setScore(LK.getScore() + 1);
+ scoreTxt.setText('Score: ' + LK.getScore());
+ LK.getSound('goal').play();
+ LK.effects.flashScreen(0x00ff00, 500);
+ LK.setTimeout(function () {
+ resetGame();
+ }, 1000);
+ }
+ }
+ // Check for goalkeeper block
+ if (ball.moving && ball.intersects(goalkeeper)) {
+ LK.getSound('block').play();
+ LK.effects.flashScreen(0xff0000, 500);
+ LK.showGameOver();
+ }
+ // Check if ball went off screen or stopped moving for too long
+ if (ball.moving && (ball.x < -100 || ball.x > 2148 || ball.y > fieldY + 100)) {
+ LK.showGameOver();
+ }
+ // Check if ball stopped moving and didn't score
+ if (!ball.moving && gameState === 'shooting') {
+ if (!(ball.x > goalX && ball.x < goalX + goalWidth && ball.y > goalY && ball.y < goalY + goalHeight)) {
+ LK.setTimeout(function () {
+ LK.showGameOver();
+ }, 1000);
+ }
+ }
+ // Track ball position for collision detection
+ ball.lastY = ball.y;
+};
\ No newline at end of file