Modify a text file: $/39/p1b.java
$/39/p1b.java
//// Practice project #2a //// p2a.java (bam:3920) String title= "Practice project #2"; String author="Bruce Alan Martin (2013 Sep 20)"; int score=0; float horizon; float heroX=20, heroY=300, heroDX=0, heroDY=0; float monsterX=500, monsterY=200, monsterDX=4, monsterDY=3; float birdsX=10, birdsY=100, birdsDX=6, birdsDY=0; float targetX=-20, targetY=-20; // (off-screen) void setup() { //// Init. size( 800, 600 ); horizon= height/3; } void draw() { //// Next frame if (key == '?') { help(); } else { scene(); action(); scoring(); } } void scene() { //// sky, grass, etc. background( 200,200,255 ); fill( 100,200,100 ); // grass rectMode( CORNERS ); rect( 0,horizon, width,height ); //// Titles, etc. textSize( 20 ); fill(0); text( title, width/3, 40 ); text( author, 20, height-40 ); textSize( 12 ); text( "Press ? key for help", width/3, 60 ); } void scoring() { //// Scoreboard if (score ==0) { return; } textSize( 20 ); if (score < 0) { fill( 255,0,0 ); } else { fill(0,150,0); } text( "Score is "+score, width-200, 50 ); } void help() { int x=width/2, y=height/2; textSize(12); text( "Click to start hero moving.", x,y ); y +=15; text( "Lose 10 points, when monster hits hero.", x,y ); y +=15; text( "Gain 5 points, when birds finish a flight.", x,y ); } void action() { //// Move & draw raw creatures. hero(); monsters(); birds(); //// Check collisions if ( dist( heroX,heroY, monsterX,monsterY ) < 100 ) { background( 0 ); score -= 10; monsterX= width-20; // Move monster to left side, random height. monsterX= random( horizon, height); } } void hero() { //// Move & draw hero if (heroX<20 || heroX> width-20) heroDX *= -1; // Bounce if (heroY
height-20) heroDY *= -1; heroX += heroDX; heroY += heroDY; rectMode( CENTER ); fill( 0, 255, 255 ); rect( heroX, heroY, 50, 80); fill( 255, 255, 255 ); ellipse( heroX, heroY-60, 40,40 ); //// Draw eyes. fill( 255,255,255 ); ellipse( heroX-12, heroY-60, 15, 15 ); // Right Eye ellipse( heroX+12, heroY-60, 15, 15 ); // Left Eye fill( 0,0,255 ); ellipse( heroX-12+heroDX, heroY-60+heroDY, 5, 5 ); // Right Eyeball ellipse( heroX+12+heroDX, heroY-60+heroDY, 5, 5 ); // Left Eyeball } void monsters() { //// Move & draw monster if (monsterX<20 || monsterX> width-20) monsterDX *= -1; if (monsterY
height-20) monsterDY *= -1; monsterX += monsterDX; monsterY += monsterDY; fill( 255, 0, 0 ); ellipse( monsterX, monsterY, 50, 80); } void birds() { //// 3 birds fly. +++++ birdsX += birdsDX; if (birdsX > width) { //// Start a new flock of birds (at random height); birdsX= 10; birdsY= random( 0, horizon ); score += 5; // Gain 5 points, when birds finish a flight. } //// Draw 3 birds; drawBird( birdsX, birdsY ); drawBird( birdsX-50, birdsY-50 ); drawBird( birdsX-50, birdsY+50 ); } void drawBird( float x, float y ) { //// Draw one bird at (x,y) fill( 0,0,255 ); triangle( x,y, x-40,y-10, x-40,y+10 ); if ( (frameCount/15) % 2 > 0) triangle( x-10,y, x-35,y, x-35,y+20 ); else triangle( x-10,y, x-35,y-20, x-35,y ); } void keyPressed() { //// Event handler for keyboard if (key == 'q') { exit(); } else if (key == '+') { heroDX *= 1.1 ; // 10% speedup heroDY *= 1.1 ; } else if (key == '-') { heroDX *= 0.9 ; heroDY *= 0.9 ; } } void mousePressed() { //// Move hero toward mouse-click. if (mouseX>heroX) heroDX= +2; else heroDX= -2; heroDY= (mouseY>heroY) ? heroDY= +1.5 : -1.5 ; background( 255 ); }