User prompt
add easy medium and hard ai choises on the home screen
User prompt
Please fix the bug: 'Cannot read properties of undefined (reading 'reset')' in or related to this line: 'ball.reset();' Line Number: 398
User prompt
Please fix the bug: 'Cannot read properties of undefined (reading 'x')' in or related to this line: 'playerScoreText.x = table.x - 200;' Line Number: 301
User prompt
Please fix the bug: 'Cannot read properties of undefined (reading 'width')' in or related to this line: 'var originalPaddleWidth = playerPaddle.width;' Line Number: 288
User prompt
add homescreen
User prompt
add powerupps
User prompt
add win and lose when ten points is reached
User prompt
ad a ping pong table
User prompt
Classic Ping Pong
Initial prompt
standard table tennis
/**** * Classes ****/ var Ball = Container.expand(function () { var self = Container.call(this); var ballGraphics = self.attachAsset('ball', { anchorX: 0.5, anchorY: 0.5 }); self.width = ballGraphics.width; self.height = ballGraphics.height; self.velocityX = 0; self.velocityY = 0; self.speed = 10; self.maxSpeed = 20; self.active = false; self.lastX = 0; self.lastY = 0; self.reset = function () { self.x = table.x; self.y = table.y; self.velocityY = self.speed; self.velocityX = Math.random() * 6 - 3; self.active = true; self.lastX = self.x; self.lastY = self.y; }; self.update = function () { if (!self.active) { return; } self.lastX = self.x; self.lastY = self.y; self.x += self.velocityX; self.y += self.velocityY; // Side wall collision if (self.x < table.x - table.width / 2 + self.width / 2 || self.x > table.x + table.width / 2 - self.width / 2) { self.velocityX = -self.velocityX; LK.getSound('hit').play(); } }; self.checkPaddleCollision = function (paddle) { if (self.intersects(paddle) && (self.velocityY > 0 && self.y < paddle.y || self.velocityY < 0 && self.y > paddle.y)) { // Calculate bounce angle based on where ball hit paddle var hitPos = (self.x - paddle.x) / (paddle.width / 2); self.velocityX = hitPos * 10; // Reverse Y velocity self.velocityY = -self.velocityY; // Increase speed slightly if (Math.abs(self.velocityY) < self.maxSpeed) { self.velocityY *= 1.05; } LK.getSound('hit').play(); return true; } return false; }; return self; }); // Game dimensions are 2048x2732 var Paddle = Container.expand(function () { var self = Container.call(this); var paddleGraphics = self.attachAsset('paddle', { anchorX: 0.5, anchorY: 0.5 }); self.width = paddleGraphics.width; self.height = paddleGraphics.height; self.speed = 10; self.isAI = false; self.targetX = 0; self.update = function () { if (self.isAI) { // AI movement logic if (Math.abs(self.x - self.targetX) > self.speed) { if (self.x < self.targetX) { self.x += self.speed; } else { self.x -= self.speed; } } } // Keep paddle within table bounds if (self.x < table.x - table.width / 2 + self.width / 2 + 20) { self.x = table.x - table.width / 2 + self.width / 2 + 20; } else if (self.x > table.x + table.width / 2 - self.width / 2 - 20) { self.x = table.x + table.width / 2 - self.width / 2 - 20; } }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 }); /**** * Game Code ****/ // Game dimensions are 2048x2732 game.setBackgroundColor(0x1a1a1a); // Create table var table = game.addChild(LK.getAsset('table', { anchorX: 0.5, anchorY: 0.5, x: 2048 / 2, y: 2732 / 2 })); // Create net var net = game.addChild(LK.getAsset('net', { anchorX: 0.5, anchorY: 0.5, x: 2048 / 2, y: 2732 / 2 })); // Create player paddle var playerPaddle = new Paddle(); playerPaddle.x = 2048 / 2; playerPaddle.y = table.y + table.height / 2 - playerPaddle.height - 50; game.addChild(playerPaddle); // Create AI paddle var aiPaddle = new Paddle(); aiPaddle.x = 2048 / 2; aiPaddle.y = table.y - table.height / 2 + aiPaddle.height + 50; aiPaddle.isAI = true; game.addChild(aiPaddle); // Create ball var ball = new Ball(); game.addChild(ball); // Create score text var playerScore = 0; var aiScore = 0; var playerScoreText = new Text2(playerScore.toString(), { size: 80, fill: 0xFFFFFF }); playerScoreText.anchor.set(0.5, 0.5); playerScoreText.x = table.x - 200; playerScoreText.y = table.y; game.addChild(playerScoreText); var aiScoreText = new Text2(aiScore.toString(), { size: 80, fill: 0xFFFFFF }); aiScoreText.anchor.set(0.5, 0.5); aiScoreText.x = table.x + 200; aiScoreText.y = table.y; game.addChild(aiScoreText); // Handle paddle dragging var isDragging = false; game.down = function (x, y, obj) { isDragging = true; playerPaddle.x = x; }; game.up = function (x, y, obj) { isDragging = false; }; game.move = function (x, y, obj) { if (isDragging) { playerPaddle.x = x; } }; // Start the game with the ball ball.reset(); // Main game loop game.update = function () { // Update game objects playerPaddle.update(); aiPaddle.update(); ball.update(); // AI behavior - track the ball if (ball.active) { aiPaddle.targetX = ball.x; // Add some difficulty - AI gets less accurate at higher speeds var difficultyFactor = Math.min(Math.abs(ball.velocityY) / 15, 1); var randomOffset = (Math.random() * 300 - 150) * difficultyFactor; aiPaddle.targetX += randomOffset; } // Check for collisions ball.checkPaddleCollision(playerPaddle); ball.checkPaddleCollision(aiPaddle); // Check if ball goes off table (scoring) if (ball.active) { // Player scores if (ball.y < table.y - table.height / 2) { playerScore++; playerScoreText.setText(playerScore.toString()); LK.getSound('score').play(); // Reset ball after short delay ball.active = false; LK.setTimeout(function () { ball.reset(); }, 1000); // Check win condition if (playerScore >= 11 && playerScore - aiScore >= 2) { LK.showYouWin(); } } // AI scores if (ball.y > table.y + table.height / 2) { aiScore++; aiScoreText.setText(aiScore.toString()); LK.getSound('score').play(); // Reset ball after short delay ball.active = false; LK.setTimeout(function () { ball.reset(); }, 1000); // Check loss condition if (aiScore >= 11 && aiScore - playerScore >= 2) { LK.showGameOver(); } } } // Update score in LK system LK.setScore(playerScore); };
===================================================================
--- original.js
+++ change.js
@@ -1,6 +1,220 @@
-/****
+/****
+* Classes
+****/
+var Ball = Container.expand(function () {
+ var self = Container.call(this);
+ var ballGraphics = self.attachAsset('ball', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.width = ballGraphics.width;
+ self.height = ballGraphics.height;
+ self.velocityX = 0;
+ self.velocityY = 0;
+ self.speed = 10;
+ self.maxSpeed = 20;
+ self.active = false;
+ self.lastX = 0;
+ self.lastY = 0;
+ self.reset = function () {
+ self.x = table.x;
+ self.y = table.y;
+ self.velocityY = self.speed;
+ self.velocityX = Math.random() * 6 - 3;
+ self.active = true;
+ self.lastX = self.x;
+ self.lastY = self.y;
+ };
+ self.update = function () {
+ if (!self.active) {
+ return;
+ }
+ self.lastX = self.x;
+ self.lastY = self.y;
+ self.x += self.velocityX;
+ self.y += self.velocityY;
+ // Side wall collision
+ if (self.x < table.x - table.width / 2 + self.width / 2 || self.x > table.x + table.width / 2 - self.width / 2) {
+ self.velocityX = -self.velocityX;
+ LK.getSound('hit').play();
+ }
+ };
+ self.checkPaddleCollision = function (paddle) {
+ if (self.intersects(paddle) && (self.velocityY > 0 && self.y < paddle.y || self.velocityY < 0 && self.y > paddle.y)) {
+ // Calculate bounce angle based on where ball hit paddle
+ var hitPos = (self.x - paddle.x) / (paddle.width / 2);
+ self.velocityX = hitPos * 10;
+ // Reverse Y velocity
+ self.velocityY = -self.velocityY;
+ // Increase speed slightly
+ if (Math.abs(self.velocityY) < self.maxSpeed) {
+ self.velocityY *= 1.05;
+ }
+ LK.getSound('hit').play();
+ return true;
+ }
+ return false;
+ };
+ return self;
+});
+// Game dimensions are 2048x2732
+var Paddle = Container.expand(function () {
+ var self = Container.call(this);
+ var paddleGraphics = self.attachAsset('paddle', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.width = paddleGraphics.width;
+ self.height = paddleGraphics.height;
+ self.speed = 10;
+ self.isAI = false;
+ self.targetX = 0;
+ self.update = function () {
+ if (self.isAI) {
+ // AI movement logic
+ if (Math.abs(self.x - self.targetX) > self.speed) {
+ if (self.x < self.targetX) {
+ self.x += self.speed;
+ } else {
+ self.x -= self.speed;
+ }
+ }
+ }
+ // Keep paddle within table bounds
+ if (self.x < table.x - table.width / 2 + self.width / 2 + 20) {
+ self.x = table.x - table.width / 2 + self.width / 2 + 20;
+ } else if (self.x > table.x + table.width / 2 - self.width / 2 - 20) {
+ self.x = table.x + table.width / 2 - self.width / 2 - 20;
+ }
+ };
+ return self;
+});
+
+/****
* Initialize Game
-****/
+****/
var game = new LK.Game({
backgroundColor: 0x000000
-});
\ No newline at end of file
+});
+
+/****
+* Game Code
+****/
+// Game dimensions are 2048x2732
+game.setBackgroundColor(0x1a1a1a);
+// Create table
+var table = game.addChild(LK.getAsset('table', {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ x: 2048 / 2,
+ y: 2732 / 2
+}));
+// Create net
+var net = game.addChild(LK.getAsset('net', {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ x: 2048 / 2,
+ y: 2732 / 2
+}));
+// Create player paddle
+var playerPaddle = new Paddle();
+playerPaddle.x = 2048 / 2;
+playerPaddle.y = table.y + table.height / 2 - playerPaddle.height - 50;
+game.addChild(playerPaddle);
+// Create AI paddle
+var aiPaddle = new Paddle();
+aiPaddle.x = 2048 / 2;
+aiPaddle.y = table.y - table.height / 2 + aiPaddle.height + 50;
+aiPaddle.isAI = true;
+game.addChild(aiPaddle);
+// Create ball
+var ball = new Ball();
+game.addChild(ball);
+// Create score text
+var playerScore = 0;
+var aiScore = 0;
+var playerScoreText = new Text2(playerScore.toString(), {
+ size: 80,
+ fill: 0xFFFFFF
+});
+playerScoreText.anchor.set(0.5, 0.5);
+playerScoreText.x = table.x - 200;
+playerScoreText.y = table.y;
+game.addChild(playerScoreText);
+var aiScoreText = new Text2(aiScore.toString(), {
+ size: 80,
+ fill: 0xFFFFFF
+});
+aiScoreText.anchor.set(0.5, 0.5);
+aiScoreText.x = table.x + 200;
+aiScoreText.y = table.y;
+game.addChild(aiScoreText);
+// Handle paddle dragging
+var isDragging = false;
+game.down = function (x, y, obj) {
+ isDragging = true;
+ playerPaddle.x = x;
+};
+game.up = function (x, y, obj) {
+ isDragging = false;
+};
+game.move = function (x, y, obj) {
+ if (isDragging) {
+ playerPaddle.x = x;
+ }
+};
+// Start the game with the ball
+ball.reset();
+// Main game loop
+game.update = function () {
+ // Update game objects
+ playerPaddle.update();
+ aiPaddle.update();
+ ball.update();
+ // AI behavior - track the ball
+ if (ball.active) {
+ aiPaddle.targetX = ball.x;
+ // Add some difficulty - AI gets less accurate at higher speeds
+ var difficultyFactor = Math.min(Math.abs(ball.velocityY) / 15, 1);
+ var randomOffset = (Math.random() * 300 - 150) * difficultyFactor;
+ aiPaddle.targetX += randomOffset;
+ }
+ // Check for collisions
+ ball.checkPaddleCollision(playerPaddle);
+ ball.checkPaddleCollision(aiPaddle);
+ // Check if ball goes off table (scoring)
+ if (ball.active) {
+ // Player scores
+ if (ball.y < table.y - table.height / 2) {
+ playerScore++;
+ playerScoreText.setText(playerScore.toString());
+ LK.getSound('score').play();
+ // Reset ball after short delay
+ ball.active = false;
+ LK.setTimeout(function () {
+ ball.reset();
+ }, 1000);
+ // Check win condition
+ if (playerScore >= 11 && playerScore - aiScore >= 2) {
+ LK.showYouWin();
+ }
+ }
+ // AI scores
+ if (ball.y > table.y + table.height / 2) {
+ aiScore++;
+ aiScoreText.setText(aiScore.toString());
+ LK.getSound('score').play();
+ // Reset ball after short delay
+ ball.active = false;
+ LK.setTimeout(function () {
+ ball.reset();
+ }, 1000);
+ // Check loss condition
+ if (aiScore >= 11 && aiScore - playerScore >= 2) {
+ LK.showGameOver();
+ }
+ }
+ }
+ // Update score in LK system
+ LK.setScore(playerScore);
+};
\ No newline at end of file