/**** * Classes ****/ // Paddle class for player and AI var Paddle = Container.expand(function () { var self = Container.call(this); var paddleGraphics = self.createAsset('paddle', 'Paddle Graphics', 0.5, 0.5); self.speed = 10; self.moveUp = function () { if (self.y > 0) { self.y -= self.speed; } }; self.moveDown = function () { if (self.y < game.height - paddleGraphics.height) { self.y += self.speed; } }; }); // Ball class var Ball = Container.expand(function () { var self = Container.call(this); var ballGraphics = self.createAsset('ball', 'Ball Graphics', 0.5, 0.5); self.velocity = { x: 5, y: 5 }; self.move = function () { self.x += self.velocity.x; self.y += self.velocity.y; }; self.reset = function () { self.x = game.width / 2; self.y = game.height / 2; self.velocity = { x: 5, y: 5 }; }; self.bounce = function (axis) { if (axis === 'x') { self.velocity.x *= -1; } else if (axis === 'y') { self.velocity.y *= -1; } }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 // Init game with black background }); /**** * Game Code ****/ // Initialize paddles and ball var playerPaddle = new Paddle(); var aiPaddle = new Paddle(); var ball = new Ball(); // Set initial positions playerPaddle.x = 100; playerPaddle.y = game.height / 2; aiPaddle.x = game.width - 100; aiPaddle.y = game.height / 2; ball.reset(); // Add paddles and ball to the game game.addChild(playerPaddle); game.addChild(aiPaddle); game.addChild(ball); // AI logic function aiMove() { if (ball.y < aiPaddle.y) { aiPaddle.moveUp(); } else if (ball.y > aiPaddle.y) { aiPaddle.moveDown(); } } // Game tick event LK.on('tick', function () { // Mouse follow behavior for player paddle game.on('move', function (obj) { var mousePos = obj.event.getLocalPosition(game); playerPaddle.y = mousePos.y - playerPaddle.height / 2; // Keep paddle within game bounds if (playerPaddle.y < 0) { playerPaddle.y = 0; } if (playerPaddle.y > game.height - playerPaddle.height) { playerPaddle.y = game.height - playerPaddle.height; } }); // Move ball ball.move(); // Ball collision with top and bottom if (ball.y <= 0 || ball.y >= game.height) { ball.bounce('y'); } // Ball collision with paddles if (ball.intersects(playerPaddle) || ball.intersects(aiPaddle)) { ball.bounce('x'); } // Ball out of bounds if (ball.x <= 0 || ball.x >= game.width) { ball.reset(); } // AI movement aiMove(); });
===================================================================
--- original.js
+++ change.js
@@ -79,8 +79,20 @@
}
}
// Game tick event
LK.on('tick', function () {
+ // Mouse follow behavior for player paddle
+ game.on('move', function (obj) {
+ var mousePos = obj.event.getLocalPosition(game);
+ playerPaddle.y = mousePos.y - playerPaddle.height / 2;
+ // Keep paddle within game bounds
+ if (playerPaddle.y < 0) {
+ playerPaddle.y = 0;
+ }
+ if (playerPaddle.y > game.height - playerPaddle.height) {
+ playerPaddle.y = game.height - playerPaddle.height;
+ }
+ });
// Move ball
ball.move();
// Ball collision with top and bottom
if (ball.y <= 0 || ball.y >= game.height) {
@@ -95,24 +107,5 @@
ball.reset();
}
// AI movement
aiMove();
- // Touch controls for player paddle
- var touchY = null;
- game.on('down', function (obj) {
- touchY = obj.event.getLocalPosition(game).y;
- });
- game.on('move', function (obj) {
- if (touchY !== null) {
- var touchPos = obj.event.getLocalPosition(game);
- if (touchPos.y < touchY - 10) {
- playerPaddle.moveUp();
- } else if (touchPos.y > touchY + 10) {
- playerPaddle.moveDown();
- }
- touchY = touchPos.y;
- }
- });
- game.on('up', function () {
- touchY = null;
- });
});
\ No newline at end of file
pong background. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
pong ball and paddle.
A settings icon. The settings icon is represented by a gear-like symbol. In this SVG (Scalable Vector Graphics) format, it consists of a circle at the center with a radius of 3 units, symbolizing a central hub. Two curved lines extend from the circle, creating a gear shape. Additionally, there's a subtle arrow-like element pointing upwards, conveying the idea of customization and adjustment. This icon is commonly used to indicate access to configuration or settings options in various applications or interfaces.
Easy text. A text that says easy
Normal text. A text that says Normal
Hard text. A text that says Hard