I thank you all for your help and apologize for my ignorance. I've gotten the ball to bounce and everything seems to be working.
There's one small problem I noticed. When my mouseY is all the way up or down, my paddles half-disappear off screen. I tried fixing this with:
Code:
constrain(mouseY,0+hrect/2,height-hrect/2);
Yet that doesn't seem to fix the problem. At any rate, here is my game of Pong so far
:
Code:
//Set all variables
float xrect1=40;
float wrect=40;
float hrect=180;
float xrect2=760;
float xellipse=400;
float yellipse=200;
float wellipse=40;
float hellipse=40;
float xspeed=8;
float yspeed=4;
void setup() {
size (800,600);
smooth();
}
void draw() {
background(255);
rectMode(CENTER);
fill(0);
rect(xrect1,mouseY,wrect,hrect); //Paddle one
rect(xrect2,mouseY,wrect,hrect); //Paddle two
ellipse(xellipse,yellipse,wellipse,hellipse);
//I set my variables to increase speed so the ball has movement
xellipse=xellipse+xspeed;
yellipse=yellipse+yspeed;
if (xellipse-wellipse/2==xrect1+wrect/2&&yellipse>mouseY-hrect/2&&yellipse<mouseY+hrect/2) { //If the ball hits the left paddle, then
xspeed=-xspeed; //reverse speed away from the paddle
}
else if (xellipse+wellipse/2==xrect2-wrect/2&&yellipse>mouseY-hrect/2&&yellipse<mouseY+hrect/2) { //If the ball hits the right paddle, then
xspeed=-xspeed; //reverse speed away from the paddle
}
else if (yellipse+hellipse/2>height) { //If the ball hits the bottom of the screen, then
yspeed=-yspeed; //have the ball bounce off it
}
else if (yellipse-hellipse/2<0) { //If the ball hits the top of the screen, then
yspeed=-yspeed; //have the ball bounce off it
}
else if (xellipse>width||xellipse<0) { //If either paddles misses the ball, then the ball is reset
xellipse=400;
yellipse=200;
}
}