UPDATED: Creating a Game: Just need a kick in the right direction

Hopefully I formatted this right… So this is the code I have so far on making a somewhat pong game but also that game where you bounce the ball off the bar (rect) (like that game where you break bricks with the ball). I got the bar (rect) moving like I want it to, but I need help getting the ball to bounce off the bar itself. Maybe new int. or some codes that would achieve that? Not worried about the win or lose part yet.

float ballX;             
float ballY;              
float xspeed = random (-100, 100); 
float yspeed = random (-100, 100); 
int playerA, playerB; 


void setup (){          
size (800, 500);       
ballX = 0; 
ballY = 0;  
playerA = width; 
playerB = height; 

}

void draw() {    
background (0);  
drawBall (); 
drawPlayer (); 
}
void drawBall () { 
fill (255);
ellipse (ballX, ballY, 20, 20);   
ballX = ballX + xspeed;    
ballY = ballY + yspeed;    

if (ballX > width) {    
xspeed = -10;    
}
if (ballX < 0){ 
xspeed = 10;
}    
if (ballY > height) { 
yspeed = -10;
}    
if (ballY < 0) {  
yspeed = 10;
}   
}

void drawPlayer () { 
  fill (235, 23, 54); 
  rect (mouseX, playerB/2+140, 100, 20); // x, y coordinates of top left corner then width, then height 
}   

try: rect(mouseX, ,100,20);

1 Like