// 1. Define classes:
class Leaf {
float x;
float y;
Leaf(float x, float y) {
this.x = x;
this.y = y;
}
void appear() { // method
move();
fill(135, 68, 70);
int size = 10;
triangle(x, y,
x + size, y + size,
x + size * .8, y - size * 0.6);
}
void move() {
y++;
}
}
class Building {
float x;
float y;
Building(float x, float y) {
this.x = x;
this.y = y;
}
void appear() { //method
move();
fill(0, 128, 20);
rect(x, y - 100, 40, 100);
}
void move() {
// no move here
}
}
class UFO {
float x;
float y;
float vx = 1;
UFO(float x, float y) {
this.x = x;
this.y = y;
}
void appear() { // method
move();
fill(255);
ellipse(x, y, 20, 10);
}
void move() {
x += vx;
if (x < 0 || x > width) {
vx = -1 * vx;
}
}
}
// 2. Declaring the Global variables:
UFO steve;
UFO ufo2;
UFO ufo3;
Building building1;
Building building2;
Building building3;
Building building4;
Building building5;
Leaf[] leaves = new Leaf[12];
// Setup and Draw:
void setup() {
size(500, 300);
// Create the instances:
steve = new UFO(10, 100);
ufo2 = new UFO(100, 75);
ufo3 = new UFO(300, 120);
building1 = new Building(75, 160);
building2 = new Building(200, 180);
building3 = new Building(220, 170);
building4 = new Building(400, 200);
building5 = new Building(450, 190);
for (int i=0; i < leaves.length; i++) {
leaves[i] = new Leaf(random(500), random(150) + 150);
}
/* Same as doing this:
leaf[0] = new Leaf();
leaf[1] = new Leaf();
leaf[2] = new Leaf();
leaf[3] = new Leaf();
leaf[4] = new Leaf();
leaf[5] = new Leaf();
leaf[6] = new Leaf();
leaf[7] = new Leaf();
leaf[8] = new Leaf();
leaf[9] = new Leaf();
leaf[10] = new Leaf();
leaf[11] = new Leaf();
*/
}
void draw() {
background(0, 0, 200);
fill(0, 255, 0);
rect(0, 150, 500, 150);
building1.appear();
building2.appear();
building3.appear();
building4.appear();
building5.appear();
for (int i=0; i < leaves.length; i++) {
leaves[i].appear();
}
steve.appear();
ufo2.appear();
ufo3.appear();
/* Same as doing this:
leaf[0].appear();
leaf[1].appear();
leaf[2].appear();
leaf[3].appear();
leaf[4].appear();
leaf[5].appear();
leaf[6].appear();
leaf[7].appear();
leaf[8].appear();
leaf[9].appear();
leaf[10].appear();
leaf[11].appear();
*/
}