INTRODUCTION
In this sketch we are going to create independent agents who draw for us. We will learn how to manage a large number of objects on the screen using only a little code.
SKETCH #5
This sketch using the concept of an object. Object-oriented programming (OOP) makes clean, human-readable code that is easily adaptable. Open Processing to a blank sketch. Click on the arrow to the right of the tab at the top of the Processing window and select "New Tab":
Now switch to the new tab you just created.
At the top of the tab we are going to define a new object. Class names are capitalized to distinguish them from variables:
float x = 0; //after we define a new class we will set its location and color using variables
float y = 0;
color col = 0;
Finally, we define how the RobotAnts will move:
x+= random(-1,1); //randomly move forward then backwards by 1 pixel
y+ = random(-1,1); //same
noStroke();
fill(col);
rect( x, y, 1, 1);
}
} //this bracket corresponds to the bracket next to the class name all of the code is under that.
Switch back to the first tab. First we initialize the objects we are going to use:
RobotAnt dave = new RobotAnt(); // here we initialized dave we say we are going to have an ant called dave from that class
RobotAnt frank = new RobotAnt();
Next we declare an object variable to determine how many ants we are going to make.
int antCount = 1000; // we are going to make 1000 ants using a new variable for the object
We are also going to set up a list called an array:
RobotAnt[] antList; //tell processing to expect an array called antList
...and now setup()
void setup() {
size(600,600);
background(255);
dave.x = width/2;
dave.y = height/2;
frank.x = width/2;
frank.y = height/2;
//populate the array so we can create ants
antList = new RobotAnt[antCount]; //make the array for new ants
for ( int i = 0; i < antCount; i ++) { //count the ants for us starting at 0 and less than 1000
println(i); //tell us what number we are at
RobotAnt newbie = new RobotAnt();
newbie.x = width/2;
newbie.y = width/2;
newbie.col = color(random(255), random(255), random(255));
antList[i] = newbie;
}
}
The void draw() portion of the code makes the ants leave a trail. Notice how it references back to the capabilities defined in the RobotAnt class:
void draw() {
dave.crawl();
frank.crawl();
for (int i = 0; i < antCount; i ++) {
antList[i].crawl();
}
}
Your finished project has the main code in the first tab and the RobotAnt class in the second tab.
Difficult - #1 : Can you figure out how to create new piles of ants where ever the user clicks? Try using void mousePressed() to start.
Easy - #2 : The ants are currently "rainbow barf" color can you redefine them to be red ants? Black ants?
Medium - #3: Experiment with different numbers of ants that are generated. Try using an ellipse. Ellipses take more processor power from the computer, but try defining the RobotAnt class as an ellipse rather than a rectangle.
More information:
Use the "Find in Reference" command and explore these commands: