Modify a text file: $/39/p31b.java
$/39/p31b.java
String title="Project 3-1b", author="BAM"; 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; // Properties of the octopus. float x=400, y=300, w=50, h=80; float dx=2, dy=1; int r=200,g=50,b=200; void setup() { //// Init. size(800,600); buttonX=width-200; buttonY=height-100; } void draw() { //// Next frame scene(); if (key == '?') help(); else action(); } void scene() { //// sky, sea, grass, button, etc. background( 200,220,255 ); fill(0); textSize(20); text( title, 300, 20 ); textSize(12); text( "? for help", 300, 40 ); // Sea noStroke(); fill( seaColor ); // Greenish sea. rectMode(CORNERS); rect( 0, sealevel, width, height ); rectMode(CORNER); grass(); waves(); // Button fill( 255,200,200 ); stroke(0); rectMode( CENTER ); rect( buttonX, buttonY, 150, 50 ); fill(0); text( "CLICK HERE", buttonX-50, buttonY-10 ); text( "to move octopus", buttonX-50, buttonY+10 ); } void help() { //// Instructions int n=2; fill(100,0,0); text( "Click octopus to change color", 500, 12*n++ ); text( "Click button to move octopus", 500, 12*n++ ); text( "~ to change waves", 500, 12*n++ ); text( "@ for debugging", 500, 12*n++ ); text( "----", 500, 12*n++ ); text( "@ for debugging", 500, 12*n++ ); } void grass() { //// float grassX=10, grassX2=10, grassSkip=20; while ( grassX < width) { strokeWeight( 4) ; stroke( grassColor ); line( grassX+5*dx,height-50, grassX2, height-3 ); grassX= grassX + grassSkip; grassX2= grassX2 + grassSkip; } } void waves() { //// Waves at sealevel float p=10, dp=(waveSpacing+waveSize)/2; while (p
width-50) ? -dx : dx; dy= (y
height-100) ? -dy : dy; x += dx; y += dy; } //// Draw the octopus noStroke(); fill( r,g,b ); rectMode(CENTER); rect( x, y, w, h ); ellipse( x, y-h/2, w, w); fill(255,255,0); ellipse( x+dx*5, y-13-h/2, 12,12 ); fill(255,0,0); ellipse( x+dx*5, y-13-h/2, 3,3 ); //// DEBUG int n=0; if (debug) { bugX= x+50; bugY= y; fill(0); bug( x +","+ y ); bug( dx +","+ dy ); bug( "r"+r+" g"+g +" b"+b ); } } void bug( String s ) { //// Debug msg text( s, bugX, bugY ); bugY += 20; } void mousePressed() { //// Click event if ( hit(x,y,w,h) ) { background(0); r= int( random( 100, 250) ); g= int( random( 0,100 )); b= int( random( 100, 250)); } if (hit(buttonX,buttonY, 150,40)) { //-- background(0,255,0); x= random(50,width-50); y= random(sealevel,height-50); } } boolean hit( float x, float y, float w, float h ) { //// True if mouse hit this rect return mouseX > x-w/2 && mouseX < x+w/2 && mouseY > y-h/2 && mouseY < y+h/2 ; } void keyPressed() { //// Key handler if (key=='q') exit(); if (key=='@') debug = ! debug; if (key=='~') { waveSize = random(15,50); waveSpacing = random(15,50); } }