class Ball { float x, y; // position float r; // radius // the following also provide initial values for the fields, // equivalent to putting that in the constructor float s = 5; // speed, intialized to 5 int dx = 1, dy = 1; // direction, initialized to diagonal // Initialize a Ball at position (x0,y0), with speed s0 and radius r0 Ball(float x0, float y0, float r0) { x = x0; y = y0; r = r0; } void draw() { fill(255); ellipse(x,y,r*2,r*2); } void update() { // Move in the appropriate direction by the step size x+=s*dx; y+=s*dy; // If too close to the wall (consider the radius of the ball), // move back and change direction if (x > width-r) { x = width-r; dx = -1; } else if (x < r) { x = r; dx = 1; } if (y > height-r) { y = height-r; dy = -1; } else if (y < r) { y = r; dy = 1; } } }