Modify a text file: $/39/p31c.java
$/39/p31c.java
///// Project 3-1c String title="Project 3-1c \n (methods with args)", 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(640,480); 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 ); // 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 ); //// Text on screen //// fill(0); textSize(20); text( title, 200, 20 ); textSize(12); text( "? for help", 500, 20 ); } void help() { //// Instructions int n=3; 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, grassSkip=20; while ( grassX < width) { blade( grassX, +5*dx ); // Draw one blade of grass (slanting). // grassX= grassX + grassSkip; } } void blade( float x, float slant ) { //// Draw one blade strokeWeight( 4) ; stroke( grassColor ); line( x+slant,height-50, x, height-3 ); } 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 octopusDraw( x, y ); //// DEBUG if (debug) { bugX= x+50; bugY= y; fill(0); bug( x +","+ y ); bug( dx +","+ dy ); bug( "r"+r+" g"+g +" b"+b ); } } void octopusDraw( float x, float y ) { 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 ); //// Legs float legX=x-w/2, legY=y+h/2; for (int j=0; j<8; j++) { leg( legX, legY ); legX += w/8; } }// octopusDraw() // void leg( float x, float y ) { //// Draw a leg stroke(255,0,255); strokeWeight(3); line( x, y, x-2*dx+random(2), y+20 ); } 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); } ///// Project 3-1c String title="Project 3-1c \n (methods with args)", 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=70, h=90; float dx=2, dy=1; int r=200,g=50,b=200; void setup() { //// Init. size(640,480); 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 ); // 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 ); //// Text on screen //// fill(0); textSize(20); text( title, 200, 20 ); textSize(12); text( "? for help", 500, 20 ); } void help() { //// Instructions int n=3; 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, grassSkip=20; while ( grassX < width) { blade( grassX, +5*dx ); // Draw one blade of grass (slanting). // grassX= grassX + grassSkip; } } void blade( float x, float slant ) { //// Draw one blade strokeWeight( 4) ; stroke( grassColor ); line( x+slant,height-50, x, height-3 ); } 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 octopusDraw( x, y ); //// DEBUG if (debug) { bugX= x+50; bugY= y; fill(0); bug( x +","+ y ); bug( dx +","+ dy ); bug( "r"+r+" g"+g +" b"+b ); } } void octopusDraw( float x, float y ) { 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 ); //// Legs float legX=x-w/2, legY=y+h/2; for (int j=0; j<8; j++) { leg( legX, legY ); legX += w/8; } }// octopusDraw() // void leg( float x, float y ) { //// Draw a leg stroke(255,0,255); strokeWeight(3); line( x, y, x+dx*random(3), y+20 ); } 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); } } }