Hi so far I wrote this code which has the ball bouncing from the edges.But I have the paddle here but i want it to make bouncing from the from the paddle I have literally no idea how make the ball bounce from the paddle. Its a bouncing ball game.
//defining constants
final int BALL_SIZE=20;
final int WALL_TOP=50;
final int WALL_LEFT=50;
final int WALL_SIZE=400;
final int PADDLE_LENGTH=80;
final int THICKNESS=10; //thickness of wall and paddle
float ballX, ballY;
float ballSpeedX, ballSpeedY;
float paddleX, paddleY;
//method to draw paddle
void drawPaddle(){
//finding left position of paddle based on mouse position
paddleX=mouseX-PADDLE_LENGTH/2;
//if this is beyound left wall, adjusting it
if(paddleX<WALL_LEFT){
paddleX=WALL_LEFT;
}
//if this is beyound right wall, adjusting it
if((paddleX+PADDLE_LENGTH)>(WALL_LEFT+WALL_SIZE)){
paddleX=WALL_LEFT+WALL_SIZE-PADDLE_LENGTH;
}
//using blue color
fill(0,0,255);
//drawing paddle
rect(paddleX,paddleY,PADDLE_LENGTH,THICKNESS);
}
//method to draw the three walls
void drawWall(int wallLeft, int wallTop, int wallWidth){
//using yellow color
fill(255,255,0);
//drawing top wall
rect(wallLeft,wallTop,wallWidth,THICKNESS);
//drawing left wall
rect(wallLeft,wallTop,THICKNESS,height-wallTop);
//drawing right wall
rect(wallLeft+wallWidth,wallTop,THICKNESS,height-wallTop);
}
//method to draw ball
void drawBall(float ballX, float ballY, float ballSize){
//using white color
fill(255);
//drawing ball
circle(ballX,ballY,ballSize);
}
//method to update x or y position of the ball based on speed
float moveBall(float ballXY, float speed){
return ballXY+speed;
}
void setup(){
size(500,400);
noStroke(); //no stroke lines
//placing ball somewhere on top right
ballX=width0.7;
ballY=height0.3;
//ball speed (could be direction also based on the sign)
ballSpeedX=1;
ballSpeedY=1;
//placing paddle somewhere at the bottom
paddleY=height*0.9;
}
void draw(){
//using a gray bg
background(127);
//drawing paddle
drawPaddle();
//walls
drawWall(WALL_LEFT,WALL_TOP, WALL_SIZE);
//and ball
drawBall(ballX, ballY, BALL_SIZE);
//moving ball’s x coordinates
ballX=moveBall(ballX,ballSpeedX);
//changing x direction if hit left wall
if((ballX+BALL_SIZE)>=(WALL_LEFT+WALL_SIZE+THICKNESS)){
ballSpeedX*=-1;
}
//changing x direction if hit right wall
if((ballX-BALL_SIZE)<=WALL_LEFT){
ballSpeedX*=-1;
}
//moving ball’s y coordinates
ballY=moveBall(ballY,ballSpeedY);
//changing y direction if hit top wall
if((ballY-BALL_SIZE)<=WALL_TOP){
ballSpeedY*=-1;
}
}