Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
//////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////      INTERACTIVE TOY      ////////////////////////////////////
////////////////////              Shadow Monsters Attack               ///////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////
////////////////////                  Shirley Tong                     ///////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
////   It seems that you are trapped in a room with several shadow monsters   ////
////         Use the WASD keys to avoid them for as long as possible          ////
////            If you're lucky, maybe they won't eat you tonight             ////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////

//monster one's starting position on X would be random
float monsterOneAnchorPointX;
//monster one's starting position on Y would be random
float monsterOneAnchorPointY;
//size of the monster one width wise
int monsterOneSizeX;
//size of the monster one height wise
int monsterOneSizeY;
//speed of monster one
float monsterOneSpeed;

//monster two's starting position on X would be random
float monsterTwoAnchorPointX;
//monster two's starting position on Y would be random
float monsterTwoAnchorPointY;
//size of the monster two width wise
int monsterDirectionX;
//size of the monster two height wise
int monsterDirectionY;
//speed of monster two
float monsterTwoSpeed;

//player's starting position on X
float playerAnchorPointX;
//player's starting position on Y
float playerAnchorPointY;
//player's width
int playerSizeX;
//player's height
int playerSizeY;
//this int is to calculate the steps player has taken, which will help animate player's feet
int playerStep;

boolean legMove;

boolean goUp;
boolean goDown;
boolean goLeft;
boolean goRight;

boolean gameOver;
//the background square one
float randomOne;
//the background square two
float randomTwo;
//the background darkerner when player is touched
int shading;

//used for testing if functions worked
int test;


//basic setup
void setup() {
  //stating our variables
  monsterOneAnchorPointX = random(0, 350);
  monsterOneAnchorPointY = random(0, 350);
  monsterOneSizeX = 35;
  monsterOneSizeY = 70;
  monsterOneSpeed = 0.015;

  monsterTwoAnchorPointX = random(50, 300);
  monsterTwoAnchorPointY = random(50, 300);
  monsterDirectionX = 1;
  monsterDirectionY = 1;
  monsterTwoSpeed = 4;

  playerAnchorPointX = 200;
  playerAnchorPointY = 200;
  playerSizeX = 35; 
  playerSizeY = 35;
  playerStep = 0; 

  legMove = false;

  goUp = false;
  goDown = false;
  goLeft = false;
  goRight = false;

  gameOver = false;

  randomOne = 245;
  randomTwo = 255;
  shading = 0;

  test = 0;

  size(400, 400);
  smooth();
  noStroke();
}

//draw function made cleaner with homemade functions
void draw() {
  //the game starts
  if (gameOver == false) {
    backgroundChange();

    wallConstraint();

    playerMove();
    updateLegs();
    drawPlayer();

    damageTakenOne();
    damageTakenTwo();

    monsterOneMove();
    drawMonsterOne();
    monsterTwoMove();
    drawMonsterTwo();
    gameOverScreen();

    //stating that if the game is over
  } else if (gameOver == true) {
    background (0);
    fill(255, 0, 0);
    textAlign (CENTER, CENTER);
    textSize (20);
    text("YOU'VE BEEN DEVOURED\n PRESS 'K' TO BE KILLED AGAIN", width/2, height/2);
  }
}

//to create tiles for the background
void backgroundChange() {
  //creates a limit for width and height
  for (int i = 0; i <= width; i = i+50) {
    for (int k = 0; k <= height; k = k+50) {
      rectMode(CORNER);
      //when shading increases, it will subtract from random one and two, making it dark until black
      fill(random(randomOne - shading, randomTwo - shading));
      rect(i, k, 50, 50);
    }
  }
}

//////////////////////////////////////////////
////////        PLAYER DESIGN        /////////
//////////////////////////////////////////////

//draws and create player character
void drawPlayer() {
  //head
  fill(255);
  ellipse(playerAnchorPointX, playerAnchorPointY-30, 25, 25);
  quad(playerAnchorPointX-2, playerAnchorPointY-20, playerAnchorPointX+2, playerAnchorPointY-20, playerAnchorPointX+5, playerAnchorPointY+2, playerAnchorPointX-5, playerAnchorPointY+2);
}

