> show canvas only <


/* built with Studio Sketchpad: 
 *   https://sketchpad.cc
 * 
 * observe the evolution of this sketch: 
 *   https://studio.sketchpad.cc/sp/pad/view/ro.YRCtYAyfOjI/rev.49
 * 
 * authors: 
 *   GoToLoop
 *   

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



/*
 * Rain Game (v1.51)
 * by SoundMan4 (2012/Dec)
 *
 * http://forum.processing.org/topic/works-in-java-but-not-javascript
 */

final static byte numRainDrop = 50;
final static RainDrop[] rain = new RainDrop[numRainDrop];
Bucket buck;

boolean isGameOver, right, left;
int dropsMissed, dropsCaught;

void setup() {

  size(400, 400);
  frameRate(60);
  smooth();
  noStroke();
  textAlign(CENTER);

  buck = new Bucket();

  for ( byte i=0; i!=numRainDrop; rain[i++]=new RainDrop() );
}

void draw() {

  background(0);

  if (isGameOver) {
    text("You caught " +dropsCaught+ " raindrops!", width>>1, 100);
    text( dropsCaught >= dropsMissed ?
    "YOU WIN!" : "YOU LOSE!", width>>1, height>>1);
    noLoop();
    return;
  }

  for (RainDrop rd: rain) {

    if ( (int) (millis()/1000) == rd.timeToDisplay )
      rd.makeRain();

    if (rd.isFalling) {
      rd.display();
      rd.fall();
    }

    if (abs(rd.locationY - buck.locationY + Bucket.buckW) <=5 &&
      abs(rd.locationX - buck.locationX) <= Bucket.buckW) {
      rd.caught();
      dropsCaught++;
    }

    else if (rd.locationY > height) {
      rd.miss();
      dropsMissed++;
    }
  }

  if (right)        buck.moveRight();
  else if (left)    buck.moveLeft();

  buck.display();

  fill(255);
  text("Raindrops caught: " + dropsCaught, width>>1, 15);

  if (dropsMissed + dropsCaught == numRainDrop)
    isGameOver = true;
}

void keyPressed() {

  if (keyCode==RIGHT | keyCode=='D')        right = true;
  else if (keyCode==LEFT | keyCode=='A')    left  = true;
}

void keyReleased() {

  if (keyCode==RIGHT | keyCode=='D')        right = false;
  else if (keyCode==LEFT | keyCode=='A')    left  = false;
}

class Bucket {
  static final color buckCol = #50A050;
  static final byte speedChange = 3;
  static final byte buckW = 35;
  static final byte buckH = 10;

  int locationX = width>>1;
  int locationY = height - buckH*2;

  void moveRight() {
    if ( (locationX += speedChange) > width-buckW )
      locationX = width-buckW;
  }

  void moveLeft() {
    if ( (locationX -= speedChange) < buckW )
      locationX = buckW;
  }

  void display() {
    fill(buckCol);

    quad(locationX - buckW, locationY - buckW, 
    locationX - buckW + 5, locationY + buckH, 
    locationX + buckW - 5, locationY + buckH, 
    locationX + buckW, locationY - buckW);
  }
}

class RainDrop {
  static final color dropCol = #54A2E0;
  static final byte dropSize = 10, dropSz = dropSize>>1;
  static final byte duration = 60;

  final int timeToDisplay = (int) random(2, duration);
  final int fallingSpeed  = (int) random(2, 5);

  boolean isFalling;

  int locationX = (int) random(dropSz, width-dropSz);
  short locationY = dropSz;

  void display() {       
    fill(dropCol);
    ellipse(locationX, locationY, dropSize, dropSize);
    triangle(locationX - dropSz, locationY, 
    locationX, locationY - dropSize, 
    locationX + dropSz, locationY);
  }

  void fall() {
    if (isFalling)    locationY += fallingSpeed;
  }

  void makeRain() {
    isFalling = true;
  }

  void caught() {
    isFalling = false;
    locationX = 9999;
  }

  void miss() {
    isFalling = false;
    locationY = -100;
  }
}