> show canvas only <


/* built with Studio Sketchpad: 
 *   https://sketchpad.cc
 * 
 * observe the evolution of this sketch: 
 *   https://studio.sketchpad.cc/sp/pad/view/ro.LybEF61vvmx/rev.4983
 * 
 * authors: 
 *   Harrison Wellner

 * license (unless otherwise specified): 
 *   creative commons attribution-share alike 3.0 license.
 *   https://creativecommons.org/licenses/by-sa/3.0/ 
 */ 



// global variables
// type name
// When you look at a ghost it stops
int wid; // width of the ellipse
int hei; // hieght of the ellipse

int x; // x-position of rect
int y; // y position of rect
float pSpeed; // player speed
float gSpeed; // ghost speed
float xPos; // bullet x-pos
float yPos; // bullet y-pos
float targetX;
float targetY;
Wall wall1;
Wall wall2;

Ghost Tim;
Ghost Wabbajack;

boolean[] keyArray;

// sets up the program
void setup() {
  size(500, 500);
  background(200, 230, 250);
  rectMode(CENTER);
  keyArray = new boolean[4]; 
  wid = 50;
  hei = 50;
  xPos = x;
  yPos = y;

  targetX = xPos;
  targetY = yPos;

  x = width/2;
  y = height/2;
  pSpeed = 2;
  gSpeed = 2;

  Tim = new Ghost();
  Wabbajack = new Ghost();
  wall1 = new Wall();
  wall2 = new Wall();
} // end of setup

void draw() {
  background(200, 230, 250);
  color black = color(0);
  color red = color(255,0,0);
  wall1.update();
  wall2.update();

  loadPixels();
  
  if (pixels[(int)((y - 13)*width + x - width)] != red) {
        } else{
            x = width/2;
            y = height/2;
            }
  if (pixels[(int)((y + 13)*width + x - width)] != red) {
        } else{
            x = width/2;
            y = height/2;
            }  
  if (pixels[(int)((y)*width + x + 13)] != red) {    
        } else{
            x = width/2;
            y = height/2;
            }
  if (pixels[(int)((y)*width + x - 13)] != red) {   
        } else{
            x = width/2;
            y = height/2;         
            }

  if (keyArray[0] && pixels[(int)((y - 13)*width + x - width)] != black) {
    y-= pSpeed;
  }
  if (keyArray[1] && pixels[(int)((y + 13)*width + x - width)] != black) {
    y+= pSpeed;
  }  
  if (keyArray[3] && pixels[(int)((y)*width + x + 13)] != black) {
    x+= pSpeed;
  }
  if (keyArray[2] && pixels[(int)((y)*width + x - 13)] != black ) {
    x-= pSpeed;
  } 
    fill(255, 220, 0);  
  rect(x, y, 25, 25);

  if (x < 0) {
    x = 499;
  }
  if (y < 0) {
    y = 499;
  }
  if (x > 500) {
    x = 1;
  }
  if (y > 500) {
    y = 1;
  }
  fill(0, 210, 0);
  Tim.chase();
  fill(155, 0, 155);
  Wabbajack.chase();
  fill(255, 0, 0);
  //ellipse(xPos, yPos, 10, 10);

  //if (targetY > yPos) {
  //  yPos+=2;
  //} else if (targetY < yPos) {
  //  yPos-=2;
  //}  
  //if (targetX > xPos) {
  //  xPos+=2;
  //} else if (targetX < xPos) {
  //  xPos-=2;
  //}
   
} // end of draw

// this function is called when any key is pressed
void keyPressed() {
  if (keyCode == UP) {
    keyArray[0] = true; // y = y - 5;
  }
  if (keyCode == DOWN) {
    keyArray[1] = true; // y = y + 5;
  }
  if (keyCode == RIGHT) {
    keyArray[3] = true; // y = y + 5;
  }
  if (keyCode == LEFT) {
    keyArray[2] = true; // y = y - 5;
  } // end of keyPressed
}

  void keyReleased() {
    if (keyCode == UP) {
      keyArray[0] = false; // y = y - 5;
    }
    if (keyCode == DOWN) {
      keyArray[1] = false; // y = y + 5;
    }
    if (keyCode == RIGHT) {
      keyArray[3] = false; // x = x + 5;
    }
    if (keyCode == LEFT) {
      keyArray[2] = false; // x = x - 5;
    }
  }

  void mousePressed() {
    targetX = mouseX;
    targetY = mouseY;

    if (xPos == mouseX && yPos == mouseY) {
      xPos = x;
      yPos = y;
    }
  } // end of mousepressed

  // Ghost Class
  class Ghost {
    // intance variables
    private float Gx;
    private float Gy;

    // constructor(s)
    public Ghost() {
      Gx = random(width);
      Gy = random(height);
    } // end of default constructor

    // method(s) - what can a Ghost do?
    public void chase() {
      // figure out where the player is
      float diffX = x - Gx;
      float diffY = y - Gy;

      // if Ghost is farther away in the x-dir
      if (abs(diffX) > abs(diffY) ) {
        // move towards player in the x-dir
        // Ghost is to the right
        if (diffX < 0) {
          Gx -= gSpeed;
        } else if (diffX > 0) {
          Gx += gSpeed;
        }
      } // end of if
      else {
        // move towards the player in the y-dir
        // ghost is below
        if (diffY < 0) {
          Gy -= gSpeed;
        } else if (diffY > 0) {
          Gy += gSpeed;
        }
      } // end of else


      ellipse(Gx, Gy, 25, 25);
    } // end of chase
  } // end of Ghost class

  class Wall {
    private float Wx;
    private float Wy;
    private float Wlength;
    private float Wheight;
    
    public Wall() {
      fill(0);
      Wx = random(width);
      Wy = random(height);
      Wlength = random(50,200);
      Wheight = random(50,200);
    } // end of constructor Wall 

    public void update() {
        strokeWeight(3);
      rect(Wx, Wy, Wlength, Wheight);
    }
}