35 lines
701 B
Plaintext
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);
|
|
}
|
|
} |