Creature c1; // Declare
Creature c2;
void setup() {
size(400, 400);
background(0);
smooth();
noStroke();
randomSeed(5);
frameRate(30);
c1 = new Creature();
c2 = new Creature();
c1.assignValues(width* 0.45, height/2, 40, c2);
c2.assignValues(width* 0.75, height/2.2, 45, c1);
}
void draw() {
fill (0, 40);
rect (0, 0, width, height);
tint(200, 15, 200, 255); //Purple creature
c1.move();
c1.display();
noTint(); // green creature
c2.move();
c2.display();
}
class Creature {
float x;
float y;
float imgsize = 40;
float speed = 0.8;
float direction= 1;
PImage img;
Creature other;
Creature() {
}
void assignValues(float centerx, float centery, float imgsizeIn, Creature otherCreature) {
x = centerx;
y = centery;
other = otherCreature;
img = loadImage("fuzzy.png");
imgsize = imgsizeIn;
}
void move() {
y += (speed * direction);
if( y > 700){
direction *= -1;
}
else if (y < 0){
direction*= -1;
}
if (dist(x, y, other.x, other.y) > (imgsize/2 + other.imgsize/2)) {
speed = 6.0;
}
x = x + random(-speed, speed);
y = y + random(-speed, speed);
x = constrain (x, 0, 700);
y = constrain (y, 0, 700);
}
void display() {
fill (255);
image (img, x/2, y/2, imgsize, imgsize);
}
}
Exercise K
on Wednesday, Nov 5, 2008 – 6:43 am
One Comment
Danna, the colors should be a part of the class, not assigned within draw().