Lab 05 — Classes, This & Methods
The balls do weird things.
Notes
Methods in the Ball class:
show(): draws the ball as a circle in its own color, at its current position and size.move(): nudges the ball a few pixels in a random direction, so it wanders.
Updated methods:
changeSize(minR, maxR): randomly grows or shrinks the ball a little, constraining its size betweenminRandmaxR.
New methods:
updatePosition(x, y): moves the ball to a new spot and resets its falling speed.swirl(mouseX, mouseY): moves the ball around in a circle centered on the mouse.fall(): pulls the ball down with gravity and bounces it off the bottom of the canvas, a bit lower each time.
Interactions:
- Ten balls wander around, starting from the middle of the canvas by default.
- The color of all of the balls is unique, and the size keeps randomly incrementing or decrementing making them appear jittery.
- Mouse Click anywhere on the canvas makes them appear at the mouse position with a random increment/decrement of 50 pixels and gravity is activated, so they fall to the bottom of the canvas.
- Hold space to make them swirl around the mouse.
- Press R to turn gravity off and reset the balls to the middle of the canvas.