Project #2
CST 112

    Click to enlarge screenshot.

Write new object-based code to satisfy the requirements in Project #1 & exam q1 , plus some additional requirements (below).
In your folder, submit your code by April 1, in a .pde file whose name begins with p2.

NOTE:   Do not copy your p1 code.
Instead, use it as a guide to write new object-based code, using class definitons for:

Each class should include declarations for necessary properties as well as a show() method to display itself on the screen (with animation if required), plus other methods, as needed.
Objects that move will need either a move( ) method OR a chase( x2, y2 ) method.

MODIFICATIONS:  

ARRAYS & LOOPS (see Chapter 9):  
Project #2 will also require an array for the 3 trees, and a for loop to display them.
NOTE: Rows of flowers & rocks (from q1) are not required; code for these may be removed. The ladder should remain.

OPTIONAL -- Here are some ideas for extra credit:



Your Project #2 objects may begin with declarations similar to the following.
Your declarations should be different:   Use first letters of your name for the "Hero" & the "Dog" -- rather than "yoshio" & " noodles", and choose another name for the monster.
//// CST 112 Project #2 / Your Name ////
String title=  "Project #2";
String author=  "Your Name";

Hero yoshio;
Dog noodles;
Monster vlad;
Star sol;
House home;
int ntrees=3;
Tree[] trees = new Tree[ntrees];

boolean day=true;
float horizon;
// . . . (more declarations) . . . 

Your Object-Oriented Project #2 may include code similar to the following.
void setup() {
    size( 800, 600 );
    horizon=  height / 4;
	
    // Instatiation of objects. //
    yoshio = new Hero();
    noodles = new Dog();
    vlad = new Monster();
    sol = new Star();
    for (int n=0; n<ntrees; n=n+1) {
          trees[n] = new Tree();
    }
    home = new House();
    // . . . (more setup) . . .
    reset();
}
void reset() {
    yoshio.reset();
    monster.reset();
    for (int n=0; n<ntrees; n=n+1) {
          trees[n].reset();
    }
}

void draw() {
    scene();
    action();
    messages();
}
// . . . 
void scene() {
    if (day) background( SKY );
    else background( NITE );
    sol.show();
    grass();
    ladder();
    for (int n=0; n<ntrees; n=n+1) {
          trees[n].show();
    }
    home.show();
    // . . . 
} 
Your action function may include include some lines of code similar to the following.
    yoshio.move();
    noodles.chase( yoshio.x, yoshio.y );
    if ( ! day ) vlad.chase( noodles.x, noodles.y );
    // . . . more action code . . . 
You will also need some code to check whether the monster catches the hero, and update the score accordingly.


Below is an example of code for a Star class.
class Star {
    // MEMBER DATA. //
    float x=0, y=50, dx=1, dy=0;	// Position and speed.
    float w=30, h=30;			// Width & Height of ellipse.
    color c = color(255,255,0);		// Yellow. (Changes at nite.)
    // METHODS //
    void show() {
	if (day) c = color(255,255,0);	// Yellow sun.
	else c = color(200,180,180);	// Pale moon.
	fill(c);
	ellipse( x,y, w,h );
    }
    void move() {
	if (x > width) { sunset(); }	// Night & Day.
	x = x + dx;
	y = y + dy;
    }
    void sunset() {
	day = ! day;			// Switch nite/day
	x = 0;				// Reset to left
    }
} 

Below is an example of a chase() method that could be used in the Monster class (and/or the Dog class).
class Monster {
    // MEMBER DATA. //
    float x=0, y=50, xx=1, yy=0;	// Position & speed.

    // . . .

    void chase( float x2, float y2 ) {
	if ( day ) { return; }		// No day chasing.
	// Slowly chase (x2,y2)
	xx =  (x2-x) / 120;
	yy =  (y2-y) / 120;
	move();
    }
    void move() {
	x = x + xx;
	y = y + yy;
    }
    // . . .
}