Modify a text file: $/p1drifting.pde
$/p1drifting.pde
// BAM: Creature chases mouse, changes direction, color, size, direction, etc. // Fancy version -- birds, fling letters, etc. int xsize=600, ysize=400; // screen size // Creature coordiantes. // int x=125, y=125; // (x,y) coordinates for center of creeature. int dx=1, dy=1; float mw=1.0, mh=1.0; // maginifiers int fathead=0; String s= "monsters"; // Empty string; add keys to this. int birdX, birdY; // Coordiantes for the flock of birds. void setup() { // size(xsize, ysize); println(frameRate); frameRate(30); } void draw() { // background(230, 220, 255); rectMode(CENTER); ellipseMode(CENTER); // How far away is the mouse? int xx= x-mouseX; int yy= y-mouseY; float xFar, yFar, xAway, yAway; xFar= (float) (xx) / (float) xsize; yFar= (float) (yy) / (float) ysize; xAway= abs(xFar); yAway= abs(yFar); //// CREATURE DESCRIPTION //// fill(63, 255, 63); // Body: 60x80 rect green teeshirt. noStroke(); rect( x, y, 60*mw, 80*mh); // Head: 35x30 red ellipse. int xHead=x, yHead= y - 60; // (Move up by 1/2 of shirt and head. fill(255, 192, 63); ellipse( x, yHead, 30+fathead, 40+fathead); fill(255, 192, 63); // Ears ellipse( x-20, yHead-5, 15, 15); ellipse( x+20, yHead-5, 15, 15); fill(127, 63, 63); // Toupee ellipse( x, yHead-20, 24, 6); int redEye=0, greenEye=0, blueEye=0; if ( x < mouseX-20) redEye=255; if ( x > mouseX+20) greenEye=255; if ( y > mouseY+20) blueEye=255; fill(redEye, greenEye, blueEye); ellipse( x-8-10*xFar, y-65, 10, 10); // Eyes ellipse( x+8-10*xFar, y-65, 10, 10); stroke(255, 0, 0); // Mouth strokeWeight(2); fill(255, 255, 255); float bigMouth= abs(yAway*20); ellipse( x, y-48, 20.0, 4+bigMouth); noStroke(); // Nose fill(255, 127, 63); ellipse( x, y-60, 8, 12); fill(127, 255, 127); // Sleeves: 60x80 rect green teeshirt. rect( x-45, y-25, 30*mw, 20*mh); // Left sleeve rect( x+45, y-25, 30*mw, 20*mh); fill(255, 127, 63); rect( x-65, y-25, 10, 15); // Left hand rect( x-70, y-30, 10, 5); // Left finger rect( x+65, y-25, 10, 15); // Right hand rect( x+70, y-30, 10, 5); // Right finger fill(255, 0, 255); text( "HELP!!", x-20, y-10); // Red label for shirt. fill(0, 0, 0); // Bellybutton rect( x, y, 2, 2); stroke(0, 0, 255); // Legs strokeWeight(12); line( x-20, y+40, pmouseX-45, pmouseY+100); line( x+20, y+40, pmouseX+45, pmouseY+100); stroke(192,192,0); // Shoes strokeWeight(2); fill(128,64,0); rectMode(CENTER); rect( pmouseX-45, pmouseY+100, 25, 15); rect( pmouseX+45, pmouseY+100, 25, 15); //// END OF CREATURE DESCRIPTION. //// // Put a shield on left hand.) shield(x-80, y-60, 20); rectMode(CENTER); ellipseMode(CENTER); // Drifting creature. x += dx; // Drift diagonally; y += dy; x = x<0? xsize-1 : x; y = y<0? ysize-1 : y; // Force >=0 (wrap) x = x % xsize; y = y % ysize; // Constrain to size of sketch //// Keystrokes create monsters to follow the creature. //// flyingKeys( pmouseX, pmouseY); //// Flocks of birds fly by, every second. //// println(frameCount); if ((frameCount % (int) frameRate) == 0 ) { // Every second, pick a y value for the birds; birdX = (int) random( xsize ); birdY = (int) random( ysize ); text( "BIRDS: ", 10, 20); text(birdX, 50, 20); text(birdY, 70, 20); } // Fly the birds for 6 frames only. if ((frameCount % (int) frameRate) < 10) { flyingBirds( birdX/2, birdY/2); birdX += 30; birdY +=10; // Keep moving text( "BIRDS: ", 10, 20); text(birdX, 50, 20); text(birdY, 70, 20); } } void mouseClicked() { // Capture mouse x,y when clicked. x= mouseX; y= mouseY; // Change direction toward mouse. if (x < xsize/2) dx= -1; else dx=1; if (y < ysize/2) dy= -1; else dy=1; } void keyPressed() { s = s + key; // Append key to string. if (s.length() >30) s=""+key; fathead = (fathead+10) % 100; // Capture mouse x,y when clicked. x= mouseX; y= mouseY; dy = dx*dy; // Reverse direction. dx = -dx; // Bump magnification factors, when clicked. /* mw *= 1.1; mh *= 1.2; */ } void flyingBirds(int x, int y) { // Flying birds. strokeWeight(1); stroke( 255,0,0 ); fill( 0, 255, 255); triangle( x,y, x+20,y, x,y+10 ); x +=50; y +=5*dy; triangle( x,y, x+20,y, x,y+10 ); x +=50; y +=5*dy; triangle( x,y, x+20,y, x,y+10 ); x +=50; y +=5*dy; triangle( x,y, x+20,y, x,y+10 ); x +=50; y +=5*dy; triangle( x,y, x+20,y, x,y+10 ); x +=50; y +=5*dy; triangle( x,y, x+30,y, x,y+15 ); // Big Bird in the lead. } void flyingKeys(int x, int y) { // Flying key(s) text(s, x, y); int xkey, ykey; xkey= x+50 - dx*60; ykey= y - dy*80; text(key, xkey, ykey); xkey += 10*dx; ykey += 10*dy; text(key, xkey, ykey); xkey += 10*dx; ykey += 10*dy; text(key, xkey, ykey); xkey += 10*dx; ykey += 10*dy; text(key, xkey, ykey); xkey += 10*dx; ykey += 10*dy; text(key, xkey, ykey); fill(127); text( "Beware of the flying ", 10, 10); fill(0); text( s, 120, 15); } void shield( float xShield, float yShield, float side) { // Draw a stop sign, centered at (x,y), black borders, white center, "STOP" text. //// Draw a red, octagonal stop sign, with side=s. float a= (sqrt(2.0) * side) / 2; // Adjacent side of 45-degree triangle. float w= side + (sqrt(2.) * side); // Width of enclosing square. /* text("WIDTH:", 10,10); text(w, 70,100); text("SIDE:", 10,120); text(s, 70,120); text("ANGLE:", 10,140); text(a, 70,140); */ float x1= xShield - w/2, y1= yShield - w/2; float x2= xShield - side/2, y2= yShield - side/2; float x3= xShield + side/2, y3= yShield + side/2; float x4= xShield + w/2, y4= yShield + w/2; // float x2= x - w/2 + a; /* text("x1,y1", 10,200); text(x1, 50,200); text(y1, 100,200); text("x2,y2", 10,220); text(x2, 50,220); text(y2, 100,220); text("x3,y3", 10,240); text(x3, 50,240); text(y3, 100,240); text("x4,y4", 10,260); text(x4, 50,260); text(y4, 100,260); */ // Draw an octagon. //rectMode(CORNERS); noStroke(); fill(255,0,0); quad( x1,y2, x2,y1, x3,y1, x4,y2 ); fill(255, 255, 255); // White middle section! quad( x1,y2, x4,y2, x4,y3, x1,y3 ); fill(255,0,0); quad( x1,y3, x2,y4, x3,y4, x4,y3 ); fill(0,0,0); text( "S T O P", xShield-20, yShield+5 ); stroke(0); strokeWeight(2); line( x1,y2, x2,y1); line(x2,y1, x3,y1); line(x3,y1, x4,y2); line( x4,y2, x4,y3); line( x4,y3, x3,y4); line(x3,y4, x2,y4); line( x2,y4, x1,y3); line( x1,y3, x1,y2); }