Giter VIP home page Giter VIP logo

tinyturtledirections's Introduction

Unit 8 Project 1 || Tiny Turtle Directions

Imgur

Tiny Turtle is a fun Computer Science activity created by a ScriptEd volunteer that teaches students how to use JavaScript functions. The original documentation can be found here.

#Directions for Set Up Step 1: New Repo
Create a new GitHub repo called TinyTurtle.


**Step 2: Clone** Clone your new repo into a Cloud 9 workspace with a similar name.
**Step 3: New Files** Create four new files in this workspace. Name these new files...
  • index.html
  • style.css
  • script.js
  • tinyTurtle.js

**Step 4: index.html** Copy and paste the code below into your index.html file
<!DOCTYPE html>
<html>
<head>
  
  <title>Tiny Turtle</title>
  <link rel="stylesheet" type="text/css" href="style.css">
  
</head>
<body>
    <h1>Tiny Turtle</h1>
  <canvas></canvas>
  
  <script src="tinyTurtle.js"></script>
  <script src="script.js"></script>
</body>
</html>

Step 5: tinyTurtle.js
Copy and paste the code below into your tinyTurtle.js file

// tinyTurtle.js
// 2013-10-11
// Public Domain.
// For more information, see http://github.com/toolness/tiny-turtle.

function TinyTurtle(canvas) {
  canvas = canvas || document.querySelector('canvas');

  var self = this;
  var rotation = 90;
  var position = {
    // See http://diveintohtml5.info/canvas.html#pixel-madness for
    // details on why we're offsetting by 0.5.
    x: canvas.width / 2 + 0.5,
    y: canvas.height / 2 + 0.5
  };
  var isPenDown = true;
  var radians = function(r) {return 2 * Math.PI * (r / 360) };
  var triangle = function(ctx, base, height) {
    ctx.beginPath(); ctx.moveTo(0, -base / 2); ctx.lineTo(height, 0);
    ctx.lineTo(0, base / 2); ctx.closePath();
  };
  var rotate = function(deg) {
    rotation = (rotation + deg) % 360;
    if (rotation < 0) rotation += 360;
  };

  self.penStyle = 'black';
  self.penWidth = 1;
  self.penUp = function() { isPenDown = false; return self; };
  self.penDown = function() { isPenDown = true; return self; };
  self.forward = self.fd = function(distance) {
    var origX = position.x, origY = position.y;
    position.x += Math.cos(radians(rotation)) * distance;
    position.y -= Math.sin(radians(rotation)) * distance;
    if (!isPenDown) return;
    var ctx = canvas.getContext('2d');
    ctx.strokeStyle = self.penStyle;
    ctx.lineWidth = self.penWidth;
    ctx.beginPath();
    ctx.moveTo(origX, origY);
    ctx.lineTo(position.x, position.y);
    ctx.stroke();
    return self;
  };
  self.stamp = function(size) {
    var ctx = canvas.getContext('2d');
    ctx.save();
    ctx.strokeStyle = ctx.fillStyle = self.penStyle;
    ctx.lineWidth = self.penWidth;
    ctx.translate(position.x, position.y);
    ctx.rotate(-radians(rotation));
    triangle(ctx, size || 10, (size || 10) * 1.5);
    isPenDown ? ctx.fill() : ctx.stroke();
    ctx.restore();
    return self;
  };
  self.left = self.lt = function(deg) { rotate(deg); return self; };
  self.right = self.rt = function(deg) { rotate(-deg); return self; };

  Object.defineProperties(self, {
    canvas: {get: function() { return canvas; }},
    rotation: {get: function() { return rotation; }},
    position: {get: function() { return {x: position.x, y: position.y}; }},
    pen: {get: function() { return isPenDown ? 'down' : 'up'; }}
  });

  return self;
}

Step 6: Canvas CSS
You will notice a new HTML tag is being used. This tag is called canvas In the style.css page give your canvas tag the following attributes

  • a width of 400px
  • a height of 400px
  • a border (any style)

**Step 7: JavaScript Set Up** In the `script.js` page paste the code below on line 1
	TinyTurtle.apply(window); 

Step 8: Play Turtle
Tiny Turtle understands the following commands:

  • forward();
  • right();
  • left();
  • stamp(); <------ This shows which direction the turtle is pointing.

Use the commands above to make Tiny Turtle travel in a Square.

Step 9: House
For the final piece of this project create a house (square with a triangle ontop)

tinyturtledirections's People

Contributors

ibrand avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.