Ball passes through paddle in simple pong game
in
Programming Questions
•
7 months ago
My problem is that when ever the ball come to the left paddle which is controlled by the mouse it just passes through it even though im pretty sure the code i have inserted shuld, i have the same code for the paddle on the right and it works perfectly. Any help is great;y appreciated, Thanks
int radius = 25;
float xp, yp;
float velocityX = 1;
float velocityY = 2;
int xdirection = 1;
int ydirection = 1;
int rpX = 585;
int rpY = height/2;
int velocityRP = 5;
int lpX1 = 15;
int lpY1 = mouseY;
void setup()
{
size(600, 400);
noStroke();
smooth();
rectMode(CENTER);
ellipseMode(CENTER);
xp = width/2;
yp = height/2;
}
void draw()
{
background(175);
Bbounce();
RP();
LP();
}
void Bbounce()
{
xp = xp + (velocityX * xdirection); //ball movement
yp = yp + (velocityY * ydirection);
if(xp > width + radius)
{
xp = width/2;
yp = height/2;
velocityY = 2;
velocityX =1;
xdirection = -1;
ydirection = 1;
}
if(xp < 0)
{
xp = width/2;
yp = height/2;
velocityY = 2;
velocityX =1;
xdirection = 1;
ydirection = 1;
}
if(yp > height - radius/2 || yp < radius/2) //ball bouncing off top and bottem of screen
{
ydirection = ydirection * -1;
}
if(yp > rpY - 35 && yp < rpY + 35) //bal bouncing off right paddle
{
if((xp + radius/2 + 5) >= rpX )
{
velocityX = velocityX * -1;
}
}
if((xp - radius/2 - 5) <= lpX1)
{
if(yp > lpY1 - 35 || yp < lpY1 + 35)
{
velocityX = velocityX * -1;
}
}
ellipse(xp, yp, radius, radius);
}
void RP()
{
rect(rpX, rpY, 15, 70);
if(keyPressed)
{
if(key == CODED)
{
if(keyCode == UP)
{
rect(rpX, rpY, 15, 70);
rpY = rpY - velocityRP;
if(rpY - 35 <= 0)
rpY = rpY + 10;
}
if(keyCode == DOWN)
{
rect(rpX, rpY, 15, 70);
rpY = rpY + velocityRP;
if(rpY + 35 >= height)
rpY = rpY - 10;
}
}
}
}
void LP()
{
int lpX = 15;
int lpY = mouseY;
rect(lpX, lpY, 15, 70);
if(lpY + 35 >= 400)
{
lpY = lpY - 10;
}
}
1