Learning Processing 5

I have now been learning processing for over a month and the difficulty has started to ramp in. I am quite enjoying this, using two sheets of code and linking them up will make coding complex programs; like my installation, easier. The methods and complex algorithms; though perhaps calling everything agent makes it harder to understand and having different names for  everything would be better and less confusing.

processing5

Code:

Agent ourAgent;
ArrayList agents;

void setup(){
size(600,600);

agents = new ArrayList();
}

void draw(){
background(222);

for(int i = 0; i < agents.size(); i++){
Agent tempA = (Agent)agents.get(i);
tempA.update();
tempA.draw();
}

}

void mousePressed(){
Agent ourAgent;
ourAgent = new Agent(mouseX,mouseY,color(random(255),random(255),random(255)));
agents.add(ourAgent);
println(agents.size());
}

Agent

class Agent{

//class data
float x,y;
color c;

//constructor
Agent(int x, int y, color c){
this.x=x;
this.y=y;
this.c=c;
}
//classmethods
void update(){
x += random(-1,1);
y += random(-1,1);
}

void draw(){
fill(c);
rect(x,y,10,10);
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s