User prompt
Make the green darkish.
User prompt
Make the background green.
User prompt
Make the opponent's paddle moving.
User prompt
Make it able so I can actually hit the ball. It's not working for me.
User prompt
Actually, don't make it faster.
User prompt
Make it harder
User prompt
Whenever I try to score on the opponent, the ball goes in the middle of the paddle and it kind of gets stuck. Can you fix that?
User prompt
But whenever the ball passes the player's paddle, that counts as a point for the opponent.
User prompt
Make it when the ball doesn't hit the player's paddle, it counts as the opponent's point. But when you do the same thing to the opponent, it counts as a point for you.
Initial prompt
classic pong
===================================================================
--- original.js
+++ change.js
@@ -1,48 +1,51 @@
-/****
+/****
* Classes
-****/
+****/
// Ball class
var Ball = Container.expand(function () {
- var self = Container.call(this);
- var ballGraphics = self.attachAsset('ball', {
- anchorX: 0.5,
- anchorY: 0.5
- });
- self.speedX = 5;
- self.speedY = 5;
- self.update = function () {
- self.x += self.speedX;
- self.y += self.speedY;
- };
+ var self = Container.call(this);
+ var ballGraphics = self.attachAsset('ball', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.speedX = 5;
+ self.speedY = 5;
+ self.update = function () {
+ self.x += self.speedX;
+ self.y += self.speedY;
+ };
});
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Paddle class for player and opponent
var Paddle = Container.expand(function () {
- var self = Container.call(this);
- var paddleGraphics = self.attachAsset('paddle', {
- anchorX: 0.5,
- anchorY: 0.5
- });
- self.width = 200;
- self.height = 20;
- self.speed = 10;
- self.update = function () {
- // Paddle update logic if needed
- };
+ var self = Container.call(this);
+ var paddleGraphics = self.attachAsset('paddle', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.width = 200;
+ self.height = 20;
+ self.speed = 10;
+ self.update = function () {
+ // Paddle update logic if needed
+ };
});
-/****
+/****
* Initialize Game
-****/
+****/
var game = new LK.Game({
- backgroundColor: 0x000000 //Init game with black background
+ backgroundColor: 0x000000 //Init game with black background
});
-/****
+/****
* Game Code
-****/
+****/
+// Initialize scores
+var playerScore = 0;
+var opponentScore = 0;
// Initialize paddles and ball
var playerPaddle = game.addChild(new Paddle());
var opponentPaddle = game.addChild(new Paddle());
var ball = game.addChild(new Ball());
@@ -54,28 +57,50 @@
ball.x = 2048 / 2;
ball.y = 2732 / 2;
// Game update logic
game.update = function () {
- // Ball movement
- ball.update();
- // Ball collision with walls
- if (ball.x <= 0 || ball.x >= 2048) {
- ball.speedX *= -1;
- }
- if (ball.y <= 0 || ball.y >= 2732) {
- ball.speedY *= -1;
- }
- // Ball collision with paddles
- if (ball.intersects(playerPaddle) || ball.intersects(opponentPaddle)) {
- ball.speedY *= -1;
- }
- // Opponent AI movement
- if (ball.x < opponentPaddle.x) {
- opponentPaddle.x -= opponentPaddle.speed;
- } else if (ball.x > opponentPaddle.x) {
- opponentPaddle.x += opponentPaddle.speed;
- }
+ // Ball movement
+ ball.update();
+ // Ball collision with walls
+ if (ball.x <= 0 || ball.x >= 2048) {
+ ball.speedX *= -1;
+ }
+ // Ball passes player's paddle
+ if (ball.y >= 2732) {
+ opponentScore += 1;
+ ball.x = 2048 / 2;
+ ball.y = 2732 / 2;
+ opponentScoreText.setText(opponentScore.toString());
+ }
+ // Ball passes opponent's paddle
+ if (ball.y <= 0) {
+ playerScore += 1;
+ ball.x = 2048 / 2;
+ ball.y = 2732 / 2;
+ playerScoreText.setText(playerScore.toString());
+ }
+ // Ball collision with paddles
+ if (ball.intersects(playerPaddle) || ball.intersects(opponentPaddle)) {
+ ball.speedY *= -1;
+ }
+ // Opponent AI movement
+ if (ball.x < opponentPaddle.x) {
+ opponentPaddle.x -= opponentPaddle.speed;
+ } else if (ball.x > opponentPaddle.x) {
+ opponentPaddle.x += opponentPaddle.speed;
+ }
};
// Player paddle control
game.move = function (x, y, obj) {
- playerPaddle.x = x;
-};
\ No newline at end of file
+ playerPaddle.x = x;
+};
+// Display scores
+var playerScoreText = new Text2(playerScore.toString(), {
+ size: 150,
+ fill: 0xFFFFFF
+});
+var opponentScoreText = new Text2(opponentScore.toString(), {
+ size: 150,
+ fill: 0xFFFFFF
+});
+LK.gui.top.addChild(playerScoreText);
+LK.gui.bottom.addChild(opponentScoreText);
\ No newline at end of file