// KEY GUIDE // space - toggles volumes // r - reset // p - print number of live bugs // Set gravity, friction, and mode. float gravity = 1; float friction = 0.98; int mode = 4; int maxSize = 10; // load Processing font into PFont variable PFont fontA; PFont fontB; // drawVolume initially set to "true" // so volumes are drawn boolean drawVolume = true; // numBugs is set to -1 NOT 0 because // the first bug created is bug[0]. int numBugs = -1; // Set up the ArrayList for "bugs" ArrayList bugs; void setup() { size(500,500); background(102); smooth(); frameRate(30); fontA = loadFont("ArnoPro-BoldCaption-20.vlw"); fontB = loadFont("ArnoPro-Regular-14.vlw"); bugs = new ArrayList(); //bugs = new Crawler[10]; } void draw() { background(0); drawGuide(); for (int i = bugs.size() - 1; i > 0; i--) { Crawler c = (Crawler) bugs.get(i); c.move(); c.display(); if (c.dead()) { bugs.remove(i); numBugs--; } } } void mouseReleased() { int rand = int(random(5,40)); for (int i=0; i 10) { maxSize--; } } } void resetBugs() { for(int i=numBugs; i>0; i--) { bugs.remove(i); numBugs--; } } void drawGuide() { textFont(fontA); textMode(MODEL); fill(150); text("Guide:", 20, 30); textFont(fontB); textMode(MODEL); fill(100); text("click the mouse anywhere in the applet to spawn new objects", 40, 50); text("space - toggles object volumes on and off", 40, 64); text("r - clears all objects (use if the applet start to slow down)", 65, 78); text("1 - Bounce mode", 65, 92); text("2 - Fall-wrap mode", 65, 106); text("3 - Fall-bounce mode", 65, 120); text("minus (-) - Decrease maximum object size", 200, 92); text("plus (+) - Increase maximum object size", 207, 106); text("Current maximum object size is " + str(maxSize) + ".", 200, 120); } //--------CLASS DEFS----------------// class Bug { float vx; float vy; float ax; float ay; float xpos; float ypos; float maxDist; int life = 100; float isize; float c; Bug() { maxDist = random(height/4,height/2); c = int(random(50,255)); isize = map(c,0,255,2,maxSize); } void move () { vy += gravity; //vx *= 1.01; //vx *= friction; vy *= friction; xpos += vx; ypos += vy; float r = isize/2; switch (mode) { case 3: if (xpos < 0 - r) { xpos = width + r; } else if (xpos > width + r) { xpos = 0 - r; } if (ypos < 0 - r) { ypos = height + r; } else if (ypos > height + r) { ypos = 0 - r; } break; case 4: if (ypos > height - r) { ypos = height - r; float rand = random(-1,-4); vy *= rand; //life -= int(map(rand,-0.6,-1,0,0)); } break; case 5: if (xpos < 0 + r) { xpos = 0 + r; vx *= -1; } else if (xpos > width - r) { xpos = width - r; vx *= -1; } if (ypos < 0 - r) { ypos = height + r; } else if (ypos > height + r) { ypos = 0 - r; } break; } } boolean dead() { if (xpos < 0 - isize) { return true; } else if (xpos > width + isize) { return true; } else if (life <= 0) { return true; } else { return false; } } } class Crawler extends Bug { Crawler (float ixp, float iyp, color ic) { xpos = ixp; ypos = iyp; vx = random(-10,10); vy = random(-15,0); } void display() { if (drawVolume) { noStroke(); fill(c); ellipse(xpos,ypos,isize,isize); } else { stroke(255); point(xpos,ypos); } } }