//the legs of the player in standing still position
void drawPlayerWait() {
  //legs
  fill(255);
  quad(playerAnchorPointX+1, playerAnchorPointY+2, playerAnchorPointX+5, playerAnchorPointY+2, playerAnchorPointX+4, playerAnchorPointY+12, playerAnchorPointX+2, playerAnchorPointY+12);
  quad(playerAnchorPointX-1, playerAnchorPointY+2, playerAnchorPointX-5, playerAnchorPointY+2, playerAnchorPointX-4, playerAnchorPointY+12, playerAnchorPointX-2, playerAnchorPointY+12);
}

////////////////////////////////////
/////      LEG MOVEMENTS      //////
////////////////////////////////////
//the legs of the player in with one leg up
void drawPlayerLegMoveOne() {
  //legs
  fill(255);
  //the left foot goes up
  quad(playerAnchorPointX+1, playerAnchorPointY-1, playerAnchorPointX+5, playerAnchorPointY-1, playerAnchorPointX+4, playerAnchorPointY+9, playerAnchorPointX+2, playerAnchorPointY+9);
  quad(playerAnchorPointX-1, playerAnchorPointY+2, playerAnchorPointX-5, playerAnchorPointY+2, playerAnchorPointX-4, playerAnchorPointY+12, playerAnchorPointX-2, playerAnchorPointY+12);
}

//the legs of the player in with the other leg up
void drawPlayerLegMoveTwo() {
  //legs
  fill(255);
  quad(playerAnchorPointX+1, playerAnchorPointY+2, playerAnchorPointX+5, playerAnchorPointY+2, playerAnchorPointX+4, playerAnchorPointY+12, playerAnchorPointX+2, playerAnchorPointY+12);
  //right foot goes up
  quad(playerAnchorPointX-1, playerAnchorPointY-1, playerAnchorPointX-5, playerAnchorPointY-1, playerAnchorPointX-4, playerAnchorPointY+9, playerAnchorPointX-2, playerAnchorPointY+9);
}

void updateLegs() {
  if (legMove == false)
  {
    drawPlayerWait();
  } 
  //IF KEY IS PRESSED, THEN DISPLAY EITHER WALKING MODE BASED ON THE STEPS
  if (legMove == true)
  {
    //basically, if the remainder of the steps are less than 20, then that displays one foot up, if it is greater, than the other foot is displayed
    if (playerStep%40<20)    
    { 
      //if the remainder is less than 20, lift one leg up
      drawPlayerLegMoveOne();
    } else
    { 
      //if the remainder is greater than 20, lift the other leg up
      drawPlayerLegMoveTwo();
    }
  }
}

//////////////////////////////////////////////
////////         DAMAGE RATER        /////////
//////////////////////////////////////////////

//when the monster approaches the player within its space, the screen is darked to show damage
void damageTakenOne() {
  //this helps figure out when the monster is touching the player
  //if the monster approachs the player within these coodinates, then...
  if (monsterOneAnchorPointX - playerAnchorPointX >= -40 && monsterOneAnchorPointX - playerAnchorPointX <= 10 && monsterOneAnchorPointY - playerAnchorPointY >= -105 && monsterOneAnchorPointY - playerAnchorPointY <= 10) {
    //the shading increases each time the monster hits the player
    shading+=2;
    //the shading helps darken the screen
  }
}

//when the monster approaches the player within its space, the screen is darked to show damage
void damageTakenTwo() {
  //this helps figure out when the monster is touching the player
  //if the monster approachs the player within these coodinates, then...
  if (monsterTwoAnchorPointX - playerAnchorPointX >= -95 && monsterTwoAnchorPointX - playerAnchorPointX <= 10 && monsterTwoAnchorPointY - playerAnchorPointY >= -100 && monsterTwoAnchorPointY - playerAnchorPointY <= 10) {
    //the shading increases each time the monster hits the player
    shading+=2;
    //the shading helps darken the screen
  }
}

//////////////////////////////////////////////
////////        MONSTER DESIGN       /////////
//////////////////////////////////////////////
/////        MONSTER ONE      //////
////////////////////////////////////

