/*
CINDY CHI
OCT. 17, 2008
DMA 28: INTERACTIVITY
EXCERCISE H
A square that acts like a fish, swimming back and forth in the dark tank.
When user clicks somewhere among the tank the square creature is attracted
toward the mouse and moves towards it like a fish racing toward food.
When the mouse is relased there is a feeding effect that happens in which the
square grows in size, but only up to a certain point because it can't surpass the
size of the tank. If the user tries to grow the square passed 300x300 pixels
the square will die from being over-fed and be reborn as a 50x50 square.
When the mouse moves, the brightness of the square changes
depending on the distance from the square to the cursor. The closer the mouse is
to the square, the darker it turns to hide itself because it's shy.
*/
float x = 200.0;
float y = 200.0;
float sq_width = 50.0;
float targetX;
float targetY;
float direction = 1.0;
float speed = 1.0;
int sq_color = 200;
float mouse_speed = 0.0;
float easing = 0.05;
void setup()
{
size(400, 400);
background(0);
noStroke();
frameRate(30);
}
void draw()
{
targetX = mouseX;
targetY = mouseY;
background(0);
fill(sq_color);
rect(x - (sq_width/2), y - (sq_width/2), sq_width, sq_width);
//When mouse is pressed square travels towards it
if(mousePressed == true)
{
x += (targetX - x) * easing;
y += (targetY - y) * easing;
}
//otherwise square continuously moves left/right among the window
else
{
x += speed * direction;
if((x > width - (sq_width/2)) || (x < (sq_width/2)))
{
direction = -direction;
}
}
}
//When mouse is released square grows in size but is reborn if too big
void mouseReleased()
{
if(sq_width < 300)
{
sq_width += 10;
}
else
{
sq_width = 50;
}
}
void mouseMoved()
{
mouse_speed = dist(x, y, targetX, targetY);
if(mouse_speed > 255)
{
mouse_speed = 255;
}
sq_color = int(mouse_speed);
}
Cindy - H
on Tuesday, Oct 21, 2008 – 10:20 pm