Files
sunny9898/task8/wk9/wk9_example_5b/Circle.pde
louiscklaw 5637fbf94f update,
2025-02-01 02:07:58 +08:00

35 lines
701 B
Plaintext

class Circle {
int x; // x coordinates
int y; // y coordinates
float sx; // speed in x direction
float sy; // speed in y direction
int d; // diameters
color c; // colors
Circle(int scale, color c) {
x = width/2;
y = height/2;
d = scale;
while (sx==0)
sx = random(-10, 10);
while (sy==0)
sy = random(-10, 10);
this.c = c;
}
void move() {
// Update circle's position
x += sx;
y += sy;
// Bounce back when touch the wall
if ((sx<0 && x<d/2) || (sx>0 && x>width-d/2))
sx *= -1;
if ((sy<0 && y<d/2) || (sy>0 && y>height-d/2))
sy *= -1;
}
void display() {
fill(c);
ellipse(x, y, d, d);
}
}