Bouncing Ball OOP
Ball ball1; float x=0; float y=0; float color1=0; float color2=0; float color3=0; void setup() { size(800,800); ball1 = new Ball (0f,0f, random(255), 10.0, 5.0, 100.0); } void draw() { background(125,125,125); fill(color1,color2,color3); ball1.display(); ball1.move(); ball1.detectHorizontalWall(); ball1.detectVerticalWall(); }
Ball.pde
class Ball { float xpos; float ypos; float c; float x_velocity; float y_velocity; float diameterCircle; int diameterChange = 1; Ball (float tempXpos, float tempYpos, float tempC, float temp_X_velocity, float temp_Y_velocity, float tempDiameterCircle) { xpos = tempXpos; ypos = tempYpos; c = tempC; x_velocity = temp_X_velocity; y_velocity = temp_Y_velocity; diameterCircle = tempDiameterCircle; } void display () { ellipse(xpos, ypos, diameterCircle, diameterCircle); } void move () { xpos = xpos+x_velocity; ypos = ypos+y_velocity; } void detectHorizontalWall () { if ((xpos &rt; (750-(x_velocity/2))) && (xpos < (750+(x_velocity/2)))) { x_velocity=x_velocity*-1; color1 = random(255); color2 = random(255); color3 = random(255); ball1.changeDiameterCircle(); } else if ((x_velocity < 0) && (xpos < (50-(x_velocity/2))) && (xpos &rt; (50+(x_velocity/2)))) { x_velocity=x_velocity*-1; color1 = random(255); color2 = random(255); color3 = random(255); ball1.changeDiameterCircle(); } } void detectVerticalWall () { if ((ypos &rt; (750-(y_velocity/2))) && (ypos < (750+(y_velocity/2)))) { y_velocity=y_velocity*-1; color1 = random(255); color2 = random(255); color3 = random(255); ball1.changeDiameterCircle(); } else if ((y_velocity < 0) && (ypos < (50-(y_velocity/2))) && (ypos &rt; (50+(y_velocity/2)))) { y_velocity=y_velocity*-1; color1 = random(255); color2 = random(255); color3 = random(255); ball1.changeDiameterCircle(); } } void changeDiameterCircle() { if (diameterCircle < 750 && diameterChange == 1) { diameterCircle=diameterCircle+10; diameterChange = 1; } else if (diameterCircle == 750) { diameterChange = 0; diameterCircle = diameterCircle - 10; } else if (diameterChange == 0) { diameterCircle = diameterCircle - 10; } else if (diameterChange == 0 && diameterCircle == 10) { diameterChange = 1; diameterCircle = diameterCircle+10; } println(diameterChange); } }