how to increase the speed of the ball when it hits the edge of a paddle and then return back to original speed?

Refresh

2 weeks ago

Views

4 time

1

how to increase the speed of the ball when it hits the edge of a paddle and then return back to original speed? In this pong game, I want my ball to increase its speed when it hits the edge of a paddle and then return back to its normal speed when it hits the middle of the paddle. i commented a few "if statements" because i tried with them and they did not work. The language I am using is "processing"

    //size of the ball
int ballSize;

//position of the ball
float ballPositionX; 
float ballPositionY; 

//speed of the ball
float ballSpeedX, ballSpeedY;

//size of the paddles
final int PADDLE_WIDTH = (20);
final int PADDLE_HEIGHT = (100);

//coordinates of the LEFT paddle
float keyPaddleY;
float keyPaddleX;

//coordinates of the RIGHT paddle
float mousePaddleX ;
float mousePaddleY;

//score of keyboard
int keyScore = 0;

//score of mouse
int mouseScore = 0;

//movement of the ball
boolean moveDown = true;
boolean moveRight = true;

//gameover
boolean gameOver, moveNewSpeed;

//paddle speed
int paddleSpeed;

final int MIN_SPEED = 3;
final int MAX_SPEED = 6;

void setup () {

  size (500, 500);
  background (#FF7C00);
  frameRate (60);
  ballPositionX = width/2;
  ballPositionY = height/2;

  ballSpeedX = 3;
  ballSpeedY = 3;

  keyPaddleY= 200;
  keyPaddleX = 20;

  mousePaddleX= width - 40;
  mousePaddleY = 200;

  ballSize = 20;

  keyScore = 0;
  mouseScore = 0;

  paddleSpeed = 4;
}

void draw () {
  background (#FF7C00);
  drawGame ();
  bounce ();
  scoreCount ();
  paddleBounce ();
  gameover ();
  canvasBounce ();

  if (keyPressed) {

    if (keyCode == UP) {

      keyPaddleY = keyPaddleY - paddleSpeed;
    }

    /*if ((keyPaddleY == 0) || (keyPaddleY == height )) {

      paddleSpeed = 0;

    }*/ 

    if (keyCode == DOWN) {

      keyPaddleY = keyPaddleY + paddleSpeed;
    }
  }

  /*if ((mousePaddleY == 0) || (mousePaddleY == height )) {

      paddleSpeed = 0;

    }*/

  if (mousePressed) {

    if (mouseButton == LEFT ) {

      mousePaddleY = mousePaddleY - paddleSpeed;
    } 

    if (mouseButton == RIGHT ) {

      mousePaddleY = mousePaddleY + paddleSpeed;
    }
  }
}//VOID BRAC
void drawGame () {

  drawScore ();
  defaultBall ();


  //the ball
  fill ( 255);
  strokeWeight (2);
  ellipse (ballPositionX, ballPositionY, ballSize, ballSize);

  fill (255);
  strokeWeight (0.8);
  //the left paddle, which is controlled by the keyboard
  rect (keyPaddleX, keyPaddleY, PADDLE_WIDTH, PADDLE_HEIGHT);

  //the right paddle, which is controlled by the mouse
  rect (mousePaddleX, mousePaddleY, PADDLE_WIDTH, PADDLE_HEIGHT);
}

void drawScore() {

  textSize(20);

  String toPrint = "Keyboard: " + keyScore;

  text(toPrint, width/4-textWidth(toPrint)/2, 50);

  toPrint = "Mouse: "+ mouseScore;

  text(toPrint, width*3/4-textWidth(toPrint)/2, 50);
}

/*this funtion puts the ball back into the centre of the screen when it fails to hit either
 the paddle or the top or the bottom of the screen 
 */
void defaultBall () {

  if ((ballPositionX < -ballSize/2) || (ballPositionX > width + ballSize)) {

    ballPositionX = width/2;
    ballPositionY = height/2;
  }
}

void scoreCount () {

  if (ballPositionX  < -ballSize/2) {

    mouseScore = mouseScore + 1;
  }

  if (ballPositionX > width + ballSize) {

    keyScore =  keyScore + 1;
  }
}


void bounce () {
  //bouncing off the top and bottom of the screen  
  if (moveDown) {

    ballPositionY += MIN_SPEED +(sin(QUARTER_PI));
  } else {

    ballPositionY -= MIN_SPEED + (sin(QUARTER_PI));
  }

  if (moveRight) {

    ballPositionX += MIN_SPEED + (sin(HALF_PI + QUARTER_PI));

  } else {

    ballPositionX -= MIN_SPEED + (sin(-PI));
  }

  /*if (moveNewSpeed) {

   ballPositionX = ballPositionX + MAX_SPEED + (sin(QUARTER_PI));
   } else {

   ballPositionX = ballPositionX - MAX_SPEED + (cos(QUARTER_PI));
   }
  */
}

void paddleBounce () {


  if (ballPositionX <= (keyPaddleX + PADDLE_WIDTH + ballSize/2) && ballPositionY < (keyPaddleY + 100) && ballPositionY > (keyPaddleY) && ballPositionX > keyPaddleX  ) {

    moveRight = true;

  }


  if (ballPositionX >= (mousePaddleX - ballSize/2) && ballPositionY > (mousePaddleY) && ballPositionY < (mousePaddleY + 100) && ballPositionX < mousePaddleX ) {

    moveRight = false;

  }

  /*if (ballPositionX == keyPaddleX + PADDLE_WIDTH && ballPositionY == keyPaddleY + PADDLE_WIDTH ) {

   moveNewSpeed  = false;  

   } 

   if (ballPositionX == mousePaddleX && ballPositionY == mousePaddleY) {

   moveNewSpeed = true;

   }   

   */

}

void canvasBounce () {

  if (ballPositionY < ballSize/2) {

    moveDown = true;
  }

  if (ballPositionY > height - ballSize/2) {

    moveDown = false;
  }

}

void gameover () {

  if (gameOver) {

    ballSpeedX = 0;
    ballSpeedY = 0;
    paddleSpeed = 0;
    ballPositionX = width/2;
    ballPositionY = height/2;

    textSize(50);

    fill (random(255), random(255), random (255));

    String toPrint = "GAME OVER!!";

    text(toPrint, width/2-textWidth(toPrint)/2, height/2);
  }

  if (keyScore == 11 || mouseScore == 11) {

    gameOver = true;
  } else {

    gameOver = false;
  }
}


----------
## 


----------


 ##

0 answers