Modify a text file: $/guydog4.java
$/guydog4.java
//////// Guy and dog, plus birds. //////// // bam:2907a // //// GLOBAL DATA //// int birdX=100, birdY=100; // Initial position of bird int birdDX=10; int sunX=100, sunY=100; // Position of sun float bombX=0, bombY=0, bombDY=0; // No bomb until Y > 0. float gravity= 0.5 * 9.81 / frameRate; boolean eastbound=true; int speed; void setup() { size( 800, 600 ); smooth(); //-- frameRate(5); speed= (int) frameRate; } void draw() { //// Move and show everything // scene(); guy(); dog(); bird(); } void scene() { //// Sky, sun, grass. background( 150, 200, 255 ); // Sky //// Move the sun. sunX = (sunX+1) % width; float xw= (float) sunX / width; sunY = (int) ( 150 - 100*sin( PI*xw ) ); float arc= (float) sin( PI * sunX / width ); sunY = (int) ( 150 - 100*arc ); //-- text( "@@@ sun: "+sunX+","+sunY, sunX, sunY+50 ); //-- text( "@@@ xw: "+xw, sunX, sunY+70 ); float sinyy= sin( PI*( xw ) ); //-- text( "@@@ sin(PI*x/w): "+sinyy, sunX, sunY+90 ); //// Draw the sun. fill( 255, 255, 0 ); // Sun ellipse( sunX, sunY, 40, 40 ); //// Grass fill( 100, 255, 100 ); rectMode( CORNERS); rect( 0, height/3, width, height ); fill( 0 ); text( "Guy and dog, plus birds. -- bam:2907c", 50, height-20 ); text( "Click for bomb.", width-100, 50 ); } void guy() { //// Guy follows mouse. fill( 0,0,255 ); // Blue shirt rectMode( CENTER ); rect( mouseX, mouseY, 60, 80 ); fill( 255, 200, 200 ); // Pink face ellipseMode( CENTER ); ellipse( mouseX, mouseY-40-15, 30,30 ); // +++ add a hat? //// Eye. fill(0,0,255); if (eastbound) { ellipse( mouseX+10, mouseY-40-20, 5,5 ); } else { ellipse( mouseX-10, mouseY-40-20, 5,5 ); } } void dog() { if (pmouseX < mouseX) eastbound=true; else if (pmouseX > mouseX) eastbound=false; else {} // No change if = fill( 150, 50, 50 ); rectMode( CORNER ); //// Dog chases guy. //dog is on the right if ( ! eastbound){ int dogX=pmouseX-50; int dogY=pmouseY+100; dogX = dogX + 5*( pmouseX-mouseX ); dogY = dogY + 2*( pmouseY-mouseY ); rect( dogX+80, dogY, 40, 20 ); rect( dogX+70, dogY-10, 20, 10 ); } //dog is on the left else { int dogX=pmouseX-70; int dogY=pmouseY+100; dogX = dogX + 5*( pmouseX-mouseX ); dogY = dogY + 2*( pmouseY-mouseY ); rect( dogX, dogY, 40, 20 ); rect( dogX+30, dogY-10, 20, 10 ); } } void bird() { //// Bird flies across screen. (Press key to drop load.) birdX = birdX + birdDX; birdX = birdX % width; birdY = birdY + 10 - (int) random( 20 ); fill( 200, 100, 200, 150 ); triangle( birdX,birdY, birdX-50,birdY-15, birdX-50,birdY+15 ); if (birdY >150){ birdY = birdY - 5; } else if (birdY <-200){ birdY= birdY - 10; } //// Check if bomb is falling from bird. if (bombX > 0) { bombX= birdX - 5; bombDY= bombDY + gravity; bombY += bombDY; if (bombY > height) { //// Bomb vanishes at bottom bombY=0; background(255); } fill( 150, 0, 100 ); ellipseMode( CENTER ); triangle( bombX-12,bombY-15, bombX+12,bombY-15, bombX,bombY+15 ); // Fins fill( 70, 0, 70 ); ellipse( bombX, bombY, 16, 30 ); } } void bomb() { //// Bomb falls with gravity. No bomb if zero. } void mousePressed() { //// Drop bomb, when mouse is pressed. bombX = birdX; bombY = birdY; bombDY= 0; background(0); } void keyPressed() { if (key == 's') { speed -= 5; if (speed<1) speed= 30; frameRate( speed ); } }