Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
//Gold digging! Interactive Toy
//Zhipu Chen
//10/2/2017  10h TimeSpt

//State position variables for rope
float ropeY;

//State position variables for machine
float machineX, machineY;
float machineSizeX, machineSizeY;

//Variable for machine, bricks and rope's movement speed
int machineSpeed, ropeSpeed, gold1Speed, gold2Speed, gold3Speed, gold4Speed, gold5Speed;

//State variables for gold bricks
float gold1Y, gold2Y, gold3Y, gold4Y, gold5Y, gold1X, gold2X, gold3X, gold4X, gold5X;
float gold1SizeY, gold2SizeY, gold3SizeY, gold4SizeY, gold5SizeY, gold1SizeX, gold2SizeX, gold3SizeX, gold4SizeX, gold5SizeX;

//State variables for the rope status and the bricks status
boolean ropeLosed;
boolean ropeHooked;
boolean gold1Hooked, gold2Hooked, gold3Hooked, gold4Hooked, gold5Hooked;

void setup()
{
  frameRate(60);
  size(400, 400);

  //Set machine position
  machineSizeX=20;
  machineSizeY=20;
  //Set speed
  machineSpeed=5;
  ropeSpeed=3;
  gold1Speed=1;
  gold2Speed=2;
  gold3Speed=3;
  gold4Speed=4;
  gold5Speed=5;
  //Set the machine position
  machineX=width/2;
  machineY=40;
  //The rope's end is on the machine
  ropeY=machineY;
  //Initial rope and bricks status
  ropeLosed=false;
  gold1Hooked=false;
  gold2Hooked=false;
  gold3Hooked=false;
  gold4Hooked=false;
  gold5Hooked=false;
  generateGoldBricks();
}

void draw()
{
  background(225);

  drawBackgrounds();
  machineWave();
  hookBricks();
  moveRope();
  moveBricks();
  drawGoldBricks();
}

void drawBackgrounds()
{
  noStroke();
  fill(150, 90, 0);
  rect(0, 60, width, 60);
  fill(130, 70, 39);
  rect(0, 120, width, 60);
  fill(115, 60, 39);
  rect(0, 180, width, 60);
  fill(100, 40, 39);
  rect(0, 240, width, 60);
  fill(90, 30, 39);
  rect(0, 300, width, 60);
  fill(60, 15, 39);
  rect(0, 360, width, 60);
}

//Generate Bricks randomly
void generateGoldBricks()
{
  rectMode(CORNER);
  gold1Y=random(80, height);
  gold2Y=random(80, height);
  gold3Y=random(80, height);
  gold4Y=random(80, height);
  gold5Y=random(80, height);
  gold1X=random(80, 350);
  gold2X=random(80, 350);
  gold3X=random(80, 350);
  gold4X=random(80, 350);
  gold5X=random(80, 350);
  gold1SizeY=random(10, 70);
  gold2SizeY=random(10, 70);
  gold3SizeY=random(10, 70);
  gold4SizeY=random(10, 70);
  gold5SizeY=random(10, 70);
  gold1SizeX=random(10, 70);
  gold2SizeX=random(10, 70);
  gold3SizeX=random(10, 70);
  gold4SizeX=random(10, 70);
  gold5SizeX=random(10, 70);
}

void drawGoldBricks()
{
  rectMode(CORNER);
  strokeWeight(3);
  stroke(150, 150, 100);
  //Gold
  fill(200, 200, 10);
  //Draw gold bricks if they have not been hooked up yet
  if (!checkHookedUp(gold1Y))
  {
    rect(gold1X, gold1Y, gold1SizeX, gold1SizeY);
  }
  if (!checkHookedUp(gold2Y))
  {
    rect(gold2X, gold2Y, gold2SizeX, gold2SizeY);
  }
  if (!checkHookedUp(gold3Y))
  {
    rect(gold3X, gold3Y, gold3SizeX, gold3SizeY);
  }
  if (!checkHookedUp(gold4Y))
  {
    rect(gold4X, gold4Y, gold4SizeX, gold4SizeY);
  }
  if (!checkHookedUp(gold5Y))
  {
    rect(gold5X, gold5Y, gold5SizeX, gold5SizeY);
  }
}

void machineWave()
{
  //Move the machine, stop the machine when losing the rope
  if (!mousePressed&&ropeY==machineY)
  {
    machineX+=machineSpeed;
  }

  //prevent the machine moving out of screen
  if (machineX>width||machineX<0)
  {
    machineSpeed*=-1;
  }

  ellipseMode(CENTER);
  //Fill with green when all gold bricks have been picked up
  if (gold1Y==0&&gold2Y==0&&gold3Y==0&&gold4Y==0&&gold5Y==0)
  {
    stroke(0);
    //green
    fill(0, 225, 0);
  }
  //Fill with blue while machine is waving
  else if (ropeY==machineY)
  {
    stroke(0);
    //blue
    fill(0, 0, 225);
  } else
  {
    //Fill with red when machine is oprating
    stroke(0);
    //red
    fill(225, 0, 0);
  }
  ellipse(machineX, machineY, machineSizeX, machineSizeY);
}

