Python and Turtle Difficulty Level 4,python Penrose Triangle with Python Turtle (Source Code Included)

Penrose Triangle with Python Turtle (Source Code Included)

Penrose Triangle is a famous impossible shape. Draw it with Python Turtle!

Penrose Triangle with Python Turtle

Source Code:

import turtle
turtle.speed(1)
turtle.setup(1000,1000)
turtle.title('Penrose Triangle - PythonTurtle.Academy')

def draw_penrose_triangle(x,y,size,tilt):
    turtle.pencolor((0.2,0.2,0.2))
    turtle.up()
    turtle.goto(x,y)
    turtle.seth(tilt)
    turtle.down()
    turtle.fd(size*2)
    p1x = turtle.xcor()
    p1y = turtle.ycor()
    p1a = turtle.heading()
    turtle.left(120)
    turtle.fd(size*2)
    p2x = turtle.xcor()
    p2y = turtle.ycor()
    p2a = turtle.heading()
    turtle.left(120)
    turtle.fd(size*2)
    p3x = turtle.xcor()
    p3y = turtle.ycor()
    p3a = turtle.heading()
    # gray
    turtle.up()
    turtle.goto(p1x,p1y)
    turtle.seth(p1a)
    turtle.down()
    turtle.fillcolor('dark gray')
    turtle.begin_fill()
    turtle.fd(size)
    turtle.left(120)
    turtle.fd(size*5)
    turtle.left(120)
    turtle.fd(size*6)
    turtle.left(60)
    turtle.fd(size)
    turtle.left(120)
    turtle.fd(size*5)
    turtle.right(120)
    turtle.fd(3*size)
    turtle.end_fill()
    #black
    turtle.up()
    turtle.goto(p2x,p2y)
    turtle.seth(p2a)
    turtle.down()
    turtle.fillcolor('black')
    turtle.begin_fill()
    turtle.fd(size)
    turtle.left(120)
    turtle.fd(5*size)
    turtle.left(120)
    turtle.fd(6*size)
    turtle.left(60)
    turtle.fd(size)
    turtle.left(120)
    turtle.fd(5*size)
    turtle.right(120)
    turtle.fd(3*size)
    turtle.end_fill()
    #white
    turtle.up()
    turtle.goto(p3x,p3y)
    turtle.seth(p3a)
    turtle.down()
    turtle.fd(size)
    turtle.left(120)
    turtle.fd(5*size)
    turtle.left(120)
    turtle.fd(6*size)
    turtle.left(60)
    turtle.fd(size)
    turtle.left(120)
    turtle.fd(5*size)
    turtle.right(120)
    turtle.fd(3*size)

draw_penrose_triangle(-80,-50,80,0)
Tags:

Related Post