CS work

profiletenuised
CS.docx

For this assignment you will need to add at least three new behaviors to the following code

class or create your own virtual pet that similar to the Pet class but with its own unique set of behaviors. In this later case your pet must have at least 3 behaviors. It is not necessary for your class to be visually sophisticated. It in fact does not need to be even as complex as my very crude drawing of a dog. The key is to demonstrate some understanding of a class as representing an object with state (pose, tail wag angle, and location for the dog) and operations that can be performed on that object (make it change pose and render itself as a drawing).

/*

This is a very rudimentary virtual pet. It can sit,

lie down, and wag it's tail.

*/

class Pet {

int x, y;

int pose;

int WAG = 1, SLEEP = 2, SIT = 3;

float tailWag, wagSpeed;

Pet(int x, int y) {

this.x = x;

this.y = y;

pose = SIT;

}

// adjust pose and stop tail wagging

void sit() {

pose = SIT;

wagSpeed = 0;

tailWag = 0;

}

// adjust pose and start tail wagging

void wag() {

pose = WAG;

wagSpeed = .1;

}

// adjust pose and stop tail wagging

void sleep() {

pose = SLEEP;

wagSpeed = 0;

tailWag = 0;

}

// draw in selected pose

void draw() {

pushMatrix();

translate(x, y);

if (pose == SIT) {

drawSitting();

}

else if (pose == WAG) {

wagTail();

drawSitting();

}

else {

drawLaying();

}

popMatrix();

}

void drawLaying() {

// needs work :-)

ellipse(0, 0, 150, 60);

}

void wagTail() {

float maxTailWag = .5; // controls how much the tail wags back and forth

tailWag = tailWag + wagSpeed;

// reverse wag direction if the wag limit is reached

if (tailWag > maxTailWag || tailWag < -maxTailWag) {

wagSpeed = -wagSpeed;

}

}

// not pretty but gets the idea across

// origin is the center of the torso

void drawSitting() {

// torso

pushMatrix();

rotate(radians(-30));

ellipse(0, 0, 80, 120);

popMatrix();

ellipse(-20, -70, 60, 60); // head

// nose

pushMatrix();

translate(-55, -55);

rotate(radians(-15));

arc(0, 0, 40, 30, radians(20), radians(310), OPEN);

popMatrix();

// eyes

ellipse(-40, -85, 15, 15); // left eye

ellipse(-25, -80, 15, 15); // right eye

//ear

pushMatrix();

translate(15, -50);

rotate(radians(-20));

ellipse(0, 0, 20, 40);

popMatrix();

//tail

pushMatrix();

translate(40, 30);

rotate(radians(45)+tailWag);

arc(0, -35, 30, 60, radians(-220)-tailWag, radians(80), OPEN);

popMatrix();

// back leg

ellipse(0, 60, 50, 20);

// front leg

pushMatrix();

translate(-50, 30);

rotate(radians(15));

ellipse(0, 0, 30, 60);

popMatrix();

}

}