Pong paddle glitch

XxChaoticStrike
Sat, 14 Sep 2019 03:53:20 +0000
Coding Questions

Hello all! First post on this forum!

I’ve built a cute little pong game using previous knowledge, however moving collision was not something I wanted to spend a lot of time on (I have made a bouncing ball example). Using code online, I was able to get a (mostly) stable pong experience. However, I am porting it to my phone, and the pong glitch is starting to get on my nerves. Unfortunately, I just started learning Processing two weeks ago (high school class), and I don’t know how I can fix the issue. Can someone please walk me through the steps needed?

Here is my example:

float paddleRectX = 50;
float paddleRectY = 0;
float paddleRectW = 14;
float paddleRectH = 150;

float bouncingBallX = 500;
float bouncingBallY = 350;
float bouncingBallW = 30;
float bouncingBallH = 30;
float bouncingBallSpeedX = -4;
float bouncingBallSpeedY = 1;

void setup() {
  size(1000, 700);
  bouncingBallX = width/2;
  bouncingBallY = height/2;
}

void draw() {
  background(200);
  paddleRectY = mouseY;
  rectMode(CENTER);
  rect(paddleRectX, paddleRectY, paddleRectW, paddleRectH);



  bouncingBallX = bouncingBallX + bouncingBallSpeedX;
  bouncingBallY = bouncingBallY + bouncingBallSpeedY;

            if (bouncingBallX + bouncingBallW + bouncingBallSpeedX > paddleRectX &&
      bouncingBallX + bouncingBallSpeedX < paddleRectX + paddleRectW &&
      bouncingBallY + bouncingBallH > paddleRectY &&
      bouncingBallY < paddleRectY + paddleRectH) { // If the ball moves, check to make sure it won't collide with the paddle

        bouncingBallSpeedX *= -1;

      } if (bouncingBallX + bouncingBallW > paddleRectX &&
      bouncingBallX < paddleRectX + paddleRectW &&
      bouncingBallY + bouncingBallH + bouncingBallSpeedY > paddleRectY &&
      bouncingBallY + bouncingBallSpeedY < paddleRectY + paddleRectH) {

        bouncingBallSpeedY *= -1;
      }

      if((bouncingBallX > width) || (bouncingBallX < 0)){
        bouncingBallSpeedX *= -1;
      }
      if((bouncingBallY > height) || (bouncingBallY < 0)){
        bouncingBallSpeedY *= -1;
      }

  ellipse(bouncingBallX, bouncingBallY, bouncingBallW, bouncingBallH);

}

Any possible tips are greatly accepted!

9 posts - 4 participants