Turtle allows you to do basic drawing in python. Turtle was intended to be used as an introductory lesson to programming but you might find it useful if your are teaching trigonometry, algebra and other computer math subjects.
Turtle is programmed by instructing the cursor(turtle) to move forward or backward by a distance in units of pixels. You can also instruct it to turn left or right in term of degrees angle.
In the video below I’ll wall you through the basics of turtle.
Below are the source code that I have used in the demo:
Example #1. The basic turtle code
#************************Example 1: Drawing Box***************************************************************
import turtle
turtle.pendown()
input('Press enter to continue: ')
turtle.speed(0)
turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.left(90)
#***************************************************************************************
Example #2. Applying Loop to do repetitive task
#************************Example 2: Box using for control loop ***********************************************
import turtle
turtle.pendown()
input('Press enter to continue: ')
turtle.speed(1)
for i in range (4): # repeat below indented commands 4x'
turtle.forward(50)
turtle.left(90)
#************************Example 2***************************************************************
Example #3. Filled Box
#************************Example 3: Filled Box***************************************************************
import turtle
turtle.pendown()
input('Press enter to continue: ')
turtle.speed(1)
turtle.color('red', 'blue')
turtle.begin_fill()
for i in range (4): # repeat below indented commands 4x'
turtle.forward(50)
turtle.left(90)
turtle.end_fill()
#************************Example 3***************************************************************
Example 4.Building basic of Fractals by introducing variables.
#************************Example 4: fractal basic*************************************************************** import turtle turtle.pendown() input('Press enter to continue: ') turtle.speed(1) x=0 for i in range (20): # repeat below indented commands 4x' turtle.forward(50+x) turtle.left(90) x=x+5 #************************Example 4***************************************************************
Example 5. Solving Basic Trigonometry
#************************Example 5*************************************************************** ##prove that 95 and 85 are suplementary angle. Angle when add up together forms 180 degress import turtle turtle.pendown() input('Press enter to continue: ') turtle.speed(1) turtle.speed(1) turtle.forward(100) #this is angle 0 turtle.backward(100) turtle.left(95) #this is angel 75 turtle.forward(200) turtle.backward(200) turtle.left(85) #adding 285+75 = 360 degrees turn turtle.forward(200) #************************Example 5***************************************************************
Example 6. Drawing Symetrical Objects
#Sourcecode from turtle documentaion: https://docs.python.org/3.3/library/turtle.html?highlight=turtle from turtle import * color('red', 'yellow') pendown() input('Press enter to continue: ') speed(0) begin_fill() while True: forward(200) left(47) if abs(pos()) < 1: break end_fill() done()
Example 7. Basic Fractals
##********************************************************************************************************* ## example 7 reference https://repl.it/@lucia0101/Looping-Circles-with-PythonTurtle import turtle # You must include this line for any turtle commands to work import random turtle.pendown() input('Press enter to continue: ') colors = ["red","green","blue","orange","purple","pink","yellow","black"] turtle.speed(0) # Sets the drawing speed. 1 is slowest, 10 highest. 0 will draw eveything without seeing the turtle move. x=0 # This loop will draw 8 circles in a circle. for i in range (200): # This line means 'do all these lines of code 8 times' color = random.choice(colors) #Choose a random color turtle.color(color) turtle.circle(30+x) # This line creates a circle with a diameter of 30 turtle.penup() # This lifts the pen up so that when the turtle moves it does not draw. turtle.forward(50) # Moves the turtle forward 50 turtle.right(45) # Turns the turtle 45 degrees right turtle.pendown() # Puts the pen back down ready to draw the next time we go through the loop x=x+1 ##************************************************************************************************************
Example 8. Fractal Plant
##******************************* Fractal plant ********************************************************** ## As described in http://en.wikipedia.org/wiki/L-system#Example_7:_Fractal_plant ##source https://gist.github.com/HansNewbie/9808789 import turtle turtle.pendown() input('Press enter to continue: ') turtle.speed(0) ruleInput = ['F', 'X'] ruleOutput = ["FF", "F-[[X]+X]+F[+FX]-X"] start = "X" front = 5 turn = 30 stack = [] dirstack = [] turtle.left(90) turtle.penup() turtle.setpos(0, -350) turtle.pendown() turtle.shape("turtle") def generate(iteration): result = start temp = "" for i in range(iteration): for j in range(len(result)): for k in range(len(ruleInput)): if (result[j] == ruleInput[k]): temp += ruleOutput[k] break if (k == len(ruleInput)-1): temp += result[j] result = temp temp = "" return result def draw(input): for x in input: if (x == 'F'): turtle.forward(front) elif (x == '-'): turtle.left(turn) elif (x == '+'): turtle.right(turn) elif (x == '['): stack.append(turtle.pos()) dirstack.append(turtle.heading()) elif (x == ']'): turtle.penup() post = stack.pop() direc = dirstack.pop() turtle.setpos(post) turtle.setheading(direc) turtle.pendown() turtle.hideturtle() turtle.done() draw(generate(6))