//design of the monster one
void drawMonsterOne() {
  fill(0);
  //head
  ellipse(monsterOneAnchorPointX+15, monsterOneAnchorPointY+15, 30, 30);
  //body
  quad(monsterOneAnchorPointX+13, monsterOneAnchorPointY+25, monsterOneAnchorPointX+17, monsterOneAnchorPointY+25, monsterOneAnchorPointX+19, monsterOneAnchorPointY+60, monsterOneAnchorPointX+11, monsterOneAnchorPointY+60);
  //legs
  quad(monsterOneAnchorPointX+12, monsterOneAnchorPointY+60, monsterOneAnchorPointX+13, monsterOneAnchorPointY+60, monsterOneAnchorPointX+13, monsterOneAnchorPointY+70, monsterOneAnchorPointX+12, monsterOneAnchorPointY+70);
  quad(monsterOneAnchorPointX+17, monsterOneAnchorPointY+60, monsterOneAnchorPointX+18, monsterOneAnchorPointY+60, monsterOneAnchorPointX+18, monsterOneAnchorPointY+70, monsterOneAnchorPointX+17, monsterOneAnchorPointY+70);
  //the mouth
  fill(255);
  arc(monsterOneAnchorPointX+15, monsterOneAnchorPointY+19, 25, 32, -PI, 0);
  //this is the other arc to help darken the white part of the mouth, making it frown
  fill(0);
  arc(monsterOneAnchorPointX+15, monsterOneAnchorPointY+20, 26, 25, -PI, 0);
} 

//the movement of monster one is based on the where the last position of the player is multiplied by the speed
void monsterOneMove() {
  //for the monster to follow the player, last position of the monster and player together, where player is subtracted by the last position of the monster, which is then multipled by the speed
  //we add the monster with the player position because we want the monster to constantly add itself so it is in the last position of the player
  //the reason why we subtract player from monster is because if we left it alone, then the monster would automatically go to player, so we add '-monster' because we want it to follow
  monsterOneAnchorPointX = monsterOneAnchorPointX + (playerAnchorPointX - monsterOneAnchorPointX) * monsterOneSpeed;
  monsterOneAnchorPointY = monsterOneAnchorPointY + (playerAnchorPointY - monsterOneAnchorPointY) * monsterOneSpeed;
}

////////////////////////////////////
/////        MONSTER TWO      //////
////////////////////////////////////

//design of the monster one
void drawMonsterTwo() {
  //legs    
  fill(0);
  arc(monsterTwoAnchorPointX+20, monsterTwoAnchorPointY+55, 30, 45, -PI, -HALF_PI);
  arc(monsterTwoAnchorPointX+70, monsterTwoAnchorPointY+55, 30, 45, -HALF_PI, 0);

  fill(0);
  arc(monsterTwoAnchorPointX+48, monsterTwoAnchorPointY+58, 30, 45, -HALF_PI, 0);
  arc(monsterTwoAnchorPointX+42, monsterTwoAnchorPointY+58, 30, 45, -PI, -HALF_PI);

  //body
  fill(0);
  ellipse(monsterTwoAnchorPointX+45, monsterTwoAnchorPointY+25, 90, 50);
  //large middle eye
  fill(255);
  arc(monsterTwoAnchorPointX+45, monsterTwoAnchorPointY+20, 25, 15, -PI, 0);
  arc(monsterTwoAnchorPointX+45, monsterTwoAnchorPointY+20, 25, 15, 0, PI);
  //iris
  fill(0);
  ellipse(monsterTwoAnchorPointX+45, monsterTwoAnchorPointY+20, 5, 15);

  //left side eye 1
  fill(255);
  arc(monsterTwoAnchorPointX+23, monsterTwoAnchorPointY+21, 7, 5, -PI, 0);
  arc(monsterTwoAnchorPointX+23, monsterTwoAnchorPointY+21, 7, 5, 0, PI);
  //iris
  fill(0);
  ellipse(monsterTwoAnchorPointX+23, monsterTwoAnchorPointY+21, 2, 5);
  //left side eye 2
  fill(255);
  arc(monsterTwoAnchorPointX+11, monsterTwoAnchorPointY+22, 7, 5, -PI, 0);
  arc(monsterTwoAnchorPointX+11, monsterTwoAnchorPointY+22, 7, 5, 0, PI);
  //iris
  fill(0);
  ellipse(monsterTwoAnchorPointX+11, monsterTwoAnchorPointY+22, 2, 5);

  //right side eye 1
  fill(255);
  arc(monsterTwoAnchorPointX+68, monsterTwoAnchorPointY+21, 7, 5, -PI, 0);
  arc(monsterTwoAnchorPointX+68, monsterTwoAnchorPointY+21, 7, 5, 0, PI);
  //iris
  fill(0);
  ellipse(monsterTwoAnchorPointX+68, monsterTwoAnchorPointY+21, 2, 5);
  //right side eye 2
  fill(255);
  arc(monsterTwoAnchorPointX+80, monsterTwoAnchorPointY+22, 7, 5, -PI, 0);
  arc(monsterTwoAnchorPointX+80, monsterTwoAnchorPointY+22, 7, 5, 0, PI);
  //iris
  fill(0);
  ellipse(monsterTwoAnchorPointX+80, monsterTwoAnchorPointY+22, 2, 5);
}

