Bounce Ball off of Paddle

edited August 2017 in Questions about Code

Hey, back again with another question... I got my ball to move, my paddle to display and to move! Now, I just cant seem to get the paddle to reverse the Ball's direction by having it bounce off of it. Ive tried a handful of different methods, but none have been successful. Sometimes the ball just starts bouncing up and down at the top of the screen.

Here is my Code:

    //Gwendolyn Leister - Pong Game
    //Hit the ball with the paddle by moving the mouse left and right 
    //Each time you hit the paddle, your score goes up

    //Many thanks to the two forums that helped me: 
    //https://forum.processing.org/two/discussion/23862/subclass-won-t-display-or-move-help#latest
    //https://forum.processing.org/two/discussion/23842/myclass-move-does-not-exist-but-it-does#latest


    //Global Variables
    boolean gameStart = false;

    //Declare Fonts
    PFont myFont;
    PFont myFont2;

    final GamePiece Ball = new Ball();
    final GamePiece Paddle = new Paddle();

    void setup () {
      size(800, 800);
      smooth();
      background(0);
      myFont = loadFont("AnonymousPro-30.vlw"); //smaller text font
      myFont2 = loadFont("AnonymousPro-80.vlw"); //larger title font
    } 

    void draw() {

      background (#0A141F);

      //Layout
      rectMode(CORNER);
      fill(#CBFEFF);
      noStroke();
      rect(0, 640, 800, 260);
      stroke(255);
      fill(#CBFEFF);
      strokeWeight(10);
      line(0, 640, 800, 640);
      fill(#0A141F);
      rect(200, 700, 400, 160);

      //score text 
      textFont(myFont);
      textAlign(RIGHT);
      textSize(20);
      text("Score:", 70, 25);

      //display classes
      Ball.display();
      Paddle.display();

      if (gameStart) { //if the user hits enter to start game and allow subclasses to move/bounce
        Ball.move();
        Ball.bounce();
        Paddle.move();
      } else {  //when enter is not pressed or if it is pressed twice, it will pause the game

        textAlign(CENTER);
        textFont(myFont2);
        textSize(80);
        text("P O N G", 400, 100);
        textSize(20);
        text("press enter to start", 400, 600);
        text("move mouse horizontally to control the", 400, 140);
        text("paddle and to bounce the ball off of", 400, 162);
      }
    }



    // Resource used: https://processing.org/tutorials/interactivity/
    void keyPressed() { //if enter is pressed then the game starts {
      if (keyCode == ENTER) {
        gameStart = !gameStart;
      } else {
      }
    }

    abstract class GamePiece {
      //ball variables
      int rad = 15;
      float xPos=width/2;
      float yPos=height/2;
      float Speed = 6; 
      float xDirection = -2;
      float yDirection = -1;

      int score = 0;//call booleans
      boolean mouseMoved;
      boolean overPaddle = false;
      boolean locked = false;
      float dx=0;

      //paddle variables
      int px=mouseX;
      int py=550;
      int pw=100;
      int ph=30;
      int pSpeed = 5;

      int pDistance = 250; // paddle dis


      //calls
      abstract void move(); 
      abstract void display();
      abstract void bounce();
    }


    class Ball extends GamePiece {
      //Resource used: https://processing.org/examples/bounce.html


      int rad = 15;
      float xPos=width/2;
      float yPos=height/2;
      float Speed = 5;
      float xDirection = -2;
      float yDirection = -1;

      Ball() {
        super();
      }

      void display() {
        stroke(255);
        strokeWeight(3);
        fill(#FCE1BD);
        frameRate(30);
        ellipseMode(RADIUS);
        ellipse(xPos, yPos, rad, rad);
      }
      void move() { // ball position change
        xPos=xPos+Speed*xDirection;
        yPos=yPos+Speed*yDirection;
      }
      void bounce() { //https://processing.org/examples/bounce.html

        if (yPos - rad < py + ph/2) { // bounce ball off of paddle
          yDirection = -yDirection;  // when i apply this, then the ball acts crazy

          if (xPos>(width-rad)) { //bounce right side
            xPos = width-rad;
            xDirection=-xDirection;
          }
          if (xPos< rad) { //bounce left side
            xPos = rad;
            xDirection=-xDirection;
          }
          if (yPos<rad) { //bounce top side
            yPos=rad;
            yDirection=-yDirection;
          }

          if (yPos>800) { //falls below screen and relocates ball to inside the frame
            yDirection = -1;
            xPos=width/2;
            yPos=height/2;
          }
        }
      }
    }

    class Paddle extends GamePiece {

      Paddle () {
        super();
      }

      void display() {
        rectMode(CENTER);
        fill(#CBFEFF);
        stroke(255);
        strokeWeight(5);
        rect(mouseX, py, pw, ph);  //moves paddle with mouse
      }

      void move() {
      }

      void bounce () { 
      }
    }

Answers

  • Answer ✓
    //Gwendolyn Leister - Pong Game
    //Hit the ball with the paddle by moving the mouse left and right 
    //Each time you hit the paddle, your score goes up
    
    //Many thanks to the two forums that helped me: 
    //https : // forum.processing.org/two/discussion/23862/subclass-won-t-display-or-move-help#latest
    //https : // forum.processing.org/two/discussion/23842/myclass-move-does-not-exist-but-it-does#latest    
    
    //Global Variables
    boolean gameStart = false;
    
    //Declare Fonts
    PFont myFont;
    PFont myFont2;
    
    final GamePiece Ball = new Ball();
    final GamePiece Paddle = new Paddle();
    
    void setup () {
      size(800, 800);
      smooth();
      background(0);
      myFont = createFont("AnonymousPro-30.vlw", 14); //smaller text font
      myFont2 = createFont("AnonymousPro-80.vlw", 24); //larger title font
    } 
    
    void draw() {
    
      background (#0A141F);
    
      //Layout
      rectMode(CORNER);
      fill(#CBFEFF);
      noStroke();
      rect(0, 640, 800, 260);
      stroke(255);
      fill(#CBFEFF);
      strokeWeight(10);
      line(0, 640, 800, 640);
      fill(#0A141F);
      rect(200, 700, 400, 160);
    
      //score text 
      textFont(myFont);
      textAlign(RIGHT);
      textSize(20);
      text("Score:", 70, 25);
    
      //display classes
      Ball.display();
      Paddle.display();
    
      if (gameStart) {
        //if the user hits enter to start game and allow subclasses to move/bounce
        Ball.move();
        Ball.bounce();
        Paddle.move();
      } else {  
        //when enter is not pressed or if it is pressed twice, it will pause the game
    
        textAlign(CENTER);
        textFont(myFont2);
        textSize(80);
        text("P O N G", 400, 100);
        textSize(20);
        text("press enter to start", 400, 600);
        text("move mouse horizontally to control the", 400, 140);
        text("paddle and to bounce the ball off of", 400, 162);
      }//else
    }
    
    
    // Resource used: https : // processing.org/tutorials/interactivity/
    void keyPressed() { //if enter is pressed then the game starts {
      if (keyCode == ENTER) {
        gameStart = !gameStart;
      } else {
      }
    }
    
    // =======================================================================
    
    abstract class GamePiece {
      //ball variables
      int rad = 15;
      float xPos=width/2;
      float yPos=height/2;
      float Speed = 6; 
      float xDirection = -2;
      float yDirection = -1;
    
      int score = 0;//call booleans
      boolean mouseMoved;
      boolean overPaddle = false;
      boolean locked = false;
      float dx=0;
    
      //paddle variables
      int px=mouseX;
      int py=550;
      int pw=100;
      int ph=30;
      int pSpeed = 5;
    
      int pDistance = 250; // paddle dis
    
    
      //calls
      abstract void move(); 
      abstract void display();
      abstract void bounce();
    }
    
    
    class Ball extends GamePiece {
      //Resource used: https : // processing.org/examples/bounce.html
    
    
      int rad = 15;
      float xPos=width/2;
      float yPos=height/2;
      float Speed = 5;
      float xDirection = -2;
      float yDirection = -1;
    
      Ball() {
        super();
      }
    
      void display() {
        stroke(255);
        strokeWeight(3);
        fill(#FCE1BD);
        frameRate(30);
        ellipseMode(RADIUS);
        ellipse(xPos, yPos, rad, rad);
      }
    
      void move() { // ball position change
        xPos=xPos+Speed*xDirection;
        yPos=yPos+Speed*yDirection;
      }
    
      void bounce() {
    
        // https : // processing.org/examples/bounce.html
    
        if (xPos > mouseX-pw/2 && xPos < mouseX+pw/2 &&
          yPos + rad > py - ph/2) { 
          // bounce ball off of paddle
          yDirection = -abs(yDirection);  // some corrections here
        }
    
        if (xPos>(width-rad)) { //bounce right side
          xPos = width-rad;
          xDirection=-abs(xDirection);
        }
        if (xPos<rad) { //bounce left side
          xPos = rad;
          xDirection=abs(xDirection);
        }
        if (yPos<rad) { //bounce top side
          yPos = rad;
          yDirection=abs(yDirection);
        }
    
        if (yPos>800) {
          //falls below screen and relocates ball to inside the frame
          yDirection = -1;
          xPos=width/2;
          yPos=height/2;
        }
      }
    }
    
    class Paddle extends GamePiece {
    
      Paddle () {
        super();
      }
    
      void display() {
        rectMode(CENTER);
        fill(#CBFEFF);
        stroke(255);
        strokeWeight(5);
        rect(mouseX, py, pw, ph);  //moves paddle with mouse
      }
    
      void move() {
      }
    
      void bounce () {
      }
    }
    //
    
  • edited August 2017 Answer ✓

    in your code (not my post but yours) in line 142: if (yPos - rad < py + ph/2) {

    This bracket { was closing only at line 163 (}) and so enclosing all other reflections. Bad.

    I also made some corrections in the line 142 itself.

    Using abs

    I also changed yDirection = -yDirection; to either

    • yDirection = -abs(yDirection); // always negativeor
    • yDirection = abs(yDirection); // always positive

    to make the sign (if it's positive or negative direction) that yDirection needs to have fix. The word abs() says give me the positive value (the absolute) of yDirection no matter if yDirection is currently positive or negative.

  • Thank you so much for your eye! I have made the adjustments and everything seems to be working properly now... Many thanks, Chrisir!

  • Final thought: My next step is to have the score add up with each bounce off of the paddle. Should I implement this in my main code or within the bounce off of paddle if statement?

  • That's great!

  • score within the bounce off of paddle if statement

  • I have written text("Score = "+score, 30, 30); to display the score in the top left corner in void display() and implemented score=score+1; into the bounce off of paddle if statement... This doesn't seem to adjust the score, but since I call the text to be displayed before the bouncing, would putting it later in my code be the solution?

  • Answer: No :( Score still stays at 0 (tried hitting it 5 times , still nothing)

  • Answer ✓

    important is that you don't have two scores in your program

    • only the one in line 92 in my code above
  • only have one score

    post your entire code

  • Ah! that did it. I had int score as a global variable and a local variable within gamePiece.. Now that I deleted the one on line 92, the counter is working now. Thanks again for all your help! My program is working just the way I wanted it to now.

  • maybe gotoloop can also add his opinion but in my opinion your design of the class is still utterly wrong.

    abstract class GamePiece should not carry xpos for the ball and px for the paddle but only one xpos that gets used either for the ball or the paddle.

    Hence the class is called GamePiece since its fields xpos can be used for different elements in the game. The way you do it is wasting resources.

Sign In or Register to comment.