// c0.pde //// c0.pde (copy of d9_modular) String title= "c0.pde: Dynamic sketch (d9_modular)."; String hint="Click to reset creature.\n 'q' to quit."; String author="B.A.Martin"; 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 & speed float freddx, freddy; //// SETUP: Define screen size, set modes. void setup() { size( 600, 400 ); sunX= width/2; // Start sun half-way 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 ); // House at (100,100) triangle( 100,100, 220,100, 160,50 ); } //// ACTION: 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 body 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; } } |
// c1.pde //// c1.pde: Object-Based revision of c0.pde String title= "c1.pde: Object-Based version of c0.pde sketch"; String hint="Click to reset creature.\n 'q' to quit."; String author="B.A.Martin"; color RED=color(255,0,0), YELLOW=color(255,255,0), BLUE=color(0,0,255), SKY=color(150,220,250); // GLOBAL DECLARATIONS (Object-Based) // Sun sol = new Sun(); Creature fred = new Creature(); //// SETUP: Define screen size, set modes. void setup() { size( 600, 400 ); sunReset(); fredReset(); } void sunReset() { sol.x = width/2; // Start the sun half-way across the screen. sol.y = 50; } void fredReset() { fred.x = width/2; // Start in center. fred.y = height/2; fred.dx = random( 1, 3 ); // Random speed. fred.dy = 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 sol.show(); // Yellow sun house( 100, 100 ); // house at 100,100 } void house( float x, float y ) { float w=120, h=50; // Size of house fill( RED ); rect( x, y, w, h ); // Red house at (x,y) triangle( x,y, x+w,y, x+w/2,y-50 ); } //// ACTION: sun moves (then resets to random height) void action() { sol.move(); fred.move(); if (fred.x>width) { fredReset(); background(0); } } //// SHOW: creature void show() { fred.show(); } //// 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() { // Set the position (x,y) fred.x= mouseX; fred.y= mouseY; } void keyPressed() { if (key == 'q') { exit(); } if (key == 'r') { fredReset(); sunReset(); } if (key == 's') { sol.y += 50; } } class Sun { float x, y; // Position float dx=1; // METHODS // void show() { fill( YELLOW ); ellipse( x, y, 30, 30 ); // Yellow sun } void move() { x += dx; if (x > width) { x=random(20,120 ); background(YELLOW); } } } class Creature { float x, y; // Position & speed float dx, dy; // Move the creature. void move() { x= x + dx; y= y + dy; } void show() { // Draw creature. fill( BLUE ); rect( x, y, 50, 80 ); // Blue creature ellipse( x+25, y-20, 40, 40 ); // Head on top // Eyes. eye( x+15, y-25 ); eye( x+35, y-25 ); } void eye( float eyeX, float eyeY ) { fill( 255 ); ellipse( eyeX, eyeY, 12, 12 ); fill( 0, 150, 0 ); ellipse( eyeX, eyeY, 4, 4 ); } } |