//monster two's movement is that everytime it hits the wall, it will make a 45 degree angle the other direction
void monsterTwoMove() {
  monsterTwoAnchorPointX = monsterTwoAnchorPointX + (monsterTwoSpeed * monsterDirectionX);
  monsterTwoAnchorPointY = monsterTwoAnchorPointY + (monsterTwoSpeed * monsterDirectionY);

  //when monster hits the sides wall, it needs to change direction by multiplying it by -1
  if (monsterTwoAnchorPointX > width - 90|| monsterTwoAnchorPointX < 0) {
    monsterDirectionX = monsterDirectionX * -1;
  }
  //when monster hits the top and bottom wall, it needs to change direction
  if (monsterTwoAnchorPointY > height - 60|| monsterTwoAnchorPointY < 0) {
    monsterDirectionY = monsterDirectionY * -1;
  }
  //the speed increases overtime
  monsterTwoSpeed=monsterTwoSpeed+0.001;
}

//////////////////////////////////////////////
////////   MOVEMENT AND CONSTRAINS   /////////
//////////////////////////////////////////////

//the game over screen
void gameOverScreen () {
  //when the shading increases to 400 or more
  if (shading >= 400) {
    gameOver = true;
  }
}

//the wall and boundary of the game
void wallConstraint() {
  //when the player hits the side of the walls
  if (playerAnchorPointX - playerSizeX/2 <= 0) {
    //it will continue to hit the side of the wall and can no longer go left
    playerAnchorPointX = 0 + playerSizeX/2;
    goLeft = false;
    //if it hits the other side of the wall
  } else if (playerAnchorPointX + playerSizeX/2 >= width) {
    //will continue to hit that wall again and no longer allowed to go right
    playerAnchorPointX = width - playerSizeX/2;
    goRight = false;
  } 
  //basically the same as above, but written for up and down
  if (playerAnchorPointY - playerSizeY <= 12) {
    playerAnchorPointY = 12 + playerSizeY;
    goUp = false;
  } else if (playerAnchorPointY + playerSizeY/2 >= height) {
    playerAnchorPointY = height - playerSizeY/2;
    goDown = false;
  }
}

//the players movement
void playerMove() {
  if (goUp == true) {
    //when up is pushed, the player will go +2 in pixels in any direction, along with adding 2 steps in player
    playerAnchorPointY -= 2;
    playerStep += 2;
  } 
  if (goLeft == true) {
    playerAnchorPointX -= 2;
    playerStep += 2;
  } 
  if (goDown == true) {
    playerAnchorPointY += 2;
    playerStep += 2;
  } 

  if (goRight == true) {
    playerAnchorPointX += 2;
    playerStep += 2;
  }
}

//setting up the WASD so it will respond
//included a capital version so that if someone accidentally CapLocks, it will still play
void keyPressed() {
  if (key == 'w' || key == 'W') {
    goUp=true;
    legMove = true;
  } 
  if (key == 'a' || key == 'A') {
    goLeft=true;
    legMove = true;
  } 
  if (key == 's'|| key == 'S') {
    goDown=true;
    legMove = true;
  } 
  if (key == 'd'|| key == 'D') {
    goRight=true;
    legMove = true;
  }
  //if 'k' or 'K' if pressed AND if it is game over, it will run setup again
  if ((key == 'k'|| key == 'K') && gameOver == true) {
    setup();
  }
}

//if WASD is released, player will stop
void keyReleased() {
  if (key == 'w' || key == 'W') {
    goUp=false;
    legMove = false;
  }
  if (key == 'a' || key == 'A') {
    goLeft=false;
    legMove = false;
  }
  if (key == 's' || key == 'S') {
    goLeft=false;
    goDown=false;
  }
  if (key == 'd' || key == 'D') {
    goLeft=false;
    goRight=false;
  }
}