petercour
Posted on July 7, 2019
The turtle module lets you draw all kinds of things. It's originally made for kids, but it's fun :)
This program below draw a bunch of shapes on the window including a circle, square and triangle.
Python Turtle shapes
If you run the program you see the turtle drawing the shapes. The turtle starts walking and leaves behind a trail. Over time you see the shapes show up.
#!/usr/bin/python3
import turtle
import random
def drawshape(sides, length):
angle = 360.0 / sides
for sides in range(sides):
turtle.forward(length)
turtle.right(angle)
def moveTurtle(x, y):
turtle.penup()
turtle.goto(x,y)
turtle.pendown()
def drawsquare(length):
drawshape(4,length)
def drawtriangle(length):
drawshape(3,length)
def drawcircle(length):
drawshape(360, length)
drawcircle(1)
drawsquare(100)
drawtriangle(250)
turtle.done()
Result:
Learn Python:
💖 💪 🙅 🚩
petercour
Posted on July 7, 2019
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.