class PaddleBall { float x, y; // position float s; // speed int dx = 1, dy = 1; // direction, defaulting to diagonal float r; // radius color c = color(255,255,255); // color, starting black float a = 255; // transparency, starting opaque // Initialize at position (x0,y0), with speed s0 and radius r0 PaddleBall(float x0, float y0, float s0, float r0) { x = x0; y = y0; s = s0; r = r0; } void draw() { fill(c,a); noStroke(); ellipse(x,y,r*2,r*2); } void update() { if (a>100) a--; // fade away until hit paddle again x+=s*dx; y+=s*dy; // Have to account for thickness of wall when checking for bounce if (x > width-wallR-r) { x = width-wallR-r; dx = -1; // See if bounced off paddle if (abs(y-mouseY) < paddleR) paddle(); } else if (x < r+wallR) { x = r+wallR; dx = 1; if (abs(y-mouseY) < paddleR) paddle(); } else if (y > height-wallR-r) { y = height-wallR-r; dy = -1; if (abs(x-mouseX) < paddleR) paddle(); } else if (y < r+wallR) { y = r+wallR; dy = 1; if (abs(x-mouseX) < paddleR) paddle(); } } // When hit paddle, update the color, and go back to opaque void paddle() { c = paddleColor; a = 255; } }