Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
/*Pushing Blocks Assignment 2, Interactive Toy by Patrick Li
 The idea behind this design was to create a toy 
 where you as a player could push blocks around
 as well as blink.

 */









int i =0;
float playerEye; //eye
float playerPositionX; //x
float playerPositionY; //y
float playerWidth; //width
float playerHeight; //height

float boxPosX; //x of box
float boxPosY; //y of box
float boxWidth; //width of box
float boxHeight; //height of box
float playerSpeed= 3; // player movement speed
float speedX =3; // general speed of all interactables within X
float speedY =0; // general speed for all interactables within Y
float gravity = 0.09; // gravity of all objects
boolean jump = false; // jumping
boolean moveLeft = false;// moving left
boolean moveRight = false;// moving right
boolean blink = false;// blinking
boolean doRectOverlay = false; // checking if the rectangles overlay
float color1 = 169;
float color2 = 232;
float color3 = 226;


//////////////////////////////////////////////////////
//setup and defining certain variables such as playerPosition and boxPos
void setup() {
  size(400, 400);

  playerPositionX = 180;
  playerPositionY = 240;
  boxPosX = 300;
  boxPosY = 240;
}
/////////////////////////////////////////////////////


//////////////////////////////////////////////////
//void draw; draw all the functions
void draw() {
  background(255);

  makeGrass();
  boxWallCollision();
  displayBackground();
  playerCharacter();
  displayMovement();
  eyeBlink();
  displayObject();
  makeCollision();
  playerWidth=20;
  playerHeight=20;
  boxWidth = 20;
  boxHeight = 20;
  updatePlayerPosition();
}
//////////////////////////////////


/////////////////////////////////////////////////////////////////
//creates the player character with all his unique features
void playerCharacter() {
  //these lines of code will draw the player and it's 'eye'
  fill(color1, color2, color3);
  stroke(0);
  rectMode(CORNERS);
  rect(playerPositionX, playerPositionY, playerPositionX+playerWidth, playerPositionY +playerHeight);
  ellipseMode(CENTER);
  fill(0);

  stroke(0);
  ellipse(playerPositionX+10, playerPositionY+10, 10, playerEye);
}
//////////////////////////////////////////////////////////////////


//////////////////////////////////////////////
//displays the background and the floor
void displayBackground() {
  //this line of code will draw the background
  fill(155);
  rectMode(CENTER);
  rect(200, 360, 440, 200);
}
////////////////////////////////////////////////


////////////////////////////////////////
  //this line of code will display all movement of the playerCharacter, from blinking to moving
void displayMovement() {
  if (moveLeft==true) 
  {
    playerPositionX-=playerSpeed;
  } 
  if (moveRight==true)
  {
    playerPositionX+=playerSpeed;
  }
  if (jump ==true) {
    playerPositionY-=2.5;
  }
}
//////////////////////


/////////////////////////////////////////////////////////////////////
//displays the objects (e.g. the box)
void displayObject() {
  //this displays the objects
  fill(255);
  rect(boxPosX, boxPosY, boxPosX+boxWidth, boxPosY+boxHeight);
}
////////////////////////////////////



////////////////////
//allows pressing the according keys to perform actions
void keyPressed() {

  if (key == 'd') {
    moveRight = true;
  } 
  if (key == 'a') {
    moveLeft = true;
  }
  if (key =='w') {
    jump = true;
  }
}
/////////////////////////////


/////////////////////////////////////////////////////////
//makes the character 'blink' based on a random timer
void eyeBlink() {

  if (blink == false)
  {//this determines how often the player blinks
    float timer = random(1, 100);
    if ((int)timer == 2)
    {
      blink = true;
      playerEye = 10;
    }
  }

  if (blink == true)
  {
    //this determines the blinking speed
    playerEye -= 1; // Blink Speed
    if (playerEye <= 0)
    {
      playerEye = 0;
      blink = false;
    }
  } else
  {
    if (playerEye < 10)
    {
      playerEye += 1; // Blink Speed
    }
  }
} 
////////////////////////////////////////

/////////////////////////////
// creates collision for the player character and the box using boolean rectOverlay
void makeCollision() {
  rectMode(CORNERS);

  rect(boxPosX, boxPosY, boxPosX+boxWidth, boxPosY+boxHeight);
  if (rectOverlay(playerPositionX, playerPositionY, playerPositionX+playerWidth, playerPositionY +playerHeight, boxPosX, boxPosY, boxPosX+boxWidth, boxPosY+boxHeight) == true) {
 if (moveRight==true) {
      boxPosX+=speedX;
    }
  }
  if
    (rectOverlay(playerPositionX, playerPositionY, playerPositionX+playerWidth, playerPositionY +playerHeight, boxPosX, boxPosY, boxPosX+boxWidth, boxPosY+boxHeight) == true) 
  {
    if (moveLeft==true) {
      boxPosX-=speedX;
    }
  }
}
//////////////////////////////////

/////////////////////////////////////////////////
//Creates wall collision only for the box
void boxWallCollision() {
 boxPosX= constrain(boxPosX,0, 380);
 doRectOverlay = false;
  }

////////////////////////////////////////////

//creates the interaction with the box, and allows the player character to act independently to the box
boolean rectOverlay(float left, float top, float right, float bottom, float otherLeft, float otherTop, float otherRight, float otherBottom)
{
  return !(left > otherRight || right < otherLeft || top > otherBottom || bottom < otherTop);
}

//updates the player position, and implements basic jump physics.
void updatePlayerPosition() {
  speedY+=gravity;
  playerPositionY+=speedY;
  if (playerPositionY>240) {
    playerPositionY=240;
    speedY=-speedY;
    speedY*=0.05;
  }
}
// allows key releasing to occur when directional inputs are released
void keyReleased() {
  if (key == 'd') {
    moveRight = false;
  }
  if (key == 'a') {
    moveLeft = false;
  }
  if (key =='w') {
    jump = false;
  }
}


//creates a grass background that waves around, pretty fun.
void makeGrass() {
  for (int i = 0; i <= width; i+=10) {
    noStroke();
    fill(125, 222, 147);
    triangle(i+sin(frameCount*.1), 255, i-5, 260, i+5, 260);
  }
}