//////// Exercise x2: modularize exercise x1, and add dog to chase hero. //////// Whoever Whoever (CST 112; today's date?) //////// Please change these to your name and today's date. String author= "Whoever Whatever"; String title= " ??? "; String help= " Click to relocate hero \n 'q' to quit; 'r' to reset. "; int count = 0; //// GLOBALS: coordinates, speed, etc. float horizon; float x, y; // Position. float dx=3, dy=2; // Speed. float dogX, dogY; //// SETUP: window size, initialization (start in middle of screen). void setup() { size( 640,480); horizon= height/4; x= width/2; y= height/2; dx= 3; dy= 2; //frameRate(2); } //// NEXT FRAME: scene, action, show. void draw() { count = count+1; scene(); hero(); dog(); messages(); } //// SCENE: sky, sun, tree, house, etc. void scene() { /* INSERT YOUR CODE HERE! */ background( 200,255,255 ); fill(0); /* REPLACE THIS STUB! */ text( "scene", 100, 100 ); } void messages() { text( title, width/3, 20 ); text( help, width*2/3, 30 ); text( author, 10,height-20 ); } //// ACTION: move (x,y) coordinates of hero & dog; show them. void hero() { /* INSERT YOUR CODE HERE! */ /* REPLACE THIS STUB! */ text( "[[[[ Zoog. ]]]]", 200, 200 ); rect( x,y, 50,80 ); // move hero // x= x + dx; if we take this out the hero stays and dog follows. if (x>width-20) {dx=-dx;} if (x<20) {dx=-dx; } //y= y+ dy; if we take this out the hero stays and dog follows. if (y<20 || y>height-20){ dy= - dy; } } void dog() { dogX= dogX - (dogX-x)/30; // we can use frameRate(30); set frameRate =30 dogY= dogY - (dogY-y)/40; text( dogX, 10, 10 ); text( dogY, 10, 20 ); // fill( 150,0,0 ); rect(dogX,dogY, 60,30 ); /* INSERT YOUR CODE HERE! */ /* REPLACE THIS STUB! */ text( "woof, woof!d!!", 150, 150 ); stroke(150,0,0); strokeWeight(6); if (count/30 % 2 == 0){ line(dogX,dogY+30,dogX,dogY+30+20); line(dogX,dogY+30,dogX+20,dogY+30+20); } else{ line(dogX,dogY+30,dogX,dogY+30+20); line(dogX,dogY+30,dogX-20,dogY+30+20); } strokeWeight(1); } //////// HANDLERS: mouse clicks, keys void mousePressed() { x= mouseX; // Set (x,y) to mouse y= mouseY; // dx= random( -6, +6 ); // random speed. dy= random( -4, +4 ); } void keyPressed() { if (key == 'q') { exit(); // press 'q' key to QUIT. } /* INSERT YOUR CODE HERE! */ }