Monday, March 15, 2010

Implement time

float[] x;
float[] y;
int LEN = 19;
float direction = 1;
float direction_d = 0;
float speed = 1;
int xMax = 32;
int yMax = 32;
float yScale;
float xScale;

int CIRCLE = 0;
int LINE = 1;
int time = LINE;

void setup() {
  smooth();
  size(300,300);
  x = new float[LEN];
  y = new float[LEN];
  background(255);
  for (int i = 0; i < LEN; ++i) {
    x[i] = 0;
    y[i] = 0;
  }
  xScale = (float) width / (float) xMax;
  yScale = (float) height / (float) yMax;
  frameRate(10);
}

void draw() {
  //noStroke();
  // clear tail
  fill(255);
  ellipse(x[LEN-1] * xScale, y[LEN-1] * yScale, xScale, yScale);  

  // shift body (can't be arsed with ring buffer)
  for (int i = LEN-1; i > 0; --i) {
    x[i] = x[i-1];
    y[i] = y[i-1];
  }

  // move head
  float hyp = speed;
  float opp = sin(direction) * hyp;
  float adj = cos(direction) * hyp;
  x[0] += opp;
  y[0] += adj;

  // connect edges
  x[0] %= xMax;
  y[0] %= yMax;

  if (x[0] < 0) {
    x[0] = xMax;
  }
  if (y[0] < 0) {
    y[0] = yMax;
  }

  // draw head
  fill(255,0,255);
  ellipse(x[0] * xScale, y[0] * yScale, xScale, yScale);  

  // draw first segment
  fill(128);
  ellipse(x[1] * xScale, y[1] * yScale, xScale, yScale);  

  if (random(1) > 0.95) {
    change_time();
  }
  direction += direction_d;
}

void change_time() {
  time = time == 0 ? 1 : 0;
  if (time == CIRCLE) {
    direction_d = 0.5 - random(1);
  }
  else {
    direction_d = 0;
  }
}

info info

submitted by: lxyxpx
views: 

First as a line, then as a circle. http://instructionset.org/instruction/17/

treeThis sketch has a parent
Tags: snake, worm

comments comment

loading loading...

 

Add a comment: