LibrariesNow we'll install the libraries that we need to use Turtle graphics on the Circuit Playground Bluefruit with TFT Gizmo. Click this link to go to the circuitpython.org Libraries page. Download the latest version of the Bundle library .zip file that matches the version of CircuitPython you're using on the board. Uncompress the .zip file and then copy the following directory and three library .mpy files to the lib directory of the CIRCUITPY drive:
|
The Mu Editor
Adafruit recommends using the free program Mu to edit your CircuitPython programs and save them on your Circuit Playground Bluefruit. You can use any text editor, but Mu has some handy features.
See this page on the Circuit Playground Bluefruit guide on the steps used to install Mu.
Turtle Graphics Code
Let's start of with a simple bit of code. It will draw a square on the screen.
Copy the code below and then paste it into a new document in Mu. Save it to the CIRCUITPY drive as code.py.
#Turtle Gizmo Square #==| Turtle Gizmo Setup start |======================================== import board import busio import displayio from adafruit_st7789 import ST7789 from adafruit_turtle import turtle displayio.release_displays() spi = busio.SPI(board.SCL, MOSI=board.SDA) display_bus = displayio.FourWire(spi, command=board.TX, chip_select=board.RX) display = ST7789(display_bus, width=240, height=240, rowstart=80, backlight_pin=board.A3, rotation=180) turtle = turtle(display) #==| Turtle Gizmo Setup end |========================================= turtle.pendown() #top turtle.forward(80) turtle.right(90) #right turtle.forward(80) turtle.right(90) #bottom turtle.forward(80) turtle.right(90) #left turtle.forward(80) turtle.right(90) while True: pass
SquareAfter the setup code imports libraries and configures some pins for the display, the main body of the code runs. Watch the screen on your TFT Gizmo as the board restarts and runs the code. First, it sets the pen down with the The turtle starts out at the origin point -- Next, the turtle turns right using the This same pair of commands -- |
Since there are some repetitive steps in there, we can tidy up the code by iterating through a loop four times like this:
#Turtle Gizmo Square Loop #==| Turtle Gizmo Setup start |======================================== import board import busio import displayio from adafruit_st7789 import ST7789 from adafruit_turtle import turtle displayio.release_displays() spi = busio.SPI(board.SCL, MOSI=board.SDA) display_bus = displayio.FourWire(spi, command=board.TX, chip_select=board.RX) display = ST7789(display_bus, width=240, height=240, rowstart=80, backlight_pin=board.A3, rotation=180) turtle = turtle(display) #==| Turtle Gizmo Setup end |========================================= turtle.pendown() for _ in range(4): turtle.forward(80) turtle.right(90) while True: pass
Try out that code -- it's much shorter, but you'll see the same square drawn on the screen!
AsteriskNow that we can loop through a command many times, let's add another two commands -- We can move forward and backward to create a line, shift the angle 18 degrees to the left, and do it all again. After twenty iterations of this we have a nice star pattern! With a small change to the code (see the comment on line 18) we can turn this into an iris pattern! |
#Turtle Gizmo Asterix #==| Turtle Gizmo Setup start |======================================== import board import busio import displayio from adafruit_st7789 import ST7789 from adafruit_turtle import turtle displayio.release_displays() spi = busio.SPI(board.SCL, MOSI=board.SDA) display_bus = displayio.FourWire(spi, command=board.TX, chip_select=board.RX) display = ST7789(display_bus, width=240, height=240, rowstart=80, backlight_pin=board.A3, rotation=180) turtle = turtle(display) #==| Turtle Gizmo Setup end |========================================= turtle.pendown() for _ in range(20): turtle.forward(80) turtle.back(80) #try changing this to 70 for an iris effect turtle.left(18) while True: pass
CircleThe built-in command Try out the example here, and then us some different numbers for the circle radius instead of the 118 here. |
# Turtle Gizmo Circle #==| Turtle Gizmo Setup start |======================================== import board import busio import displayio from adafruit_st7789 import ST7789 from adafruit_turtle import turtle displayio.release_displays() spi = busio.SPI(board.SCL, MOSI=board.SDA) display_bus = displayio.FourWire(spi, command=board.TX, chip_select=board.RX) display = ST7789(display_bus, width=240, height=240, rowstart=80, backlight_pin=board.A3, rotation=180) turtle = turtle(display) #==| Turtle Gizmo Setup end |========================================= turtle.penup() turtle.right(90) turtle.forward(118) turtle.left(90) turtle.pendown() turtle.circle(118) #radius of the circle while True: pass
Circle PetalsLet's get a little fancy. We will now introduce a few more techniques, including the This will allow us to change the size of a series of five circles, and repeat the pattern four times. |
# Turtle Circle Petals #==| Turtle Gizmo Setup start |======================================== import board import busio import displayio from adafruit_st7789 import ST7789 from adafruit_turtle import Color, turtle displayio.release_displays() spi = busio.SPI(board.SCL, MOSI=board.SDA) display_bus = displayio.FourWire(spi, command=board.TX, chip_select=board.RX) display = ST7789(display_bus, width=240, height=240, rowstart=80, backlight_pin=board.A3, rotation=180) turtle = turtle(display) #==| Turtle Gizmo Setup end |========================================= colors = [Color.YELLOW, Color.GREEN] for _ in range(4): for i in range (5): turtle.pencolor(colors[i % 2]) turtle.pendown() turtle.circle(60 - (i*10) ) turtle.penup() turtle.right(90) while True: pass
StarHere's a super stylish, 1970's inspired star! Note how the length of each forward command is increased as the value of |
# Turtle Gizmo Star Fancy #==| Turtle Gizmo Setup start |======================================== import board import busio import displayio from adafruit_st7789 import ST7789 from adafruit_turtle import Color, turtle displayio.release_displays() spi = busio.SPI(board.SCL, MOSI=board.SDA) display_bus = displayio.FourWire(spi, command=board.TX, chip_select=board.RX) display = ST7789(display_bus, width=240, height=240, rowstart=80, backlight_pin=board.A3, rotation=180) turtle = turtle(display) #==| Turtle Gizmo Setup end |========================================= turtle.pendown() turtle.pencolor(Color.BLUE) for i in range(26): turtle.fd(i*10) turtle.rt(144) while True: pass
Rainbow BenzeneHere's a classic, beautiful design. This hexagonal shape twists with each iteration, and every side of each perceived hexagon is a different color of the rainbow! |
# Turtle Gizmo Rainbow Benzene #==| Turtle Gizmo Setup start |======================================== import board import busio import displayio from adafruit_st7789 import ST7789 from adafruit_turtle import Color, turtle displayio.release_displays() spi = busio.SPI(board.SCL, MOSI=board.SDA) display_bus = displayio.FourWire(spi, command=board.TX, chip_select=board.RX) display = ST7789(display_bus, width=240, height=240, rowstart=80, backlight_pin=board.A3, rotation=180) turtle = turtle(display) #==| Turtle Gizmo Setup end |========================================= benzsize = min(display.width, display.height) * 0.5 print("Turtle time! Lets draw a rainbow benzene") colors = (Color.RED, Color.ORANGE, Color.YELLOW, Color.GREEN, Color.BLUE, Color.PURPLE) turtle.pendown() start = turtle.pos() for x in range(benzsize): turtle.pencolor(colors[x%6]) turtle.forward(x) turtle.left(59) while True: pass
ParabolasThis lovely parabolic curve set creates the illusion of curvature. It is in fact made up entirely of straight line segments, just as you would with a piece of string art and some pins pushed into a canvas. It also introduces the |
# Turtle Gizmo Parabolic Jack #==| Turtle Gizmo Setup start |======================================== import board import busio import displayio from adafruit_st7789 import ST7789 from adafruit_turtle import Color, turtle displayio.release_displays() spi = busio.SPI(board.SCL, MOSI=board.SDA) display_bus = displayio.FourWire(spi, command=board.TX, chip_select=board.RX) display = ST7789(display_bus, width=240, height=240, rowstart=80, backlight_pin=board.A3, rotation=180) turtle = turtle(display) #==| Turtle Gizmo Setup end |========================================= print("Draw parabolas using straight line segments!") def vert(x, y, size): turtle.goto(x, y) turtle.dot(size) turtle.penup() turtle.pencolor(Color.GREEN) vert(0, 0, 7) vert(0, 100, 7) vert(100, 0, 7) vert(0, -100, 7) vert(-100, 0, 7) x_quad=[10, 10, -10, -10] y_quad=[10, -10, -10, 10] for q in range(4): for i in range(0,11): x_from = 0 y_from = (10-i) * y_quad[q] x_to = i * x_quad[q] y_to = 0 turtle.penup() turtle.goto(x_from,y_from) turtle.pendown() turtle.goto(x_to,y_to) turtle.home() while True: pass
Sierpinski TriangleTime to get fractal! These are recursive, equilateral triangles as described by Waclaw Sierpinski in 1915! Read more about them here. |
# Turtle Gizmo Sierpinski Triangle #==| Turtle Gizmo Setup start |======================================== import board import busio import displayio from adafruit_st7789 import ST7789 from adafruit_turtle import turtle displayio.release_displays() spi = busio.SPI(board.SCL, MOSI=board.SDA) display_bus = displayio.FourWire(spi, command=board.TX, chip_select=board.RX) display = ST7789(display_bus, width=240, height=240, rowstart=80, backlight_pin=board.A3, rotation=180) turtle = turtle(display) #==| Turtle Gizmo Setup end |========================================= def getMid(p1, p2): return ((p1[0] + p2[0]) / 2, (p1[1] + p2[1]) / 2) #find midpoint def triangle(points, depth): turtle.penup() turtle.goto(points[0][0], points[0][1]) turtle.pendown() turtle.goto(points[1][0], points[1][1]) turtle.goto(points[2][0], points[2][1]) turtle.goto(points[0][0], points[0][1]) if depth > 0: triangle([points[0], getMid(points[0], points[1]), getMid(points[0], points[2])], depth-1) triangle([points[1], getMid(points[0], points[1]), getMid(points[1], points[2])], depth-1) triangle([points[2], getMid(points[2], points[1]), getMid(points[0], points[2])], depth-1) big = min(display.width/2, display.height/2) little = big / 1.4 seed_points = [[-big, -little], [0, big], [big, -little]] #size of triangle triangle(seed_points, 4) while True: pass
Hilbert CurveHere's another interesting fractal pattern, called a Hilbert curve. |
# Turtle Gizmo Hilbert #==| Turtle Gizmo Setup start |======================================== import board import busio import displayio from adafruit_st7789 import ST7789 from adafruit_turtle import Color, turtle displayio.release_displays() spi = busio.SPI(board.SCL, MOSI=board.SDA) display_bus = displayio.FourWire(spi, command=board.TX, chip_select=board.RX) display = ST7789(display_bus, width=240, height=240, rowstart=80, backlight_pin=board.A3, rotation=180) turtle = turtle(display) #==| Turtle Gizmo Setup end |========================================= def hilbert2(step, rule, angle, depth, t): if depth > 0: a = lambda: hilbert2(step, "a", angle, depth - 1, t) b = lambda: hilbert2(step, "b", angle, depth - 1, t) left = lambda: t.left(angle) right = lambda: t.right(angle) forward = lambda: t.forward(step) if rule == "a": left() b() forward() right() a() forward() a() right() forward() b() left() if rule == "b": right() a() forward() left() b() forward() b() left() forward() a() right() turtle.penup() turtle.goto(-108, -108) turtle.pendown() turtle.pencolor(Color.PURPLE) hilbert2(7, "a", 90, 5, turtle) while True: pass
Koch SnowflakeThis is a third generation fractal Koch snowflake. |
# Turtle Gizmo Koch Snowflake #==| Turtle Gizmo Setup start |======================================== import board import busio import displayio from adafruit_st7789 import ST7789 from adafruit_turtle import turtle displayio.release_displays() spi = busio.SPI(board.SCL, MOSI=board.SDA) display_bus = displayio.FourWire(spi, command=board.TX, chip_select=board.RX) display = ST7789(display_bus, width=240, height=240, rowstart=80, backlight_pin=board.A3, rotation=180) turtle = turtle(display) #==| Turtle Gizmo Setup end |========================================= def f(side_length, depth, generation): if depth == 0: side = turtle.forward(side_length) else: side = lambda: f(side_length / 3, depth - 1, generation + 1) side() turtle.left(60) side() turtle.right(120) side() turtle.left(60) side() turtle.penup() turtle.goto(-99, 56) turtle.pendown() num_generations = 3 top_side = lambda: f(218, num_generations, 0) top_side() turtle.right(120) top_side() turtle.right(120) top_side() while True: pass
Christmas TreeThis lovely Christmas tree was adapted from this program written by Keith Randall. |
# Turtle Gizmo Christmas Tree #==| Turtle Gizmo Setup start |======================================== import board import busio import displayio from adafruit_st7789 import ST7789 from adafruit_turtle import Color, turtle displayio.release_displays() spi = busio.SPI(board.SCL, MOSI=board.SDA) display_bus = displayio.FourWire(spi, command=board.TX, chip_select=board.RX) display = ST7789(display_bus, width=240, height=240, rowstart=80, backlight_pin=board.A3, rotation=180) turtle = turtle(display) #==| Turtle Gizmo Setup end |========================================= # Fractal Christmas Tree: # https://codegolf.stackexchange.com/questions/15860/make-a-scalable-christmas-tree # by Keith Randall n = 42 # input value for scaling the tree. note: ornaments don't scale turtle.goto(0, -20) #star turtle.left(90) turtle.forward(3*n) turtle.pencolor(Color.YELLOW) turtle.left(126) turtle.pendown() for _ in range(5): turtle.forward(n/5) turtle.right(144) turtle.forward(n/5) turtle.left(72) turtle.right(126) #tree turtle.pencolor(Color.GREEN) turtle.back(n*4.8) def tree(d,s): if d <= 0: return turtle.forward(s) tree(d-1, s*.8) turtle.right(120) tree(d-3, s*.5) turtle.right(120) tree(d-3, s*.5) turtle.right(120) turtle.back(s) turtle.pendown() turtle.pencolor(Color.GREEN) tree(15, n) turtle.back(n/2) #ornaments def ornament(x, y): turtle.penup() turtle.goto(x, y) turtle.pencolor(Color.RED) turtle.pendown() turtle.dot(7) turtle.penup() orn_pnts=[ (5, 60), (-7, 40), (10, 20), (-15, 0), (25, -20), (-27, -30), (7, -33), (40, -60), (-9, -63), (-50, -88), (62, -97) ] for j in range(len(orn_pnts)): ornament(orn_pnts[j][0], orn_pnts[j][1]) turtle.penup() turtle.goto(0, -120) while True: pass
Snowflakes
This example will draw lots of beautiful snowflake-style patterns! If you want to get technical about the number of sides in your flakes, make some adjustments to this line:
draw_flake(randint(5, 8)) #adjust number of arms here
# Turtle Gizmo Snowflakes #==| Turtle Gizmo Setup start |======================================== import time from random import randint import board import busio import displayio from adafruit_st7789 import ST7789 from adafruit_turtle import turtle displayio.release_displays() spi = busio.SPI(board.SCL, MOSI=board.SDA) display_bus = displayio.FourWire(spi, command=board.TX, chip_select=board.RX) display = ST7789(display_bus, width=240, height=240, rowstart=80, backlight_pin=board.A3, rotation=180) turtle = turtle(display) #==| Turtle Gizmo Setup end |========================================= def draw_arm(): turtle.pendown() for angle, length in arm_data: turtle.forward(length) turtle.left(angle) turtle.forward(length) turtle.backward(length) turtle.right(2*angle) turtle.forward(length) turtle.backward(length) turtle.left(angle) turtle.penup() def draw_flake(arms): turtle.penup() turtle.home() turtle.clear() angle = 0 delta_angle = 360 // arms for _ in range(arms): turtle.home() turtle.setheading(angle) draw_arm() angle += delta_angle turtle.penup() turtle.home() while True: arm_data = [(randint(30, 80), randint(10, 40)) for _ in range(5)] draw_flake(randint(5, 8)) #adjust number of arms here time.sleep(5)
Where to Find More
You can find much more information about turtle graphics as well as example scripts through an Internet search of "turtle python graphics examples" or some variation. Since turtle graphics are mainly in terms of moving and turning they can usually be converted to CircuitPython and the adafruit_turtle library.
Check out these example sites: