Modify a text file: $/guydog5.java
$/guydog5.java
//////// Guy, dog, birds, bomb. //////// //////// Guy bounces off walls, shrinks when receding. // bam:2920f // //// GLOBAL DATA //// String title="Guy and dog, plus birds. -- bam:2917d"; float horizon; int guyX=100,guyY=300, guyDX=10, guyDY=5; int dogX=100,dogY=200; 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(); bomb(); } 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 horizon= height/3; fill( 100, 255, 100 ); rectMode( CORNERS); rect( 0, horizon, width, height ); fill( 0 ); text( title , 50, height-20 ); text( "Click for bomb.", width-100, 50 ); } void guy() { //// Guy moves dx,dy and bounces off walls. guyDX= guyX>50 && guyX
horizon && guyY
0; fill( 150, 50, 50 ); rectMode( CORNER ); float shrunk= 0.3 + (float) dogY / height; //// Dog chases guy. dogX += (guyX-dogX) / 20; dogY += (guyY+40-dogY) / 10; // Dog chases bottom of guy // Draw dog fill( 150, 50, 50 ); rectMode( CENTER ); rect( dogX, dogY, shrunk*50, shrunk*20 ); // Dog body if ( guyX>dogX ){ rect( dogX+30, dogY-10, 20, 10 ); // Dog head faces guy } else { 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; } } void bomb() { //// Check if bomb is falling from bird. //// Bomb falls with gravity. No bomb if zero. 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 ); //// Did bomb hit guy? if ( abs(guyX-bombX)<30 && abs(guyY-bombY)<30 ) { flashGuy(); } } } void flashGuy() { //// Make a flash around the guy. //// Paint red flash around guy. (Also decrease score by 50.) fill( 256,0,0, 200 ); ellipse( guyX, guyY, 200, 200 ); strokeWeight( 6 ); stroke( 256,0,0 ); line( guyX,guyY, guyX+100,guyY ); line( guyX,guyY, guyX+70,guyY+70 ); line( guyX,guyY, guyX,guyY+100 ); line( guyX,guyY, guyX-100,guyY ); line( guyX,guyY, guyX-70,guyY-70 ); line( guyX,guyY, guyX,guyY-100 ); strokeWeight( 1 ); } void mousePressed() { //// Drop bomb, when mouse is pressed. bombX = birdX; bombY = birdY; bombDY= 0; } void keyPressed() { if (key == 's') { speed -= 5; if (speed<1) speed= 30; frameRate( speed ); } }