> show canvas only <


/* built with Studio Sketchpad: 
 *   https://sketchpad.cc
 * 
 * observe the evolution of this sketch: 
 *   https://studio.sketchpad.cc/sp/pad/view/ro.FJ0mX0qe3lm/rev.147
 * 
 * authors: 
 *   ben w

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



/* @pjs preload="/static/uploaded_resources/p.7411/frog.png"; */
/* @pjs preload="/static/uploaded_resources/p.7411/grass.jpg"; */

Car[] kia = new Car[7];
Frog jerry;

PImage fg;
PImage back;
void setup() {
  size(400, 400);
  fg = loadImage("/static/uploaded_resources/p.7411/frog.png");
  back = loadImage("/static/uploaded_resources/p.7411/grass.jpg");
  jerry = new Frog(width/2, height-40);
  for (int i = 0; i< kia.length; i++) {
    kia[i] = new Car(random(width-60), 130+i*30);
  }
}


void draw() {
  imageMode(CORNER);
  background(38,180,179);
  image(back,0,80,width,height-80);
  jerry.display();
  jerry.showLives();
  if (jerry.y < 80)jerry.endGame();

  for (int i = 0; i< kia.length; i++) {
    kia[i].display();
    kia[i].move();
    if (kia[i].checkHit(jerry.x, jerry.y)) {
      jerry.reset();
    }
  }
}


void keyPressed() {
  if (key == 'w') {
    jerry.y -= 20;
  }
  if (key == 's') {
    jerry.y += 20;
  }
  if (key == 'a') {
    jerry.x -= 20;
  }
  if (key == 'd') {
    jerry.x += 20;
  }
}


class Car {

  float x, y, mx, xlength, ylength;
  color c, d;

  Car(float myX, float myY) {
    this.x = myX;
    this.y =  myY;
    this.xlength = 60;
    this.ylength = 20;
    this.mx = random(4, 6);
    int m = (int) random(2);
    if(m== 1) mx*=-1
    
    this.c = color(random(255), random(255), random(255));
  }

  void display() {
    fill(c);
    stroke(0);
    rect(x, y, xlength, ylength);
    fill(0, 150);
    rect(x+20, y, 20, ylength);
  }


  boolean checkHit (float fx, float fy) {
    float y2 = y+ylength;
    float x2 = x+xlength;
    if (fy+10 > y && fx+10 > x && fx-10 < x2 && fy -10 < y2)return true;

    //else if (dist(x, y, fx, fy) < 20) return true;

    else return false;
  }

  void reset() {
    for (int i = 0; i< kia.length; i++) {
      kia[i].x = random(width-60);
      kia[i].mx = random(4, 6);
    }
  }
  void move() {
    x+=mx;
    if (x > width + xlength|| x <  0-xlength) {
      mx*=-1;
        this.c = color(random(255), random(255), random(255));
    }
  }
}


class Frog {

  float x, y, radi;
  int lives;

  Frog(float tempX, float tempY) {
    this.x = tempX;
    this.y = tempY;
    radi = 20;
    lives = 3;
  }


  void endGame() {
    textAlign(CENTER);
    textSize(20);
    text("Home Free...", width/2, 20);
    noLoop();
  }

  void reset() {
    x = width/2;
    y = height-40;
    lives--;
    fill(38,180,179);
    rect(0,0,width,80);
    if (jerry.lives <= 0) noLoop();
    for (int i = 0; i< kia.length; i++) {
      kia[i].reset();
    }
  }

  void showLives() {
    fill(0);
    int linex = 0;
    int y = 0;
    int x = 0;
    while (linex < lives) {
      rect(x + 20, y+5, 3, 20);
      linex ++;
      x += 5;
    }
  }

void display() {
  imageMode(CENTER);
  fill(50, 200, 35);
  noStroke();
  // ellipse(x, y, radi, radi);
  image(fg, x, y, radi, radi);
}
}