> show canvas only <


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

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



/**
 * Player Move (v1.2.1)
 * by GoToLoop (2014/Dec/07)
 *
 * Forum.Processing.org/two/discussion/8511/movement-code-messed-up#Item_6
 * Studio.ProcessingTogether.com/sp/pad/export/ro.91tcpPtI9LrXp
 */

static final int DIAM = 80, SPD = 4, FPS = 60;
static final color BG = 0350;

Player p;

void setup() {
  size(800, 600);

  smooth(3);
  frameRate(FPS);
  ellipseMode(CENTER);

  fill(Player.INK);
  stroke(Player.OUTLINE);
  strokeWeight(Player.BOLD);

  p = new Player(width, height, DIAM, SPD);
}

void draw() {
  background(BG);
  p.move();
  p.display();
}

void keyPressed() {
  p.setMove(keyCode, true);
}

void keyReleased() {
  p.setMove(keyCode, false);
}

final class Player {
  static final color INK = #008000, OUTLINE = 0;
  static final float BOLD = 2.0;

  boolean isLeft, isRight, isUp, isDown;
  int x, y;
  final int d, v;

  Player(final int xx, final int yy, final int dd, final int vv) {
    x = xx;
    y = yy;
    d = dd;
    v = vv;
  }

  void display() {
    ellipse(x, y, d, d);
  }

  void move() {
    final int r = d >> 1;
    x = constrain(x + v*(int(isRight) - int(isLeft)), r, width  - r);
    y = constrain(y + v*(int(isDown)  - int(isUp)),   r, height - r);
  }

  boolean setMove(final int k, final boolean b) {
    switch (k) {
    case +'W':
    case UP:
      return isUp = b;

    case +'S':
    case DOWN:
      return isDown = b;

    case +'A':
    case LEFT:
      return isLeft = b;

    case +'D':
    case RIGHT:
      return isRight = b;

    default:
      return b;
    }
  }
}