////Anthony Porras- Project 2-Using objects ////Monster stalks the hero, who hunts for gold String title="Project 2- Using objects"; String author= "Anthony Porras"; //String score= "Score"; int sum=0; //Declarations float xUlk=173, yUlk=187; float xGold=360, yGold=250; float xMen=150, yMen=437; float xCliff=100, yCliff=300; float xCour=250, yCour=310; int score=0, hero=0, monster=0; float n=0; Hero ulkatron; Monster mennis; Dog clifford; Puppy courage; //Declare sun variable to move float sunX = 50; float horizon; void setup() { //Set the size of the window size(800, 600); horizon= height/4; frameRate(30); //Instantiate the objects: hero, monster, dog, puppy. ulkatron= new Hero(); mennis= new Monster(); clifford= new Dog(); courage= new Puppy(); reset(); } void reset() { //ulkatron.reset(); //mennis.reset(); } void draw() { //Draw a blue sky background background(3, 225, 252); scene(); nugget(); //Show the hero ???? ulkatron.displayHero(); //hero(); mennis.displayMonster(); //monster(); clifford.displayDog(); //dog(); //courage.displayPuppy(); action(); } ////SCENE: grass, house, etc. void scene() { //Draw a light green grass noStroke(); fill(3, 252, 71); rectMode(CORNERS); //-- rect(0, 200, 600, 400); rect(0, horizon, width, height); //Draw a yellow sun noStroke(); fill(240, 233, 17); ellipseMode(CENTER); ellipse(sunX, 50, 40, 40); sunX = sunX+2; if (sunX > width) { sunX = 0; } //Draw a tree with brown trunk noStroke(); fill(185, 96, 11); rectMode(CORNERS); rect(460, 130, 480, 200); fill(26, 167, 2); ellipseMode(CENTER); ellipse(470, 100, 50, 100); //Draw a house noStroke(); fill(242, 83, 120); rectMode(CORNERS); rect(100, 120, 250, 200); triangle(80, 120, 270, 120, 176, 50); //Draw windows on the house stroke(162); fill(255); rectMode(CENTER); rect(130, 140, 30, 30); rect(210, 140, 30, 30); //Draw a door on the house stroke(162); fill(193, 138, 240); rectMode(CORNERS); rect(155, 120, 190, 200); fill(0); text(title, width/3, 20); text(author, 10, height-10); text(score, 170, height-10); //Draw waving grass int endLegs = 800; int y = 550; int x = 0; int spacing = 20; int len = 30; stroke(14, 175, 4); while(x <= endLegs) { line(x, y, x+n, y+len); x = x + spacing; } //Draw a ladder } //Show nugget void nugget() { //Draw gold nugget noStroke(); fill(random(252, 147), 150, random(150)); ellipseMode(CENTER); ellipse(xGold, yGold, 60, 30); //Set CENTER mode ellipseMode(CENTER); rectMode(CENTER); } void action() { ulkatron.move(); clifford.actionDog(); mennis.actionMonster(); courage.actionPuppy(); //Ulkatron found gold if ( dist( ulkatron.xUlk, ulkatron.yUlk, xGold, yGold) < 30) { sum=sum + 100; restart(); } else { sum=sum - 1; } } void restart() { //x= 50;\ //y= random(horizon, height); xGold=random(0, width); yGold=random(horizon, height); } void mousePressed() { xGold= mouseX; yGold= mouseY; restart(); } //// OBJECTS ///// class Hero { float xUlk; float yUlk; Hero() { xUlk=173; yUlk=187; } void move() { if (xUlk>width) { xUlk=173; yUlk=187; } xUlk= xUlk + (xGold-xUlk) / 30; yUlk= yUlk + (yGold-yUlk) / 30; if(xUlk >= width/2) { n = 5; } else { n = -5; } } void displayHero() { //Body stroke(0); fill(19, 60, 160); ellipse(xUlk, yUlk, 45, 45); fill(255, 247, 3); text("Ulkatron", xUlk-23, yUlk+3); //Head stroke(0); fill(250, 233, 187); rectMode(CENTER); rect(xUlk, yUlk-38, 30, 30); //Arms stroke(0); line(xUlk-17, yUlk-16, xUlk-43, yUlk-16); line(xUlk+14, yUlk-16, xUlk+39, yUlk-16); //Legs line(xUlk-17, yUlk+16, xUlk-17, yUlk+51); line(xUlk+17, yUlk+16, xUlk+17, yUlk+51); //Antenna stroke(0); fill(250, 3, 3); ellipse(xUlk, yUlk-67, 5, 5); line(xUlk, yUlk-65, xUlk, yUlk-54); //Eyes stroke(0); fill(255); ellipse(xUlk-7, yUlk-43, 10, 10); ellipse(xUlk+7, yUlk-43, 10, 10); fill(0, 0, 255); point(xUlk+7, yUlk-43); point(xUlk-7, yUlk-43); //Mouth line(xUlk-7, yUlk-30, xUlk+7, yUlk-30); } } //Show the monster class Monster { float xMen; float yMen; Monster() { xMen=150; yMen=437; } void actionMonster() { xMen= xMen + (ulkatron.xUlk - xMen) / 90; yMen= yMen + (ulkatron.yUlk - yMen) / 90; if (xMen>width) { xMen=0; } } void displayMonster() { //Body stroke(0); fill(250, 40, 8); rectMode(CENTER); rect(xMen, yMen, 39, 50); fill(103, 250, 195); text("Mennis", xMen-20, yMen); //Head stroke(0); fill(103, 250, 195); ellipseMode(CENTER); ellipse(xMen, yMen-37, 25, 25); //Arms line(xMen-20, yMen-25, xMen-50, yMen-7); line(xMen+20, yMen-25, xMen+45, yMen-7); //Legs line(xMen-20, yMen+8, xMen-20, yMen+38); line(xMen+19, yMen+8, xMen+19, yMen+38); //Eyes stroke(0); fill(8, 58, 250); ellipseMode(CENTER); ellipse(xMen-5, yMen-40, 8, 14); ellipse(xMen+5, yMen-40, 8, 14); } } class Dog { float xCliff; float yCliff; Dog() { xCliff=100; yCliff=300; } void actionDog() { if (xCliff>width) { //xCliff=0; //yCliff=0; //dx=1; //dy=1; } //-- xCliff= xCliff + (xUlk-xCliff) / 30; //-- yCliff= yCliff + (yUlk-yCliff) / 30; xCliff= xCliff + ( ulkatron.xUlk - xCliff) / 30; yCliff= yCliff + ( ulkatron.yUlk - yCliff) / 30; } void displayDog() { //Body stroke(0); fill(149, 91, 29); rectMode(CENTER); rect(xCliff, yCliff, 60, 30); fill(250, 119, 119); text("Clifford", xCliff-20, yCliff); float dir=1; if (xCliff > ulkatron.xUlk) dir= -1; //Head stroke(0); fill(149, 91, 29); rectMode(CENTER); rect(xCliff+20*dir, yCliff-28, 26, 26); //Eye stroke(0); fill(245, 30, 159); ellipseMode(CENTER); ellipse(xCliff+25*dir, yCliff-30, 6, 12); //Legs stroke(0); line(xCliff-30, yCliff+10, xCliff-30, yCliff+40); line(xCliff+30, yCliff+10, xCliff+30, yCliff+40); //Tail stroke(0); line(xCliff-50*dir, yCliff-30, xCliff-30*dir, yCliff-16); } } class Puppy { float xCour; float yCour; Puppy() { xCour=250; yCour=310; } void actionPuppy() { if (xCour>width) { xCour= xCour + (clifford.xCliff - xCour) / 30; yCour= yCour + (clifford.yCliff - yCour) / 30; } } void displayPuppy() { //Body stroke(0); fill(245, 30, 159); rectMode(CENTER); rect(xCour, yCour, 55, 22); fill(111, 52, 10); text("Courage", xCour-22, yCour+6); //Head stroke(0); fill(245, 30, 159); rectMode(CENTER); rect(xCour-20, yCour-23, 24, 24); //Eye stroke(0); fill(149, 91, 29); ellipseMode(CENTER); ellipse(xCour-24, yCour-24, 6, 12); //Legs stroke(0); line(xCour+26, yCour+12, xCour+26, yCour+25); line(xCour-28, yCour+12, xCour-28, yCour+25); //Tail stroke(0); line(xCour+26, yCour-12, xCour+44, yCour-20); } }