Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
/*
 Spookem's Mansion by James Pratt
 Assignment#2: Interactive Toy
 
 My interactive toy stars a cute ghost named Spookem and a little dude named Little Dude (duh).
 You control an invisible Spookem with your mouse. Clicking causes him to go visible.
 Little Dude will walk back and forth and turn around when he reaches an edge of the screen.
 If he spots Spookem in front of him, he'll get scared and stop moving.
 All the while theres a storm outside, and lighting will randomly flash outside.
 */


// VARIABLES \\
//guy variables
float guyX; //where Little Dude is horizontally
float guyY; //where Little Dude is vertically (doesn't change but makes it a solid point of reference)
int speed = 2; //how fast Dude goes across the screen
float guyEye; //detirmines where the far eye goes
boolean guyScared; //detirmines if Dude is scared or not
int directionFacing; //sets the direction Little Dude is facing, 1 = right, 0 = left

//Spookem variables
float spookemsOpacity = 0; //makes spookems invisible intially
float tailOffset= 10; //distance between all of Spookems tail pieces
boolean spookemsVisible = false; //sets spookems visiblity to false so Little Dude can't see him. When it's true, Dude can see him if he's facing him

//Environment variable
int windowLightning; //Changes the color of the window to white when lightingChance is called
boolean leftEdge; //left edge of the screen
boolean rightEdge; //left edge of the screen


// SETUP \\

void setup() {
  frameRate(60);
  size(800, 600);
  smooth();
  rectMode(CORNERS);
  ellipseMode(CENTER);

  //Set Guy's positon on the screen
  guyX=0;
  guyY=height/2;
}
//EXECUTION\\
//Conditionals set whether or not Little Dude is facing left or right based on the last edge he hit.
void draw() {
  if (guyX==width) {
    rightEdge=true;
    leftEdge=false;
  } else if (guyX==0) {
    leftEdge=true;
    rightEdge=false;
  }
//If Little Dude is facing Spookem, Spookem is in front of Little Dude and Spookem is visible, Dude will get scared and stop moving. Otherwise, he keeps moving
//This is for when Dude is moving left from the right edge of the screen.
  if (rightEdge==true) {
    if ((spookemsVisible==true)&&(mouseX<guyX)&&(directionFacing==0)) {
      stopMoving();
      guyScared=true;
    } else {  
      moveLeft();
      guyEye=guyX-20;
      guyScared=false;
    }
//Same as above, but for moving right from the left edge.
  } else if (leftEdge==true) {
    if ((spookemsVisible==true)&&(mouseX>guyX)&&(directionFacing==1)) {
      stopMoving();
      guyScared=true;
    } else {
      moveRight();
      guyEye=guyX+20;
      guyScared=false;
    }
  }
  
  guyX+=speed; //guyX's postion adds 1 every frame
  noCursor();
  spookemsOpacity= constrain(spookemsOpacity, 0, 200); //sets that spookems has a defined invisible state and visible state without constantly subtracing 100 from its opacity
  
  //makes SpookeguyX  m visible when mouse is clicked
  if (mousePressed == true) {
    spookemsOpacity +=100;
    spookemsVisible = true;
  } else {
    spookemsOpacity -=100;
    spookemsVisible = false;
  }
  //makes lightning randomly flash in the window
  int lightningChance= int (random(10));
  if (lightningChance==5) {
    windowLightning= 255;
  } else {
    windowLightning= 0;
  }
  //DISPLAY\\
  // Clear Screen
  background(82, 73, 92);
  drawFloor();
  drawWindow();
  drawPortrait();
  drawLittleGuy();
  drawSpookems();
}

void drawFloor() {
  //Draw floor\\
  noStroke();
  fill(107, 103, 76);
  rect(0, 500, 800, 600);
}

void drawSpookems() {
  //Spookem\\
  //Draws Spookem's body
  noStroke();
  fill(169, 219, 176, spookemsOpacity);
  rect(mouseX-30, mouseY-90, mouseX+30, mouseY);

  //Draw Spookem's head
  noStroke();
  arc(mouseX, mouseY-90, 60, 60, PI, TWO_PI, OPEN);

  //Draw Spookem's face
  stroke(133, 158, 137);
  strokeWeight(2);
  line(mouseX-10, mouseY-90, mouseX-10, mouseY-70);
  line(mouseX+10, mouseY-90, mouseX+10, mouseY-70);
  arc(mouseX, mouseY-60, 10, 20, 0, PI);
  noStroke();
  fill(247, 255, 248, spookemsOpacity);
  ellipse(mouseX-10, mouseY-60, 10, 5);
  ellipse(mouseX+10, mouseY-60, 10, 5);

  //Draw Spookem's tail 
  for (int i =0; i < 6; i++) {
    noStroke();
    fill(169, 219, 176, spookemsOpacity);
    ellipse(mouseX-25+i*tailOffset, mouseY, 10, 10);
  }
}

void drawWindow() {
  //Window\\
  //draws frame
  fill(107, 103, 76);
  quad(60, 90, 270, 90, 230, 270, 100, 270);

  //draw window
  fill(windowLightning);
  stroke(0);
  strokeWeight(1);
  quad(80, 100, 250, 100, 220, 260, 110, 260);

  //draw window panes
  stroke(255);
  line(130, 100, 130, 260);
  line(200, 100, 200, 260);
  line(96, 190, 233, 190);
}

void drawPortrait() {
  //Portrait\\
  //draw frame
  noStroke();
  fill(209, 209, 73);
  rect(600, 200, 700, 320);

  //draw portrait background
  fill(129, 145, 116);
  rect(610, 210, 690, 310);

  //head
  fill(229, 237, 206);
  rect(630, 230, 660, 270);

  //nose
  ellipse(660, 250, 20, 10);

  //moustache
  fill(74, 77, 66);
  ellipse(655, 260, 20, 10);

  //eye
  stroke(0);
  strokeWeight(2);
  line(650, 240, 650, 250);

  //hat
  noStroke();
  fill(74, 77, 66);
  rect(620, 230, 670, 240);
  rect(630, 215, 660, 230);

  //shirt
  quad(630, 270, 655, 270, 650, 310, 610, 310); 

  //chin
  fill(229, 237, 206);
  ellipse(660, 270, 15, 10);
}

void drawLittleGuy() {
  //Little guy\\
  //body
  fill (25, 85, 145);
  triangle(guyX, guyY+100, guyX-30, guyY+190, guyX+30, guyY+190);
  //head
  fill(232, 219, 195);
  ellipse(guyX, guyY+80, 60, 60);
  //eyes
  //If Dude is scared, then draw the scared face. Otherwise draw the neutral face.
  if (guyScared==true){
    scaredFace();
  }
   else{
  fill(0);
  ellipse(guyX, guyY+75, 5, 10);
  ellipse(guyEye, guyY+75, 5, 10);
   }
  //shoes
  fill(46, 48, 44);
  ellipse(guyX-20, guyY+200, 30, 20);
  ellipse(guyX+20, guyY+200, 30, 20);
}

void moveRight() {
  speed=2;
  directionFacing=1;
}

void moveLeft() {
  speed=-2;
  directionFacing=0;
}

void stopMoving() {
  speed=0;
}

void scaredFace() {
  noStroke();
  fill(255);
  ellipse(guyX, guyY+75,15,15);
  ellipse(guyEye, guyY+75,15,15);
  fill(0);
  ellipse(guyX,guyY+75,5,5);
  ellipse(guyEye,guyY+75,5,5);
}