4K
A collection of simple turtle code examples
Type them in and try them out
Code
# draw star using Turtle import turtle board = turtle.Turtle() for i in range(6): board.forward(50) board.right(144) turtle.done()
import turtle board = turtle.Turtle() # first triangle for star board.forward(100) # draw base board.left(120) board.forward(100) board.left(120) board.forward(100) board.penup() board.right(150) board.forward(50) # second triangle for star board.pendown() board.right(90) board.forward(100) board.right(120) board.forward(100) board.right(120) board.forward(100) turtle.done()
# Spiral Helix Pattern using Turtle import turtle loadWindow = turtle.Screen() turtle.speed(200) for i in range(100): turtle.circle(5*i) turtle.circle(-5*i) turtle.left(i) turtle.exitonclick()
# draw hexagon using Turtle import turtle board = turtle.Turtle() sides = 6 side_length = 70 angle = 360.0 / sides for i in range(sides): board.forward(side_length) board.right(angle) turtle.done()
# draw square using Turtle import turtle board = turtle.Turtle() for i in range(4): #change the number below for bigger square board.forward(70) board.right(90) turtle.done()
import turtle board = turtle.Turtle() board.speed(0) board.color("#FF0000") side=400 board.penup() board.goto(-200,-200) #position cursor at the bootom right of the screen board.pendown() #Start Spiral for i in range (1,100): board.forward(side) board.left(90) side=side-4
# A spiral out of squares import turtle size=1 turtle.speed(200) while (True): turtle.forward(size) turtle.right(91) size = size + 2
import turtle colors = ['red', 'purple', 'blue', 'green', 'orange', 'yellow'] board = turtle.Pen() turtle.bgcolor('black') turtle.speed(200) for x in range(360): board.pencolor(colors[x%6]) board.width(x/100 + 1) board.forward(x) board.left(59)
import turtle board = turtle.Turtle() board.speed(20) board.color("blue", "red") board.begin_fill() for i in range(50): board.forward(300) board.left(170) board.end_fill() turtle.done()
Links
We have a collection of these on github – https://github.com/programmershelp/maxpython/tree/main/turtle