Turtle examples : Draw a line

Introduction :

My very first introduction to programming was in middle school during the first week of class. My teacher explained that we’d be learning a programming language called “Logo”, which would let you control an imaginary “turtle” with a pen attached to its tail to draw pictures and packets.

We were then given a thick packet of examples, and was set free to experiment and do whatever we liked as long as we completed a certain number of exercises each day.

I’m not sure how much I ended up actually learning, since I was mostly blindly copying and tweaking code, but I do remember to this day having a huge amount of fun playing with Logo.

So naturally, I was very pleased to discover that Python, one of my favorite programming languages, came built-in with a module named “turtle” that was very similar to Logo. This document is an attempt to mirror some of the spirit of the original packet of examples that first introduced me to programming.

Note: I started this document a year or so ago, but never actually got around to completing or updating it beyond a few examples. Over the course of the next few months, I’ll be sporadically adding more examples as I find time.

Draw a line :

One of the simplest things you can do using the turtle module is to draw a line. There are always four steps you need to do in order to use the turtle module:

  1. Import the turtle module. If we skip this step, there’ll be no turtle to control.
  2. Create a turtle to control.
  3. Draw things. Do stuff. This will also automatically create the screen.
  4. Run turtle.done(). (NOT bob.done()!)

Notice that turtle.done() will pause the program. You’ll need to close the window in order to continue.



# Step 1: Make all the "turtle" commands available to us.
import turtle

# Step 2: Create a new turtle. We'll call it "bob"
bob = turtle.Turtle()

# Step 3: Move in the direction Bob's facing for 50 pixels
bob.forward(50)

# Step 4: We're done!
turtle.done()
 
javaranjeet.com

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top