Skip to content

Instantly share code, notes, and snippets.

@jonesfish
Last active March 21, 2019 02:57
Show Gist options
  • Save jonesfish/d5968fd03c976e82d88c to your computer and use it in GitHub Desktop.
Save jonesfish/d5968fd03c976e82d88c to your computer and use it in GitHub Desktop.

Revisions

  1. jonesfish revised this gist Oct 6, 2015. 1 changed file with 12 additions and 7 deletions.
    19 changes: 12 additions & 7 deletions state_square.pde
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,13 @@
    final int GO_RIGHT = 0;
    final int GO_DOWN = 1;
    final int GO_LEFT = 2;
    final int GO_UP = 3;

    float x, y;
    float w, h;
    float speed = 5;

    int state = 0;
    int state = GO_RIGHT;

    void setup() {
    size(500, 500);
    @@ -16,7 +21,7 @@ void draw() {

    // If the state is 0, move to the right.
    switch (state){
    case 0:
    case GO_RIGHT:
    x += speed;
    // If, while the state is 0, it reaches the right side of the window, change the state to 1
    if (x > width-w/2) {
    @@ -25,23 +30,23 @@ void draw() {
    }
    break;

    case 1:
    case GO_DOWN:
    y += speed;
    if (y > height-h/2) {
    y = height-h/2;
    state = 2;
    }
    break;

    case 2:
    case GO_LEFT:
    x -= speed;
    if (x < w/2) {
    x = w/2;
    state = 3;
    }
    break;

    case 3:
    case GO_UP:
    y -= speed;
    if (y < h/2) {
    y = h/2;
    @@ -53,8 +58,8 @@ void draw() {
    background(0);
    // UFO
    fill(151,37,210);
    ellipse(x, y, 50, 15);
    ellipse(x, y, w, h/2);
    fill(186,0,255);
    stroke(255);
    arc(x, y, 25, 25, PI, TWO_PI);
    arc(x, y, h*4/5, h*4/5, PI, TWO_PI);
    }
  2. jonesfish renamed this gist Oct 6, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. jonesfish created this gist Oct 6, 2015.
    60 changes: 60 additions & 0 deletions state_square.de
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,60 @@
    float x, y;
    float w, h;
    float speed = 5;

    int state = 0;

    void setup() {
    size(500, 500);
    w = 50;
    h = 30;
    x = w/2;
    y = h/2;
    }

    void draw() {

    // If the state is 0, move to the right.
    switch (state){
    case 0:
    x += speed;
    // If, while the state is 0, it reaches the right side of the window, change the state to 1
    if (x > width-w/2) {
    x = width-w/2;
    state = 1;
    }
    break;

    case 1:
    y += speed;
    if (y > height-h/2) {
    y = height-h/2;
    state = 2;
    }
    break;

    case 2:
    x -= speed;
    if (x < w/2) {
    x = w/2;
    state = 3;
    }
    break;

    case 3:
    y -= speed;
    if (y < h/2) {
    y = h/2;
    state=0;
    }
    break;
    }

    background(0);
    // UFO
    fill(151,37,210);
    ellipse(x, y, 50, 15);
    fill(186,0,255);
    stroke(255);
    arc(x, y, 25, 25, PI, TWO_PI);
    }