//// c0.pde is a copy of d9_modular.pde (c1.pde is object-based) String title= "c0.pde: Dynamic sketch -- modularized."; String author="B.A.Martin", hint="Click to reset creature.\n 'q' to quit."; color RED=color(255,0,0), YELLOW=color(255,255,0), BLUE=color(0,0,255), SKY=color(150,200,250); // GLOBAL DECLARATIONS // float sunX, sunY; // Position of sun float fredx, fredy; // Position and speed of creature. float freddx, freddy; //// SETUP: Define screen size, set modes. void setup() { size( 600, 400 ); sunX= width/2; // Start the sun half-way across the screen. sunY= 50; reset(); } void reset() { fredx= width/2; // Start in center. fredy= height/2; freddx= random( 1,3 ); // Random speed. freddy= random( -1, +1 ); } //// DRAW: sky & sun plus creature void draw() { scene(); action(); show(); messages(); // (Display the messages last.) } //// SCENE: sky, sun, house. void scene() { background( SKY ); // Blue sky fill( YELLOW ); ellipse( sunX, sunY, 30, 30 ); // Yellow sun fill( RED ); rect( 100,100, 120,50 ); // Red house at (100,100) triangle( 100,100, 220,100, 160,50 ); } //// ACTION: creature moves, sun moves (then resets to random height) void action() { if (sunX > width) { sunX= 0; sunY= random( 20, 120 ); background(YELLOW); } sunX= sunX + 1; // Move the creature. fredx= fredx + freddx; fredy= fredy + freddy; if (fredx>width) reset(); } //// SHOW: creature void show() { // Draw creature. fill( 0, 0, 200 ); rect( fredx,fredy, 50, 80 ); // Blue creature ellipse( fredx+25, fredy-20, 40, 40 ); // Head on top // Eyes. fill( 255 ); ellipse( fredx+15, fredy-25, 12, 12 ); ellipse( fredx+35, fredy-25, 12, 12 ); fill( 0, 150, 0 ); ellipse( fredx+15, fredy-25, 4, 4 ); ellipse( fredx+35, fredy-25, 4, 4 ); } //// MESSAGES. void messages() { fill(0); text( title, width/3, 10 ); text( hint, 10, height/2 ); text( "B.A.Martin", 10, height-10 ); } //// EVENT HANDLERS //// void mousePressed() { reset(); // Set the position (x,y) fredx= mouseX; fredy= mouseY; } void keyPressed() { if (key == 'q') { exit(); } if (key == 'r') { reset(); } if (key == 's') { sunY += 50; } }