Modify a text file: $/39/p2a.java
$/39/p2a.java
//// Practice project #2a //// p2a.java (bam:3920) String title= "Practice project #2"; String author="Bruce Alan Martin (2013 Sep 20)"; int g= 200; // Grass color. 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 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 ) < 50 ) { 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 ); } 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 ); fill(random(250) ); 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 scene() { //// sky, grass, etc. background( 200,200,255 ); fill( 100,g,100 ); // grass rectMode( CORNERS ); rect( 0,horizon, width,height ); //// Titles, etc. textSize( 20 ); text( title, width/3, 40 ); text( author, 20, height-40 ); } void scoring() { //// Scoreboard text( "Score is "+score, width-100, 50 ); } 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) ? +1.5 : -1.5 ; heroDX= (mouseX-heroX) / 60; heroDY= (mouseY-heroY) / 60; // heroDY= (mouseY>heroY) ? +1.5 : -1.5 ; background( 255 ); }