User prompt
If the enemy hits the ending side of yours then you lose
User prompt
Make only one point comes when the ball reaches the other ending side
User prompt
Move it to the top right and only show the enemy's score with 1 number no letters
User prompt
The enemy's is 0 at start and display it like the player's score 1 number
User prompt
Make the enemy's score like the player's score
User prompt
Show the enemy's score and the difficultly at the bottom left
User prompt
First ask the player which difficultly then the game starts
User prompt
Make a difficultly level option easy medium and hard
Code edit (1 edits merged)
Please save this source code
User prompt
Classic Pong Challenge
Initial prompt
Make pong
/****
* 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.reset = function () {
self.x = 2048 / 2;
self.y = 2732 / 2;
// Random angle between -45 and 45 degrees (avoiding too horizontal angles)
var angle = (Math.random() * 90 - 45) * Math.PI / 180;
// Random direction (left or right)
if (Math.random() < 0.5) {
angle = Math.PI - angle;
}
var initialSpeed = 10;
self.velocityX = Math.cos(angle) * initialSpeed;
self.velocityY = Math.sin(angle) * initialSpeed;
self.baseSpeed = initialSpeed;
self.speedMultiplier = 1.0;
};
self.bounceFromPaddle = function (paddle, hitPosition) {
// Calculate bounce angle based on where the ball hit the paddle
// hitPosition is a normalized value from -1 (top) to 1 (bottom)
var bounceAngle = hitPosition * (Math.PI / 4); // Max 45-degree angle
// Determine direction based on which paddle was hit
var direction = self.x < 2048 / 2 ? 1 : -1;
// Increase speed slightly
self.speedMultiplier += 0.05;
self.speedMultiplier = Math.min(self.speedMultiplier, 2.0); // Cap at 2x original speed
var speed = self.baseSpeed * self.speedMultiplier;
// Set new velocity
self.velocityX = direction * Math.cos(bounceAngle) * speed;
self.velocityY = Math.sin(bounceAngle) * speed;
// Play hit sound
LK.getSound('hit').play();
};
self.update = function () {
// Update position
self.x += self.velocityX;
self.y += self.velocityY;
// Bounce off top and bottom walls
if (self.y < self.height / 2 || self.y > 2732 - self.height / 2) {
self.velocityY = -self.velocityY;
// Keep ball in bounds
self.y = Math.max(self.height / 2, Math.min(2732 - self.height / 2, self.y));
// Play hit sound
LK.getSound('hit').play();
}
};
return self;
});
var Paddle = Container.expand(function () {
var self = Container.call(this);
var paddleGraphics = self.attachAsset('paddle', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 12;
self.targetY = null;
self.isAI = false;
self.difficulty = 0.7; // AI difficulty - higher is better
self.moveTowards = function (targetY) {
self.targetY = targetY;
};
self.update = function () {
if (self.targetY !== null) {
// Move towards target position
var distance = self.targetY - self.y;
var moveSpeed = Math.min(Math.abs(distance), self.speed);
if (Math.abs(distance) > 1) {
self.y += distance > 0 ? moveSpeed : -moveSpeed;
}
// Constrain paddle to screen boundaries
self.y = Math.max(self.height / 2, Math.min(2732 - self.height / 2, self.y));
}
};
self.updateAI = function (ball) {
if (!self.isAI || !ball) return;
// AI prediction logic
if (ball.velocityX > 0) {
// Ball is moving toward AI
// Predict where ball will be when it reaches AI's x position
var timeToReach = (self.x - ball.x) / ball.velocityX;
var predictedY = ball.y + ball.velocityY * timeToReach;
// Add some randomness based on difficulty
var errorAmount = (1 - self.difficulty) * 300;
var randomError = Math.random() * errorAmount - errorAmount / 2;
// Move towards predicted position with some error
self.moveTowards(predictedY + randomError);
} else {
// When ball is moving away, move towards center with some randomness
var centerY = 2732 / 2;
var randomOffset = Math.random() * 100 - 50;
self.moveTowards(centerY + randomOffset);
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000
});
/****
* Game Code
****/
// Constants
var GAME_WIDTH = 2048;
var GAME_HEIGHT = 2732;
var WINNING_SCORE = 10;
// Game state
var playerScore = 0;
var aiScore = 0;
var lastScorer = null;
var gameActive = true;
var centerLineSegments = [];
// Setup score display
var playerScoreText = new Text2('0', {
size: 120,
fill: 0xFFFFFF
});
playerScoreText.anchor.set(0.5, 0);
playerScoreText.x = GAME_WIDTH / 4;
playerScoreText.y = 50;
LK.gui.addChild(playerScoreText);
var aiScoreText = new Text2('0', {
size: 120,
fill: 0xFFFFFF
});
aiScoreText.anchor.set(0.5, 0);
aiScoreText.x = GAME_WIDTH / 4 * 3;
aiScoreText.y = 50;
LK.gui.addChild(aiScoreText);
// Create center line
var LINE_SEGMENTS = 20;
var SEGMENT_HEIGHT = GAME_HEIGHT / (LINE_SEGMENTS * 2);
for (var i = 0; i < LINE_SEGMENTS; i++) {
var segment = LK.getAsset('centerLine', {
anchorX: 0.5,
anchorY: 0.5
});
segment.y = i * SEGMENT_HEIGHT * 2 + SEGMENT_HEIGHT;
segment.x = GAME_WIDTH / 2;
game.addChild(segment);
centerLineSegments.push(segment);
}
// Create paddles
var playerPaddle = new Paddle();
playerPaddle.x = 80;
playerPaddle.y = GAME_HEIGHT / 2;
game.addChild(playerPaddle);
var aiPaddle = new Paddle();
aiPaddle.x = GAME_WIDTH - 80;
aiPaddle.y = GAME_HEIGHT / 2;
aiPaddle.isAI = true;
game.addChild(aiPaddle);
// Create ball
var ball = new Ball();
ball.reset();
game.addChild(ball);
// Handle touch and mouse events
var touchActive = false;
game.down = function (x, y, obj) {
touchActive = true;
playerPaddle.moveTowards(y);
};
game.move = function (x, y, obj) {
if (touchActive) {
playerPaddle.moveTowards(y);
}
};
game.up = function (x, y, obj) {
touchActive = false;
};
// Check for paddle-ball collision
function checkPaddleCollision(paddle, ball) {
if (ball.intersects(paddle)) {
// Calculate hit position relative to paddle center (normalized from -1 to 1)
var hitPosition = (ball.y - paddle.y) / (paddle.height / 2);
hitPosition = Math.max(-1, Math.min(1, hitPosition)); // Clamp between -1 and 1
ball.bounceFromPaddle(paddle, hitPosition);
}
}
// Reset game after scoring
function resetAfterScore() {
gameActive = true;
// Reset ball with a short delay
LK.setTimeout(function () {
ball.reset();
// If AI was the last scorer, give the ball initial direction toward the player
if (lastScorer === "ai" && ball.velocityX < 0) {
ball.velocityX = -ball.velocityX;
}
// If player was the last scorer, give the ball initial direction toward the AI
else if (lastScorer === "player" && ball.velocityX > 0) {
ball.velocityX = -ball.velocityX;
}
}, 1000);
}
// Update score displays
function updateScoreDisplays() {
playerScoreText.setText(playerScore.toString());
aiScoreText.setText(aiScore.toString());
}
// Play background music
LK.playMusic('gameBgm');
// Game update loop
game.update = function () {
if (!gameActive) return;
// Update paddles
playerPaddle.update();
aiPaddle.updateAI(ball);
// Update ball
ball.update();
// Check for paddle collisions
checkPaddleCollision(playerPaddle, ball);
checkPaddleCollision(aiPaddle, ball);
// Check for scoring
if (ball.x < 0) {
// AI scores
aiScore++;
updateScoreDisplays();
lastScorer = "ai";
gameActive = false;
LK.getSound('score').play();
// Check for win
if (aiScore >= WINNING_SCORE) {
LK.setScore(playerScore); // Set player's score
LK.showGameOver();
} else {
resetAfterScore();
}
} else if (ball.x > GAME_WIDTH) {
// Player scores
playerScore++;
updateScoreDisplays();
lastScorer = "player";
gameActive = false;
LK.getSound('score').play();
// Check for win
if (playerScore >= WINNING_SCORE) {
LK.setScore(playerScore); // Set player's score
LK.showYouWin();
} else {
resetAfterScore();
}
}
// Update LK score (we'll use player's score)
LK.setScore(playerScore);
}; ===================================================================
--- original.js
+++ change.js
@@ -1,6 +1,262 @@
-/****
+/****
+* 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.reset = function () {
+ self.x = 2048 / 2;
+ self.y = 2732 / 2;
+ // Random angle between -45 and 45 degrees (avoiding too horizontal angles)
+ var angle = (Math.random() * 90 - 45) * Math.PI / 180;
+ // Random direction (left or right)
+ if (Math.random() < 0.5) {
+ angle = Math.PI - angle;
+ }
+ var initialSpeed = 10;
+ self.velocityX = Math.cos(angle) * initialSpeed;
+ self.velocityY = Math.sin(angle) * initialSpeed;
+ self.baseSpeed = initialSpeed;
+ self.speedMultiplier = 1.0;
+ };
+ self.bounceFromPaddle = function (paddle, hitPosition) {
+ // Calculate bounce angle based on where the ball hit the paddle
+ // hitPosition is a normalized value from -1 (top) to 1 (bottom)
+ var bounceAngle = hitPosition * (Math.PI / 4); // Max 45-degree angle
+ // Determine direction based on which paddle was hit
+ var direction = self.x < 2048 / 2 ? 1 : -1;
+ // Increase speed slightly
+ self.speedMultiplier += 0.05;
+ self.speedMultiplier = Math.min(self.speedMultiplier, 2.0); // Cap at 2x original speed
+ var speed = self.baseSpeed * self.speedMultiplier;
+ // Set new velocity
+ self.velocityX = direction * Math.cos(bounceAngle) * speed;
+ self.velocityY = Math.sin(bounceAngle) * speed;
+ // Play hit sound
+ LK.getSound('hit').play();
+ };
+ self.update = function () {
+ // Update position
+ self.x += self.velocityX;
+ self.y += self.velocityY;
+ // Bounce off top and bottom walls
+ if (self.y < self.height / 2 || self.y > 2732 - self.height / 2) {
+ self.velocityY = -self.velocityY;
+ // Keep ball in bounds
+ self.y = Math.max(self.height / 2, Math.min(2732 - self.height / 2, self.y));
+ // Play hit sound
+ LK.getSound('hit').play();
+ }
+ };
+ return self;
+});
+var Paddle = Container.expand(function () {
+ var self = Container.call(this);
+ var paddleGraphics = self.attachAsset('paddle', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.speed = 12;
+ self.targetY = null;
+ self.isAI = false;
+ self.difficulty = 0.7; // AI difficulty - higher is better
+ self.moveTowards = function (targetY) {
+ self.targetY = targetY;
+ };
+ self.update = function () {
+ if (self.targetY !== null) {
+ // Move towards target position
+ var distance = self.targetY - self.y;
+ var moveSpeed = Math.min(Math.abs(distance), self.speed);
+ if (Math.abs(distance) > 1) {
+ self.y += distance > 0 ? moveSpeed : -moveSpeed;
+ }
+ // Constrain paddle to screen boundaries
+ self.y = Math.max(self.height / 2, Math.min(2732 - self.height / 2, self.y));
+ }
+ };
+ self.updateAI = function (ball) {
+ if (!self.isAI || !ball) return;
+ // AI prediction logic
+ if (ball.velocityX > 0) {
+ // Ball is moving toward AI
+ // Predict where ball will be when it reaches AI's x position
+ var timeToReach = (self.x - ball.x) / ball.velocityX;
+ var predictedY = ball.y + ball.velocityY * timeToReach;
+ // Add some randomness based on difficulty
+ var errorAmount = (1 - self.difficulty) * 300;
+ var randomError = Math.random() * errorAmount - errorAmount / 2;
+ // Move towards predicted position with some error
+ self.moveTowards(predictedY + randomError);
+ } else {
+ // When ball is moving away, move towards center with some randomness
+ var centerY = 2732 / 2;
+ var randomOffset = Math.random() * 100 - 50;
+ self.moveTowards(centerY + randomOffset);
+ }
+ };
+ return self;
+});
+
+/****
* Initialize Game
-****/
+****/
var game = new LK.Game({
backgroundColor: 0x000000
-});
\ No newline at end of file
+});
+
+/****
+* Game Code
+****/
+// Constants
+var GAME_WIDTH = 2048;
+var GAME_HEIGHT = 2732;
+var WINNING_SCORE = 10;
+// Game state
+var playerScore = 0;
+var aiScore = 0;
+var lastScorer = null;
+var gameActive = true;
+var centerLineSegments = [];
+// Setup score display
+var playerScoreText = new Text2('0', {
+ size: 120,
+ fill: 0xFFFFFF
+});
+playerScoreText.anchor.set(0.5, 0);
+playerScoreText.x = GAME_WIDTH / 4;
+playerScoreText.y = 50;
+LK.gui.addChild(playerScoreText);
+var aiScoreText = new Text2('0', {
+ size: 120,
+ fill: 0xFFFFFF
+});
+aiScoreText.anchor.set(0.5, 0);
+aiScoreText.x = GAME_WIDTH / 4 * 3;
+aiScoreText.y = 50;
+LK.gui.addChild(aiScoreText);
+// Create center line
+var LINE_SEGMENTS = 20;
+var SEGMENT_HEIGHT = GAME_HEIGHT / (LINE_SEGMENTS * 2);
+for (var i = 0; i < LINE_SEGMENTS; i++) {
+ var segment = LK.getAsset('centerLine', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ segment.y = i * SEGMENT_HEIGHT * 2 + SEGMENT_HEIGHT;
+ segment.x = GAME_WIDTH / 2;
+ game.addChild(segment);
+ centerLineSegments.push(segment);
+}
+// Create paddles
+var playerPaddle = new Paddle();
+playerPaddle.x = 80;
+playerPaddle.y = GAME_HEIGHT / 2;
+game.addChild(playerPaddle);
+var aiPaddle = new Paddle();
+aiPaddle.x = GAME_WIDTH - 80;
+aiPaddle.y = GAME_HEIGHT / 2;
+aiPaddle.isAI = true;
+game.addChild(aiPaddle);
+// Create ball
+var ball = new Ball();
+ball.reset();
+game.addChild(ball);
+// Handle touch and mouse events
+var touchActive = false;
+game.down = function (x, y, obj) {
+ touchActive = true;
+ playerPaddle.moveTowards(y);
+};
+game.move = function (x, y, obj) {
+ if (touchActive) {
+ playerPaddle.moveTowards(y);
+ }
+};
+game.up = function (x, y, obj) {
+ touchActive = false;
+};
+// Check for paddle-ball collision
+function checkPaddleCollision(paddle, ball) {
+ if (ball.intersects(paddle)) {
+ // Calculate hit position relative to paddle center (normalized from -1 to 1)
+ var hitPosition = (ball.y - paddle.y) / (paddle.height / 2);
+ hitPosition = Math.max(-1, Math.min(1, hitPosition)); // Clamp between -1 and 1
+ ball.bounceFromPaddle(paddle, hitPosition);
+ }
+}
+// Reset game after scoring
+function resetAfterScore() {
+ gameActive = true;
+ // Reset ball with a short delay
+ LK.setTimeout(function () {
+ ball.reset();
+ // If AI was the last scorer, give the ball initial direction toward the player
+ if (lastScorer === "ai" && ball.velocityX < 0) {
+ ball.velocityX = -ball.velocityX;
+ }
+ // If player was the last scorer, give the ball initial direction toward the AI
+ else if (lastScorer === "player" && ball.velocityX > 0) {
+ ball.velocityX = -ball.velocityX;
+ }
+ }, 1000);
+}
+// Update score displays
+function updateScoreDisplays() {
+ playerScoreText.setText(playerScore.toString());
+ aiScoreText.setText(aiScore.toString());
+}
+// Play background music
+LK.playMusic('gameBgm');
+// Game update loop
+game.update = function () {
+ if (!gameActive) return;
+ // Update paddles
+ playerPaddle.update();
+ aiPaddle.updateAI(ball);
+ // Update ball
+ ball.update();
+ // Check for paddle collisions
+ checkPaddleCollision(playerPaddle, ball);
+ checkPaddleCollision(aiPaddle, ball);
+ // Check for scoring
+ if (ball.x < 0) {
+ // AI scores
+ aiScore++;
+ updateScoreDisplays();
+ lastScorer = "ai";
+ gameActive = false;
+ LK.getSound('score').play();
+ // Check for win
+ if (aiScore >= WINNING_SCORE) {
+ LK.setScore(playerScore); // Set player's score
+ LK.showGameOver();
+ } else {
+ resetAfterScore();
+ }
+ } else if (ball.x > GAME_WIDTH) {
+ // Player scores
+ playerScore++;
+ updateScoreDisplays();
+ lastScorer = "player";
+ gameActive = false;
+ LK.getSound('score').play();
+ // Check for win
+ if (playerScore >= WINNING_SCORE) {
+ LK.setScore(playerScore); // Set player's score
+ LK.showYouWin();
+ } else {
+ resetAfterScore();
+ }
+ }
+ // Update LK score (we'll use player's score)
+ LK.setScore(playerScore);
+};
\ No newline at end of file