haewanp – Project 11 – Playing with your Turtle

Merry Christmas

var myTurtle;
var num;
var length = 20;

function setup() {
    createCanvas(480, 480);
    background(5, 60, 50);
    myTurtle = makeTurtle(width/2, height/2);
    flower(40);
}

function flower(angle) { //draw red flower
    for (j = 0; j <= 360/angle; j++) {
        myTurtle.penDown();
        myTurtle.x = width/2;
        myTurtle.y = height/2 - 20;
        myTurtle.setColor((color(255, 0, 0)));
        myTurtle.setWeight(6);
        drawhex(85);
        myTurtle.back(30);
        myTurtle.right(angle);
        myTurtle.penUp();
    }
}

function drawhex(length) { //draw hexagon which is a part of flower
    for (i = 0; i < 6; i++) {
        myTurtle.forward(length);
        myTurtle.right(60);
    }
}

function draw() {
    //snow flakes
    myTurtle.penDown();
    myTurtle.setWeight(1);
    myTurtle.setColor(color(255));
    drawhex(mouseX/70);
    myTurtle.x = random(0, width);
    myTurtle.y = random(0, height);
    myTurtle.penUp();
    
}

function turtleLeft(d) {
    this.angle -= d;
}
 
 
function turtleRight(d) {
    this.angle += d;
}
 
 
function turtleForward(p) {
    var rad = radians(this.angle);
    var newx = this.x + cos(rad) * p;
    var newy = this.y + sin(rad) * p;
    this.goto(newx, newy);
}
 
 
function turtleBack(p) {
    this.forward(-p);
}
 
 
function turtlePenDown() {
    this.penIsDown = true;
}
 
 
function turtlePenUp() {
    this.penIsDown = false;
}
 
 
function turtleGoTo(x, y) {
    if (this.penIsDown) {
      stroke(this.color);
      strokeWeight(this.weight);
      line(this.x, this.y, x, y);
    }
    this.x = x;
    this.y = y;
}
 
 
function turtleDistTo(x, y) {
    return sqrt(sq(this.x - x) + sq(this.y - y));
}
 
 
function turtleAngleTo(x, y) {
    var absAngle = degrees(atan2(y - this.y, x - this.x));
    var angle = ((absAngle - this.angle) + 360) % 360.0;
    return angle;
}
 
 
function turtleTurnToward(x, y, d) {
    var angle = this.angleTo(x, y);
    if (angle < 180) {
        this.angle += d;
    } else {
        this.angle -= d;
    }
}
 
 
function turtleSetColor(c) {
    this.color = c;
}
 
 
function turtleSetWeight(w) {
    this.weight = w;
}
 
 
function turtleFace(angle) {
    this.angle = angle;
}
 
 
function makeTurtle(tx, ty) {
    var turtle = {x: tx, y: ty,
                  angle: 45, 
                  penIsDown: true,
                  color: color(255, 0, 0),
                  weight: 1,
                  left: turtleLeft, right: turtleRight,
                  forward: turtleForward, back: turtleBack,
                  penDown: turtlePenDown, penUp: turtlePenUp,
                  goto: turtleGoTo, angleto: turtleAngleTo,
                  turnToward: turtleTurnToward,
                  distanceTo: turtleDistTo, angleTo: turtleAngleTo,
                  setColor: turtleSetColor, setWeight: turtleSetWeight,
                  face: turtleFace};
    return turtle;
}

function mousePressed() {
    //it stops snowflakes
    noLoop();
    //draw ellipse
    fill(255);
    stroke(255);
    ellipse(width/2, height/2 - 20, 110, 110);
    //redraw red flower
    myTurtle.x = width/2;
    myTurtle.y = height/2;
    flower(40);
    stroke(5, 60, 50);
    //text
    textAlign(CENTER);
    textSize(28);
    strokeWeight(5);
    textFont('Trebuchet MS');
    text('M E R R Y  C H R I S T M A S', width/2, height - 40);
    
}

I made it because Christmas is coming! I created snowflakes and Christmas flower with multiple hexagons.

Leave a Reply