void moveRope()
{
  //Press mouse to lose the rope
  if (mousePressed)
  {
    ropeLosed=true;
  }
  //Rope keeps going down if no object was hit
  if (ropeLosed&&!ropeHooked)
  {
    ropeY+=ropeSpeed;
    //Roll back the rope when it hits the bottom
    if (ropeY>=height)
    {
      ropeLosed=false;
    }
  } else
  {
    ropeY-=ropeSpeed;
  }
  //Set rope max length
  ropeY=constrain(ropeY, 40, height);

  stroke(0);
  strokeWeight(2);
  line(machineX, machineY, machineX, ropeY);
}

//Check if the rope hit any brick
boolean hitCheck(float ropeBotX, float ropeBotY, float brickX, float brickY, float brickSizeX, float brickSizeY)
{
  if (ropeBotX>=brickX && ropeBotX<=brickX+brickSizeX && ropeBotY>=brickY&&ropeBotY<=brickY+brickSizeY)
  {
    return true;
  } else
  {
    return false;
  }
}

//To check if the brick has been hooked up
boolean checkHookedUp(float brickY)
{
  if (brickY<=machineY)
    return true;
  else
    return false;
}

//Drag bricks up when the rope hits them
void hookBricks()
{
  if (hitCheck(machineX, ropeY, gold1X, gold1Y, gold1SizeX, gold1SizeY)==true&&ropeHooked==false)
  {
    gold1Hooked=true;
    ropeHooked=true;
    ropeLosed=false;
  } else if (hitCheck(machineX, ropeY, gold2X, gold2Y, gold2SizeX, gold2SizeY)==true&&ropeHooked==false)
  {
    gold2Hooked=true;
    ropeHooked=true;
    ropeLosed=false;
  } else if (hitCheck(machineX, ropeY, gold3X, gold3Y, gold3SizeX, gold3SizeY)==true&&ropeHooked==false)
  {
    gold3Hooked=true;
    ropeHooked=true;
    ropeLosed=false;
  } else if (hitCheck(machineX, ropeY, gold4X, gold4Y, gold4SizeX, gold4SizeY)==true&&ropeHooked==false)
  {
    gold4Hooked=true;
    ropeHooked=true;
    ropeLosed=false;
  } else if (hitCheck(machineX, ropeY, gold5X, gold5Y, gold5SizeX, gold5SizeY)==true&&ropeHooked==false)
  {
    gold5Hooked=true;
    ropeHooked=true;
    ropeLosed=false;
  }

  if (gold1Hooked)
  {
    gold1Y=ropeY;

    if (checkHookedUp(gold1Y))
    {
      //Erase the brick once it's hooked up
      ropeHooked=false;
      gold1Hooked=false;
      gold1Y=0;
      gold1X=0;
      gold1SizeX=0;
      gold1SizeY=0;
    }
  } else if (gold2Hooked)
  {
    gold2Y=ropeY;
    if (checkHookedUp(gold2Y))
    {
      ropeHooked=false;
      gold2Hooked=false;
      gold2Y=0;
      gold2X=0;
      gold2SizeX=0;
      gold2SizeY=0;
    }
  } else if (gold3Hooked)
  {
    gold3Y=ropeY;
    if (checkHookedUp(gold3Y))
    {
      ropeHooked=false;
      gold3Hooked=false;
      gold3Y=0;
      gold3X=0;
      gold3SizeX=0;
      gold3SizeY=0;
    }
  } else if (gold4Hooked)
  {
    gold4Y=ropeY;
    if (checkHookedUp(gold4Y))
    {
      ropeHooked=false;
      gold4Hooked=false;
      gold4Y=0;
      gold4X=0;
      gold4SizeX=0;
      gold4SizeY=0;
    }
  } else if (gold5Hooked)
  {
    gold5Y=ropeY;
    if (checkHookedUp(gold5Y))
    {
      ropeHooked=false;
      gold5Hooked=false;
      gold5Y=0;
      gold5X=0;
      gold5SizeX=0;
      gold5SizeY=0;
    }
  }
}

//Make bricks run in different speed
void moveBricks()
{
  if (!gold1Hooked)
  {
    gold1X+=gold1Speed;
    if (gold1X>width||gold1X<0)
    {
      gold1Speed*=-1;
    }
  }
  if (!gold2Hooked) {
    gold2X+=gold2Speed;
    if (gold2X>width||gold2X<0)
    {
      gold2Speed*=-1;
    }
  }
  if (!gold3Hooked) {
    gold3X+=gold3Speed;
    if (gold3X>width||gold3X<0)
    {
      gold3Speed*=-1;
    }
  }
  if (!gold4Hooked) {
    gold4X+=gold4Speed;
    if (gold4X>width||gold4X<0)
    {
      gold4Speed*=-1;
    }
  }
  if (!gold5Hooked) {
    gold5X+=gold5Speed;
    if (gold5X>width||gold5X<0)
    {
      gold5Speed*=-1;
    }
  }
}