Modify a text file: $/39/p4c.java
$/39/p4c.java
//// Creatures, using Objects. ///// Project 4c String title="Project 4c \n (object-based design)", author="BAM"; String hint= "Octopus follows mouse \n (@ key for debugging)"; boolean debug=false; float bugX,bugY; color skyColor=color(200,220,255); color seaColor=color(50,200,100); color grassColor=color(0,150,50); float sealevel=100, waveSize=20, waveSpacing=20; float buttonX, buttonY; // Creatures // Creature octo, squid, lobster; Cloud c1, c2, c3; void setup() { //// Init. size(640,480); buttonX=width-200; buttonY=height-100; // Create the creatures // octo= new Creature(); squid= new Creature(60, 40, 0, 10); squid.r=255; lobster= new Creature(30, 50, 2, 0 ); lobster.g=255; // Create the clouds // c1= new Cloud(); c2= new Cloud(); c3= new Cloud(); } void draw() { //// Next frame scene(); action(); } void scene() { //// sky +_ ocean background( skyColor ); noStroke(); fill( seaColor ); // Sea rectMode( CORNER ); rect( 0,100, width, height -100 ); float waveX; if (frameCount % 10 == 0 ) { waveSize= 25 + random(10); waveSpacing= 45+random(10); } for( waveX=0; waveX
width) c1= new Cloud(); if (c2.x>width) c2= new Cloud(); if (c3.x>width) c3= new Cloud(); //// Messages //// fill(0); textSize(20); text( title, width/2 - 100, 30 ); textSize(12); text( hint, 20, sealevel - 30 ); fill(255,0,255); text( author, 20, height - 30 ); } void action() { //// Move creatures. octo.dx= (mouseX -octo.x ) / 20; octo.dy= (mouseY - octo.y) / 20; octo.move(); squid.dx= (octo.x - squid.x) / 60; squid.dy= (octo.y - squid.y) / 60; squid.move(); lobster.dx= (squid.x - lobster.x) / 60; lobster.dy= (squid.y - lobster.y) / 60; lobster.move(); //// Draw creatures. octo.show(); squid.show(); lobster.show(); if (key=='@') text (octo.x, octo.x, octo.y ); } class Creature { //// Define creature with arms and legs float x=200, y=200, dx=3, dy=2; float w=80, h=120; int arms=0, legs=8; int r=255, g=0, b=0; //// CONSTRUCTORS //// Creature( ) { //// Default constructor x= random( width/4 ); y= random( 100, height/4 ); } Creature( float w, float h, int arms, int legs ) { //// 4-arg Constructor x= random( width/4 ); y= random( 100, height/4 ); dx= 3-random( 6 ); dy= 2-random( 4 ); this.w= w; this.h= h; this.arms= arms; this.legs= legs; r= int( random( 0, 150 ) ); g= int( random( 100, 250 ) ); b= int( random( 0, 150 ) ); } //// METHODS //// void move() { //// x += dx; y += dy; } void show() { //// fill( r,g,b ); noStroke(); rectMode( CENTER ); rect( x,y, w,h ); ellipse( x, y-h/2, w, w ); // Head (dome) fill(255); ellipse( x+3*dx, y+2*dy-3*h/4, 15,15 ); // Eye fill(0,0,255); ellipse( x+3*dx, y+2*dy-3*h/4, 5,5 ); //// Legs stroke(r,g,b); strokeWeight(3); float legX, legY; legX= x - w/2; legY= y + h/2; for ( int j=0; j