//// Bob chases Gold, Bird drops bomb. //// B.A Martin:20219 String title="Bob chases Gold, Bird drops bomb."; String author="B.A.Martin:20219"; String news=""; float horizon; int score=0; // Positions & speeds of objects. float sunX, sunY; float houseX=100, houseY=100; float goldX, goldY; float bobX, bobY, bobXspeed, bobYspeed; float muttX, muttY, muttXspeed, muttYspeed, muttFast=1; float muttW=80, muttH=40; // Bird & bomb float birdX=200,birdY=50, birdXspeed=3,birdYspeed=0.05; float bombY=0, bombYspeed=0, gravity=0.1; color BLUE=color(0,0,255); color BROWN=color(127,0,0); //// SETUP: Define screen size, set modes. void setup() { size( 800, 600 ); horizon= height/4; reset(); muttX= 0; // Start Mutt at left. muttY= height/2; } void reset() { sunX= width/2; // Start the sun half-way across the screen. sunY= 50; bobX= houseX; // Start Bob at house. bobY= houseY; bobXspeed= random( 2,5 ); // Random speed. bobYspeed= random( -3, +3 ); goldX= random(50,width-50); goldY= random(horizon+20,height-40); bombY= birdY+20; muttFast= random( 0.5, 3.0 ); } //// DRAW: sky & sun plus creature void draw() { scene(); action(); show(); messages(); // (Display the messages last.) } //// SCENE: sky, sun, house. void scene() { background( 150, 200, 250 ); // Blue sky fill( 255, 255, 0 ); ellipse( sunX, sunY, 30, 30 ); // Yellow sun fill( 255, 0, 0 ); rect( houseX,houseY, 100,50 ); // Red house triangle( 100,100, 200,100, 150,50 ); // Grass fill( 50,150,50 ); rect( 0,horizon, width,height-horizon ); } //// MESSAGES. void messages() { fill(0); textSize(20); text( title, width/3, 20 ); text( "SCORE: "+score, width*3/4, 40 ); textSize(12); text( news, 10, 20 ); String help1= "Click to reset Bob.\n"; String help2= "Press 's' key to lower the sun, 'q' to quit."; text (help1+help2, 10, height/2 ); text( author, 10, height-10 ); // Also display the author & file name. text( author, 10, height-10 ); } //// ACTION: sun moves (then resets to random height) void action() { if (sunX > width) { sunX= 0; sunY= random( 20, 120 ); } sunX= sunX + 1; // Bob chases Gold. if (bobX>width-20 || bobX<0) { bobXspeed *= -1; } if (bobYheight) { bobYspeed *= -1; } bobXspeed= (goldX-bobX) / 60; bobYspeed= (goldY-bobY) / 60; bobX= bobX + bobXspeed; bobY= bobY + bobYspeed; // Got the gold! if ( dist(bobX,bobY, goldX,goldY) < 20) { score += 100; news= "Bob got the gold!"; reset() ; } // Mutt chases Bob muttXspeed= muttFast * (bobX-muttX) / 30; muttYspeed= muttFast *(bobY-muttY) / 30; muttX= muttX + muttXspeed; muttY= muttY + muttYspeed; if ( dist(bobX,bobY, muttX,muttY) < 10) { news= "Mutt trips Bob"; score -= 50; reset() ; } // Bird if (birdX>width-20 || birdX<0) { birdX=0; birdY=random(30,horizon-30); } if (birdY>horizon || birdY<0) { birdYspeed *= -1; } birdX= birdX + birdXspeed; birdY= birdY + birdYspeed; if (bombY>0) { bombY += bombYspeed; bombYspeed += gravity; } if (bombY>height) { bombY=0; bombYspeed=0; } } //// SHOW: Bob & Mutt void show() { goldShow(); bobShow(); muttShow(); birdShow(); } void goldShow() { fill( random(150,200), random(150,200), random(0,100) ); ellipse( goldX, goldY, random(40,50), random(40,50) ); } void bobShow() { // Draw Bob. fill( BLUE ); rect( bobX,bobY, 50, 80 ); // Blue creature ellipse( bobX+25, bobY-20, 40, 40 ); // Head on top // Eyes. fill( 255 ); ellipse( bobX+15, bobY-25, 12, 12 ); ellipse( bobX+35, bobY-25, 12, 12 ); fill( 0, 150, 0 ); ellipse( bobX+15, bobY-25, 4, 4 ); ellipse( bobX+35, bobY-25, 4, 4 ); // } void muttShow() { // Draw Mutt. fill( BROWN ); rect( muttX, muttY, muttW, muttH ); float headX=muttX+muttW*3/4; rect( headX, muttY, muttW/2, -muttH/2 ); // Eye. fill( 255 ); ellipse( headX+15, muttY-muttH/2+5, 12, 12 ); } void birdShow() { fill(200,0,200); triangle( birdX,birdY, birdX-60,birdY-12, birdX-60,birdY+12 ); fill(150,0,150); float wingtip=-40; if ( birdX%100 > 30 ) { wingtip= +40; } triangle( birdX-30,birdY, birdX-50,birdY, birdX-50,birdY-wingtip ); // Bomb if (bombY>0) { fill(0); ellipse( birdX, bombY, 20,30); triangle( birdX-10,bombY-12, birdX-20,bombY-12, birdX-10,bombY+5 ); triangle( birdX+10,bombY-12, birdX+20,bombY-12, birdX+10,bombY+5 ); } } //// EVENT HANDLERS //// void mousePressed() { reset(); // Set the position (x,y) goldX= mouseX; goldY= mouseY; } void keyPressed() { if (key == 'q') { exit(); } if (key == 'r') { reset(); } if (key == 's') { sunY= sunY + 50; } if (key == 'b') { bombY=birdY; } }