Turtle examples : Jumping around and changing speed

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.

Jumping around and changing speed :

turtle.setposition(x, y) will set the turtle’s position to the coordinates you plug in. (0, 0) is located at the center of the screen – where the turtle first started. Note that you need to make sure the turtle’s pen is up, otherwise it’ll draw a line back to that.

You can change the speed of the turtle by doing turtle.speed(number). If you set the speed to 10, the turtle will go really fast. If you set the speed to 1, the turtle will go really slow (which is useful for trying to understand how some complicated thing is being drawn). If you set the speed to zero, however, the turtle will go at warpspeed and will draw as fast as it can.

The speed cannot be lesser then 0 or greater then 10.



import turtle 

ninja = turtle.Turtle()

ninja.speed(10)

for i in range(180):
    ninja.forward(100)
    ninja.right(30)
    ninja.forward(20)
    ninja.left(60)
    ninja.forward(50)
    ninja.right(30)
    
    ninja.penup()
    ninja.setposition(0, 0)
    ninja.pendown()
    
    ninja.right(2)
    
turtle.done()
 
javaranjeet.com

Leave a Comment

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

Scroll to Top