///// Dynamic sketch (modularized) - dog points, bird flies //// B.A.Martin: '9918 String title= "Dynamic sketch (modularized) with dog."; String hints= "Click resets Mario to mouse position\n" + "r resets randomly, d to reset dog, q to quit\n" + "+/- to speedup/slowdown Mario; f to freeze Mario"; String author= "B.A.Martin: '9918"; float horizon; float marioX, marioY, marioDX, marioDY; float mushX, mushY, mushDX, mushDY; float birdX, birdY, birdDX, birdDY; float bombY=-1,bombDY=-1, gravity=0.1; float sunX, sunY; float rate; boolean day=true, bug=false; //// SETUP: size & modes void setup() { size( 600, 400 ); smooth(); horizon= height/3; reset(); dogReset(); } void reset() { sunX=10; sunY=50; marioX= random(50, width/2); marioY= random(horizon, height-20); marioDX= random(1, 3); marioDY= random(1, 2); birdX= -1; // No bird bombY= -1; bombDY=-1; // No bomb } void dogReset() { rate= frameRate; mushX= random(width/2, width-50); mushY= random(horizon, height-20); } //// NEXT FRAME: scene, action, show void draw() { scene(); action(); credits(); } //// Scene: sky & grass void scene() { // Sky & sun/moon if (day) { background( 150, 200, 250 ); // day sky fill( 255, 255, 100 ); } else { background( 100, 150, 200 ); // nite sky fill( 250, 200, 150 ); } ellipse( sunX, sunY, 30, 30 ); // grass fill( 20, 200, 100 ); rect( 0, horizon, width, height-horizon ); // grass fill( 255, 255, 0 ); // house fill( 255, 0, 0 ); rect( 100,horizon-60, 100,60 ); triangle( 90,horizon-60, 150,horizon-90, 210,horizon-60 ); fill(0); rect( 120,horizon-40, 20,40 ); // door fill(255); rect( 125,horizon-35, 10,15 ); // windows rect( 160,horizon-40, 20,30 ); } void credits() { fill(0); textSize(20); text( title, width/4, 24); textSize(12); fill(0,0,255); text( hints, width/2, 60 ); fill(150,0,0); text( author, 20, height-20); // Debugging info if (bug) { fill(100); text( "mario:\n" +marioDX+ "\n" +marioDY, width-200, height-50 ); text( "dog:\n" +mushDX+ "\n" +mushDY, width-100, height-50 ); } } //// Action: Dog chases mario, who follows mouse. void action() { // Sun arcs across sky sunX= sunX + 1; sunY= horizon - horizon* sin( PI * (sunX/width) ); if (sunX>width) { day = ! day; sunX= 0; } // Move mario if (marioX<10 || marioX>width-10) marioDX = -marioDX; marioX= marioX + marioDX; if (marioYheight-50) marioDY = -marioDY; marioY= marioY + marioDY; showHero(); // Dog chases mario. mushDX= (marioX-mushX) / rate / 2; mushDY= (marioY-mushY) / rate / 3; mushX= mushX + mushDX; mushY= mushY + mushDY; showDog(); // Bird flies, if birdDX>0 if (birdDX>0) { birdX += birdDX; showBird(); } } //// Show: draw mario & dog. void showHero() { // mario fill( 255, 150, 200 ); // head ellipse( marioX, marioY, 30, 40 ); fill( 255 ); // eyes ellipse( marioX-6, marioY-6, 10, 10 ); ellipse( marioX+6, marioY-6, 10, 10 ); fill( 0,0,255 ); ellipse( marioX-6, marioY-6, 4,4 ); ellipse( marioX+6, marioY-6, 4,4 ); fill( 0, 255, 255 ); // body rect( marioX-25, marioY+18, 50, 80 ); fill(255,0,0); text( "Mario", marioX-15, marioY+40 ); } // Brown dog always faces Hero void showDog() { fill( 126, 0, 0 ); if (mushDX>0) { rect( mushX,mushY, 30,20 ); // head rect( mushX-40,mushY+20, 60,30 ); fill(255); ellipse( mushX+10,mushY+6, 6,6 ); } else { // Dog faces left. rect( mushX,mushY, 30,20 ); // head rect( mushX+50-40,mushY+20, 60,30 ); fill(255); ellipse( mushX+20,mushY+6, 6,6 ); } } void showBird() { fill( 0, 0, 255 ); triangle( birdX,birdY, birdX-50,birdY-10, birdX-50,birdY+10 ); float up= 30; if ( birdX % 100 < 25 ) { up= -30; } // Flap wing upward triangle( birdX-20,birdY, birdX-30,birdY+up, birdX-40,birdY ); // Bomb? if (bombY > 0) { bombY += bombDY; bombDY += gravity; fill(127,100,100); ellipse( birdX-40,bombY, 10,8 ); } } void bombDrop() { bombY= birdY+5; bombDY= 0; } //////// EVENT HANDLERS //////// void mousePressed() { if (mouseX > horizon-20) { marioX= mouseX; marioY= mouseY; } else { birdDX= random(0.1,3); birdDY= 0; birdX= 10; birdY= mouseY; bombDrop(); } } void keyPressed() { if (key == 'q') { exit(); } if (key == 'r') { reset(); } if (key == 'd') { dogReset(); } if (key == '!') { bug = ! bug; } if (key == 'n') { day = ! day; } if (key == 'f') { // Freeze mario; marioDX=0; marioDY=0; } if (key == '+') { marioDX= marioDX * 2; // Faster marioDY= marioDY * 2; } if (key == '-') { marioDX= marioDX * 0.75; // Slower marioDY= marioDY * 0.75; } // Bird drops bomb! if (key == 'b') { bombDrop(); } }