function BugtoFood(x1, y1, x2, y2, ind, bugSpeed, color,level) {
  canvas.addEventListener("click", handleClick);
  var x, y, x1, y1, x2, y2,
      f = 0, //progress
      speed,// speed based on dist/steps
      dist,
      bugSpeed,//Required speed.
      steps = bugSpeed / 59.5,//Scale to make required speed.
      ind,
      killed = 0, color;
    /// if we are moving, return
    if (f !== 0) return;

    context.fillStyle = 'rgba(160, 160, 160,0.5)';
    context.fillRect(0,0,canvas.width, 400);
    x= x1;
    y = y1;

    //A bug is produced in the starting position.
    makeBug(x, y, color);
  /*--code to test speed;
      var testx2 = findCloestFood(x, y)[0];
      var testy2 = findCloestFood(x, y)[1];
      var testdx = testx2 - x1;
      var testdy = testy2 - y1;
      road = Math.abs(Math.sqrt(testdx * testdx + testdy * testdy));
      starttime = new Date();
      /// speed will be number of steps / distance
      //speed = steps / dist;
      /// move player --*/
      /*if (pause == 0){
        loop();
      }else{
        BugtoFood(x, y, x2, y2, ind, bugSpeed, color);
      }*/


    /*------------------------------------
      Main loop for bug keep moving!
    --------------------------------------*/
   loop();
    function loop() {
      if(pause==0 && gamelevel == level){
        /// clear current drawn bug.
        context.globalAlpha = 0.3 ;
        context.fillStyle = 'rgba(160, 160, 160,0.5)';
        drawFood();
        f = 0;
        //get updated food position.
        x2 = findCloestFood(x, y)[0];
        y2 = findCloestFood(x, y)[1];

        dx = x2 - x;
        dy = y2 - y;
        dist = Math.abs(Math.sqrt(dx * dx + dy * dy));
        speed = steps / dist;
        f += speed;

        x = x + (x2 - x) * f;
        y = y + (y2 - y) * f;

        //Haven't arrived to food.
        if (f < 1) {
              if (killed == 1){//Bug is killed.
                updateScore(color);
                drawFood();
                return;

              }else{//bug keeps going.
                if(pause == 0){//Game paused.
                context.fillRect(0,0,canvas.width, 500);
                drawFood();
                makeBug(x, y, color);
                requestAnimationFrame(loop);
                }else{
                    requestAnimationFrame(loop);
                }
             }
        }else{//Arrive the food. Kill FOOD.
            if((findCloestFood(x, y)[2]) == ind){
                foodArray.splice(ind, 1);
            }else{//Two bugs eat the food at the same time. Deal with it.
                nextX = findCloestFood(x, y)[0];
                nextY = findCloestFood(x, y)[1];
                ind = findCloestFood(x, y)[2];
                BugtoFood(x, y, nextX, nextY, ind, bugSpeed, color,level);
              }
            //If no more food left, game over.
            if (foodArray.length==0){
                context.clearRect(0, 0, canvas.width, canvas.height);

                //Game over in level 1.
                if(level == 1){
                  score1 = score;
                  score = 0;
                  sethighscore(score1, 1);
                  alert('Game Over!\n' + 'Level 1 Score: ' + score1);
                  window.location.href = "a2.html";

                }else{//Game over in level 2.
                  score2 = score;
                  score = 0;
                  sethighscore(score1, 1);
                  sethighscore(score2, 2);
                  alert('Game Over!\n' + 'Level 1 Score: ' + score1 + '\nLevel 2 Score: ' + score2);
                  window.location.href = "a2.html";
                }
            }
              //After eat the food, redirect the bug.
              drawFood();

              nextX = findCloestFood(x2, y2)[0];
              nextY = findCloestFood(x2, y2)[1];
              ind = findCloestFood(x2, y2)[2];
              BugtoFood(x2, y2, nextX, nextY, ind, bugSpeed, color,level);
              f = 0;//reset.
          }

      }else{//Game is paused.
        loopid = requestAnimationFrame(loop);
      }
  }


    /*------------------------------------
      Add point when bug is killed.
    --------------------------------------*/
    function updateScore(bugType){
      if(bugType == 'black'){
          score += 5;
      }else if (bugType == 'red') {
          score += 3;
      }else{
          score += 1;
      }
    }
    /*------------------------------------
      Handle clicks in the game view port.
    --------------------------------------*/
    function handleClick(e){
      if(pause ==0){
      var rect = canvas.getBoundingClientRect();

      clickX = e.clientX - rect.left;
      clickY = e.clientY - rect.top;

        var xDiff = x - clickX;
        var yDiff = y - clickY;
        var dist = Math.sqrt(xDiff * xDiff + yDiff * yDiff);
        if(dist <= 40){
          killed = 1;
        }else{
           killed = 0;
        }
      }else{
        return;
      }
    }
}
/*------------------------------------
  Function to control Bug's movement.
  Direct them to their target food.
--------------------------------------*/
Function to control Bug's movement.
Direct them to their target food.
Function to control Bug ' s movement . Direct them to their target food .
function BugtoFood(x1, y1, x2, y2, ind, bugSpeed, color,level) {
  canvas.addEventListener("click", handleClick);
  var x, y, x1, y1, x2, y2,
      f = 0, 
      speed,
      dist,
      bugSpeed,
      steps = bugSpeed / 59.5,
      ind,
      killed = 0, color;
    if (f !== 0) return;
    context.fillStyle = 'rgba(160, 160, 160,0.5)';
    context.fillRect(0,0,canvas.width, 400);
    x= x1;
    y = y1;
    makeBug(x, y, color);
   loop();
    function loop() {
      if(pause==0 && gamelevel == level){
        context.globalAlpha = 0.3 ;
        context.fillStyle = 'rgba(160, 160, 160,0.5)';
        drawFood();
        f = 0;
        x2 = findCloestFood(x, y)[0];
        y2 = findCloestFood(x, y)[1];
        dx = x2 - x;
        dy = y2 - y;
        dist = Math.abs(Math.sqrt(dx * dx + dy * dy));
        speed = steps / dist;
        f += speed;
        x = x + (x2 - x) * f;
        y = y + (y2 - y) * f;
        if (f < 1) {
              if (killed == 1){
                updateScore(color);
                drawFood();
                return;
              }else{
                if(pause == 0){
                context.fillRect(0,0,canvas.width, 500);
                drawFood();
                makeBug(x, y, color);
                requestAnimationFrame(loop);
                }else{
                    requestAnimationFrame(loop);
                }
             }
        }else{
            if((findCloestFood(x, y)[2]) == ind){
                foodArray.splice(ind, 1);
            }else{
                nextX = findCloestFood(x, y)[0];
                nextY = findCloestFood(x, y)[1];
                ind = findCloestFood(x, y)[2];
                BugtoFood(x, y, nextX, nextY, ind, bugSpeed, color,level);
              }
            if (foodArray.length==0){
                context.clearRect(0, 0, canvas.width, canvas.height);
                if(level == 1){
                  score1 = score;
                  score = 0;
                  sethighscore(score1, 1);
                  alert('Game Over!\n' + 'Level 1 Score: ' + score1);
                  window.location.href = "a2.html";
                }else{
                  score2 = score;
                  score = 0;
                  sethighscore(score1, 1);
                  sethighscore(score2, 2);
                  alert('Game Over!\n' + 'Level 1 Score: ' + score1 + '\nLevel 2 Score: ' + score2);
                  window.location.href = "a2.html";
                }
            }
              drawFood();
              nextX = findCloestFood(x2, y2)[0];
              nextY = findCloestFood(x2, y2)[1];
              ind = findCloestFood(x2, y2)[2];
              BugtoFood(x2, y2, nextX, nextY, ind, bugSpeed, color,level);
              f = 0;
          }
      }else{
        loopid = requestAnimationFrame(loop);
      }
  }
    function updateScore(bugType){
      if(bugType == 'black'){
          score += 5;
      }else if (bugType == 'red') {
          score += 3;
      }else{
          score += 1;
      }
    }
    function handleClick(e){
      if(pause ==0){
      var rect = canvas.getBoundingClientRect();
      clickX = e.clientX - rect.left;
      clickY = e.clientY - rect.top;
        var xDiff = x - clickX;
        var yDiff = y - clickY;
        var dist = Math.sqrt(xDiff * xDiff + yDiff * yDiff);
        if(dist <= 40){
          killed = 1;
        }else{
           killed = 0;
        }
      }else{
        return;
      }
    }
}
function BugtoFood ( x1 , y1 , x2 , y2 , ind , bugSpeed , color , level ) { canvas . addEventListener ( "click" , handleClick ) ; var x , y , x1 , y1 , x2 , y2 , f = 0 , speed , dist , bugSpeed , steps = bugSpeed / 59.5 , ind , killed = 0 , color ; if ( f !== 0 ) return ; context . fillStyle = 'rgba(160, 160, 160,0.5)' ; context . fillRect ( 0 , 0 , canvas . width , 400 ) ; x = x1 ; y = y1 ; makeBug ( x , y , color ) ; loop ( ) ; function loop ( ) { if ( pause == 0 && gamelevel == level ) { context . globalAlpha = 0.3 ; context . fillStyle = 'rgba(160, 160, 160,0.5)' ; drawFood ( ) ; f = 0 ; x2 = findCloestFood ( x , y ) [ 0 ] ; y2 = findCloestFood ( x , y ) [ 1 ] ; dx = x2 - x ; dy = y2 - y ; dist = Math . abs ( Math . sqrt ( dx * dx + dy * dy ) ) ; speed = steps / dist ; f += speed ; x = x + ( x2 - x ) * f ; y = y + ( y2 - y ) * f ; if ( f < 1 ) { if ( killed == 1 ) { updateScore ( color ) ; drawFood ( ) ; return ; } else { if ( pause == 0 ) { context . fillRect ( 0 , 0 , canvas . width , 500 ) ; drawFood ( ) ; makeBug ( x , y , color ) ; requestAnimationFrame ( loop ) ; } else { requestAnimationFrame ( loop ) ; } } } else { if ( ( findCloestFood ( x , y ) [ 2 ] ) == ind ) { foodArray . splice ( ind , 1 ) ; } else { nextX = findCloestFood ( x , y ) [ 0 ] ; nextY = findCloestFood ( x , y ) [ 1 ] ; ind = findCloestFood ( x , y ) [ 2 ] ; BugtoFood ( x , y , nextX , nextY , ind , bugSpeed , color , level ) ; } if ( foodArray . length == 0 ) { context . clearRect ( 0 , 0 , canvas . width , canvas . height ) ; if ( level == 1 ) { score1 = score ; score = 0 ; sethighscore ( score1 , 1 ) ; alert ( 'Game Over!\n' + 'Level 1 Score: ' + score1 ) ; window . location . href = "a2.html" ; } else { score2 = score ; score = 0 ; sethighscore ( score1 , 1 ) ; sethighscore ( score2 , 2 ) ; alert ( 'Game Over!\n' + 'Level 1 Score: ' + score1 + '\nLevel 2 Score: ' + score2 ) ; window . location . href = "a2.html" ; } } drawFood ( ) ; nextX = findCloestFood ( x2 , y2 ) [ 0 ] ; nextY = findCloestFood ( x2 , y2 ) [ 1 ] ; ind = findCloestFood ( x2 , y2 ) [ 2 ] ; BugtoFood ( x2 , y2 , nextX , nextY , ind , bugSpeed , color , level ) ; f = 0 ; } } else { loopid = requestAnimationFrame ( loop ) ; } } function updateScore ( bugType ) { if ( bugType == 'black' ) { score += 5 ; } else if ( bugType == 'red' ) { score += 3 ; } else { score += 1 ; } } function handleClick ( e ) { if ( pause == 0 ) { var rect = canvas . getBoundingClientRect ( ) ; clickX = e . clientX - rect . left ; clickY = e . clientY - rect . top ; var xDiff = x - clickX ; var yDiff = y - clickY ; var dist = Math . sqrt ( xDiff * xDiff + yDiff * yDiff ) ; if ( dist <= 40 ) { killed = 1 ; } else { killed = 0 ; } } else { return ; } } }
Function to control Bug's movement.
Function to control Bug ' s movement .
//progress // speed based on dist/steps //Required speed. //Scale to make required speed. /// if we are moving, return //A bug is produced in the starting position. /*--code to test speed;
      var testx2 = findCloestFood(x, y)[0];
      var testy2 = findCloestFood(x, y)[1];
      var testdx = testx2 - x1;
      var testdy = testy2 - y1;
      road = Math.abs(Math.sqrt(testdx * testdx + testdy * testdy));
      starttime = new Date();
      /// speed will be number of steps / distance
      //speed = steps / dist;
      /// move player --*/ /*if (pause == 0){
        loop();
      }else{
        BugtoFood(x, y, x2, y2, ind, bugSpeed, color);
      }*/ /*------------------------------------
      Main loop for bug keep moving!
    --------------------------------------*/ /// clear current drawn bug. //get updated food position. //Haven't arrived to food. //Bug is killed. //bug keeps going. //Game paused. //Arrive the food. Kill FOOD. //Two bugs eat the food at the same time. Deal with it. //If no more food left, game over. //Game over in level 1. //Game over in level 2. //After eat the food, redirect the bug. //reset. //Game is paused. /*------------------------------------
      Add point when bug is killed.
    --------------------------------------*/ /*------------------------------------
      Handle clicks in the game view port.
    --------------------------------------*/