//TODO: change colors, perhaps pixel by pixel? //TODO: chill out critter scare //-----decreasing probability over distance int SCREENSIZE = 300; int CRITTERCOUNT = 75; int CRITTERWIDTH=8; int CRITTERHEIGHT=10; critter c[]; int count; void setup(){ size(300,300); c = new critter[CRITTERCOUNT*10]; frameRate(30); for(int h = 1; h <= 7; h++){ PImage leftdown = loadImage(h+"-down-l.gif"); PImage rightdown = loadImage(h+"-down-r.gif"); PImage leftup = loadImage(h+"-up-l.gif"); PImage rightup = loadImage(h+"-up-r.gif"); for(int i = 0; i < CRITTERCOUNT; i++){ c[count++] = new critter(random(SCREENSIZE),random(SCREENSIZE),leftdown,rightdown,leftup,rightup); } } } void draw(){ background(4,66,90); for(int i = 0; i < count; i++){ c[i].move(mouseX,mouseY); c[i].show(); } } class critter { float x, y; PImage imgLeftDown, imgLeftUp, imgRightDown, imgRightUp; boolean isStopped = false; boolean isScared = false; boolean nowUp = true; PImage img; float xspeed = 0; float yspeed = 0; float frozX, frozY; critter(float inX, float inY, PImage inLeftDown,PImage inRightDown, PImage inLeftUp, PImage inRightUp){ x = inX; y = inY; imgLeftDown = inLeftDown; imgRightDown = inRightDown; imgLeftUp = inLeftUp; imgRightUp = inRightUp; img = inRightUp; } void show(){ if(! isStopped) nowUp = ! nowUp; if(xspeed <= 0) { if(nowUp) img = imgLeftUp; else img = imgLeftDown; } if(xspeed > 0){ if(nowUp) img = imgRightUp; else img = imgRightDown; } image(img,x,y); } boolean between(int val, int lower, int higher){ if(val >= lower && val <= higher){ return true; } return false; } void move(int mx, int my){ int ccX = (int)x+(CRITTERWIDTH/2); int ccY =(int) y+(CRITTERHEIGHT/2); int cRight = (int)x+CRITTERWIDTH; int cBottom = (int)y+CRITTERHEIGHT; if(mousePressed){ if(! isStopped){ if((between(mx,(int)x,cRight) && between(my,(int)y,cBottom))){ isStopped = true; isScared = true; frozX = mx; frozY = my; } //end else (not frozen now) } // end not previously stopped else { //previously stopped x = x + (mx-frozX); y = y + (my-frozY); frozX = mx; frozY = my; } } // end if mouse pressed else { isStopped = false; } if(! isStopped){ float d = sqrt( pow((mx - ccX),2) + pow((my-ccY),2 )); //println(d); if(d < 60 && random(100) < (60-d) ){ //line(mx,my,ccX,ccY); isScared = true; if(mx < ccX){ xspeed = 1; } else { xspeed = -1; } if(my < ccY){ yspeed = 1; } else { yspeed = -1; } }//end if random else { isScared = false; } } if(! isStopped && ! isScared){ if(int(random(3)) == 0){ xspeed = int(random(0,1)*3)-1; } if(int(random(3)) == 0){ yspeed = int(random(0,1)*3)-1; } } if(! isStopped){ x += xspeed; //random(-1,1); y += yspeed; random(-1,1); x = constrain(x, 0, SCREENSIZE-CRITTERWIDTH); y = constrain(y, 0, SCREENSIZE-CRITTERHEIGHT); } } }