Turtle Graphics on ESP32-S3-EYE/CircuitPython

In Python, turtle graphics provides a representation of a physical “turtle” (a little robot with a pen) that draws on a sheet of paper on the floor.

It’s an effective and well-proven way for learners to encounter programming concepts and interaction with software, as it provides instant, visible feedback. It also provides convenient access to graphical output in general.

py_turtle.py is a Python 3 script run on desktop to draw Rotated Squares using Turtle Graphics. (code copied from https://deborahrfowler.com/MathForVSFX/RotatedSquares.html)

# Turtle Exercise run on Desktop/Python 3

import turtle

print("Turtle Exercise run on Desktop/Python 3")

def drawSquare(t,size,angle):
    t.left(angle)
    halfsize = size * .5
    t.pu()
    t.setpos(0,0)
    t.backward(halfsize)
    t.left(90)
    t.backward(halfsize)
    t.right(90)
    t.pd()

    for i in range(0,4):
        t.fd(size)
        t.left(90)


def drawSquarePattern(t):
    t.color("black")
    drawSquare(t,240,0)
    for i in range(0,20):
        size = 240 * pow(1/1.2,i+1)
        drawSquare(t,size,12.5)
    t.ht()

def main():
    turtle.setup(750,750)
    turtle.title("Rotated Squares")
    t = turtle.Pen()
    t.width(2)
    t.speed(0)
    drawSquarePattern(t)
    turtle.exitonclick()

main()


CircuitPython Turtle Graphics Library is built on top of displayio for use on display based boards.

cpyS3EYE_turtle.py, ported to run on ESP23-S3-EYE/CircuitPython 8 using adafruit_turtle.mpy library.
# Turtle Exercise run on ESP32-S3-EYE/CircuitPython
# ref: https://learn.adafruit.com/circuitpython-turtle-graphics

import board
from adafruit_turtle import Color, turtle
import os, sys

display = board.DISPLAY
turtle = turtle(board.DISPLAY)

info = os.uname()[4] + "\n" + \
       sys.implementation[0] + " " + os.uname()[3] + "\n" + \
       "board.DISPLAY: " + str(display.width) + "x" + str(display.height)
print("=======================================")
print(info)
print("=======================================")
print("Turtle Exercise run on ESP32-S3-EYE/CircuitPython")

def drawSquare(t,size,angle):
    t.left(angle)
    halfsize = size * .5
    t.pu()
    t.setpos(0,0)
    t.backward(halfsize)
    t.left(90)
    t.backward(halfsize)
    t.right(90)
    t.pd()

    for i in range(0,4):
        t.fd(size)
        t.left(90)

def drawSquarePattern(t):
    t.pencolor(Color.WHITE)
    drawSquare(t,240,0)
    for i in range(0,20):
        size = 240 * pow(1/1.2,i+1)
        drawSquare(t,size,12.5)
    t.ht()

drawSquarePattern(turtle)
print("~ bye ~")




Comments

Popular posts from this blog

MicroPython/ESP32-C3 + 1.8" 128x160 TFT ST7735 SPI, using boochow/MicroPython-ST7735 library.

CameraWebServe: ESP32-S3 (arduino-esp32) + OV5640 camera module