Modify a text file: $/flapping.java
$/flapping.java
//// Flapping bird //// String title="CST 112 Bird & Jewels"; String author="B.A.Martin (2012 Oct 26)"; String hint= "Press ? for help."; float horizon; int score=0; int grade; float birdX, birdY, birdDX=2; float rubyX, rubyY, rubyR=40; float emeraldX, emeraldY, emeraldR=30; float sapphireX, sapphireY, sapphireR=25; color rubyC=color(255,0,0), emeraldC=color(0,20,0), sapphireC=color(0,0,255); void setup() { //// Setup: screen size, initialization. size(800,600); horizon= height/4; reset(); // Reset initial values. } void draw() { //// Draw next frame. scene(); if (key == '?') help(); if (key == '!') grade(); //// if (score < 0) gameover(); else if ( score >= 1000 ) youwin(); else if ( key == '.' ) { text("PAUSE",10,10); } else bird(); treasure(); } void scene() { //// Draw the scene: sky, grass, background( 200, 220, 250 ); fill( 200,250,200 ); rect( 0, horizon, width, height-horizon ); //// fill( 255, 0, 255 ); text( title, width/2 - 6*title.length()/2, 20 ); text( hint, 10, 40 ); fill( 100, 100, 0 ); text( "SCORE: "+score, width - 100, 40 ); fill( 0, 0, 150 ); text( author, 10, height-20 ); } void reset() { //// Random positions for 3 jewels. rubyX= random(50,width-50); rubyY= random(horizon,height-50); emeraldX= random(50,width-50); emeraldY= random(horizon,height-50); sapphireX= random(50,width-50); sapphireY= random(horizon,height-50); //// Start bird at left, random height. birdX= 10; birdY= random( 20, horizon-20 ); } void bird() { //// Bird flies West (right). Reset when it reaches width. birdX += birdDX; if (birdX > width ) { score -= 100; reset(); return; } //// Draw bird fill( 100, 0, 100 ); triangle( birdX-100,birdY-10, birdX-100,birdY+10, birdX+30,birdY ); triangle( birdX-10,birdY-10, birdX-10,birdY, birdX+30,birdY ); fill( 255,255,0 ); ellipse( birdX,birdY-5, 4,4 ); // Eye // Wings flap. fill( 150, 0, 150 ); int xx= 20 * (int) birdX / width; if ( xx % 2 == 0 ) { //// Flap wings, 30 times per flight. triangle( birdX-80,birdY-30, birdX-80,birdY, birdX,birdY ); } else { triangle( birdX-80,birdY, birdX-80,birdY+30, birdX,birdY ); } } void treasure() { //// Show treasures jewel( rubyX, rubyY, rubyR, color(255,0,0) ); jewel( emeraldX, emeraldY, emeraldR, color(0,255,0) ); jewel( sapphireX, sapphireY, sapphireR, color(0,0,255) ); } void jewel( float x, float y, float r, color c ) { //// Draw the jewel. if (x < 10 ) return; fill( c ); ellipse( x, y, r, r ); // Sparkles. noStroke(); fill( 255 ); for (int j=0; j<5; j++) { float xx= x - r/2 + random( r ); float yy= y - r/2 + random( r ); ellipse( xx, yy, 2, 2 ); } } //////// EVENT HANDLERS //////// void keyPressed() { //// Actions when a key is pressed. if (key == 'r') reset(); if (key == 'q') exit(); if (key == 'p') { score=0; reset(); } if (key == '+') score += 250; if (key == '-') birdX += width; } void mousePressed() { //// Click on jewel to get points (and make it vanish). //// Click on bird send it back to start (& reset jewels) if ( dist(mouseX, mouseY, birdX,birdY) < 10 ) { birdX=0; reset(); } if ( dist(mouseX, mouseY, rubyX,rubyY) < rubyR ) { rubyX=0; score += 30; } if ( dist(mouseX, mouseY, emeraldX,emeraldY) < emeraldR ) { emeraldX=0; score += 20; } if ( dist(mouseX, mouseY, sapphireX,sapphireY) < sapphireR ) { sapphireX=0; score += 10; } } void help() { //// Display instructions. fill(255,0,0); int x=width-200, y=height/2 - 100; int k=0; text( "Click on jewels to score points", x, y + 12*k++ ); text( "Click on bird to send it back.", x, y + 12*k++ ); text( "Lose 100 points if bird reaches end.", x, y + 12*k++ ); text( "", x, y + 12*k++ ); text( "Press r key to reset.", x, y + 12*k++ ); text( "Press p key to play again.", x, y + 12*k++ ); } void gameover() { fill(255,100,100); rect( width/2 -100, height/2 , 200, 60 ); // fill(0,0,255 ); text( "G A M E O V E R ", width/2 - 50, 30+height/2 ); text( "Press 'p' key to play again", width/2 - 70, 100 + height/2 ); clearall(); } void youwin() { fill(255,255,0); rect( width/2 -100, height/2, 150, 50 ); // fill(0, 150, 0 ); text( " Y O U ", width/2 - 50, 20 + height/2 ); text( " W I N !", width/2 - 50, 40 + height/2 ); text( "Press P key to play again", width/2 - 100, 70 + height/2 ); clearall(); } void clearall() { rubyX=emeraldX=sapphireX=0; birdX=0; } ////// ADD THIS CODE AT THE BOTTOM OF YOUR FILE ////// void grade( ) { //// Display the grade. float x= width-200, y= height/2; fill( 255, 0, 0 ); text( "EXAM GRADE: "+grade, x, y ); }