2009
10.05

I Made A Turtle!

I was trying to make a demonstration of how to do a Koch curve as an l-system with turtle graphics, but my computer would run out of memory when I set it to 3 iterations, because I would rewrite the rule back into the original string that contained the “what’s yr turtle gonna do” information. Maybe another day…

But anyways, I made a Turtle. Here it is doing a random forward/backward walk (+/- 10 pixels) and turning left one degree for every frame. It’s oddly compelling to watch.

Oh hey, if your turtle wanders off the screen (unlikely in the short term, inevitable in the long term, sort of like death…) then just click on the applet to reset him back to the middle of the screen.

Also, thanks to my Mom for getting me a “Programming with LOGO book” when I was still a toddler. Apparently, those lessons weren’t lost on me…

<br /> You need java for this to work.. :-/<br />

Turtle t;
class Turtle {
  float x, y, ang;
  boolean down;   
 
  Turtle(float _x, float _y, float _ang) { 
    x = _x;
    y = _y;
    ang = _ang;
    down = false;
  }
 
  void penDown() { 
    down = true;
  }
 
  void penUp() { 
    down = false;
  }
 
  void forward(float len) { 
    float nx = x+cos(radians(ang))*len;
    float ny = y+sin(radians(ang))*len;
    if (down) { 
      line(x,y,nx,ny);
    }
    x = nx;
    y = ny;
  } 
 
  void turnLeft(float _ang) { 
    ang -= _ang;
  }
 
  void turnRight(float _ang) { 
    ang += _ang;
  }
}
 
void setup() { 
    background(10);
    stroke(120);
    smooth();
    size(600,600);
    frameRate(30);
    t = new Turtle(300,300,0);
}
 
 
void draw() {   
    if (mousePressed) { 
        t = new Turtle(300,300,0);
    }
    t.penDown();
    t.forward(random(-10,10));
    t.turnLeft(1);
}
Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay

2 comments so far

Add Your Comment
  1. Oh geeze…. I’d forgotten all about that turtle graphics book. Didn’t that run on a VIC 20 with 4K of RAM?

  2. I don’t remember if it ran on anything. I just remember the book. I believe it had a cartoon turtle. That confused me for a while.

    However, it would have been about when we had the Vic-20 and I was tearing it up on Snack-Man.