// Global variables for the paddle float paddle_x; float paddle_y; int paddle_width = 50; int paddle_height = 10; int dist_wall = 25; // declare ball class objects Ball ball01; Ball ball02; Ball ball03; Ball ball04; void setup() { size(500, 500); rectMode(CENTER_RADIUS); ellipseMode(CENTER_RADIUS); noStroke(); smooth(); ball01 = new Ball(color(20, 30, 100), 100, 100, 20); ball02 = new Ball(color(90, 20, 30), 250, 10, 5.5); ball03 = new Ball(color(100, 10, 20), 300, 20, 15); ball04 = new Ball(color(70, 80, 5), 450, 200, 3.5); } void draw() { background(255); frameRate(30); ball01.display(); ball01.move(); ball02.display(); ball02.move(); ball03.display(); ball03.move(); ball04.display(); ball04.move(); // Draw the paddlex fill(100); rect(paddle_x, height-dist_wall, paddle_width, paddle_height); } class Ball { color c; float xpos; float ypos; float ball_dir = 1; float ball_size = 40; // Radius float dx = 0; // Direction float yspeed; // Speed Ball(color tempC, float tempXpos, float tempYpos, float tempYspeed) { c = tempC; xpos = tempXpos; ypos = tempYpos; yspeed = tempYspeed; } void display() { ellipseMode(CENTER); noStroke(); smooth(); fill(c); ellipse(xpos, ypos, 40, 40); paddle_x = constrain(mouseX, paddle_width, width-paddle_width); } void move() { yspeed = 10; xpos += dx; ypos += (ball_dir * yspeed); //ypos = ypos +(yspeed * ydirection); if(ypos > height+ball_size) { xpos = random(0, width); ypos = -height/2 - ball_size; dx = 0; } // PUT HERE AN IF STATEMENT TO SEE IF BALL HITS PADDLE // Test to see if the ball is touching the paddle float px = 450; if(ypos >= px && xpos > paddle_x - paddle_width - ball_size && xpos < paddle_x + paddle_width + ball_size) { ball_dir *= -1; ypos = px; if(mouseX != pmouseX) { dx = (mouseX-pmouseX)/2.0; if(dx > 10) { dx = 10; } if(dx < -10) { dx = -10; } } } // If ball hits paddle or back wall, reverse direction if(ypos < ball_size && ball_dir == -1) { ball_dir *= -1; } // If the ball is touching top or bottom edge, reverse direction if(xpos > height-ball_size) { dx = dx * -1; } if(xpos < ball_size) { dx = dx * -1; } } }