1#!/usr/bin/env python3
2
3import turtle
4
5# --- functions ---
6
7def s(n, l):
8
9 if n == 0: # stop conditions
10
11 # draw filled rectangle
12
13 turtle.color('black')
14 turtle.begin_fill()
15 for _ in range (4):
16 turtle.forward(l)
17 turtle.left(90)
18 turtle.end_fill()
19
20 else: # recursion
21
22 # around center point create 8 smalles rectangles.
23 # create two rectangles on every side
24 # so you have to repeat it four times
25
26 for _ in range(4):
27 # first rectangle
28 s(n-1, l/3)
29 turtle.forward(l/3)
30
31 # second rectangle
32 s(n-1, l/3)
33 turtle.forward(l/3)
34
35 # go to next corner
36 turtle.forward(l/3)
37 turtle.left(90)
38
39 # update screen
40 turtle.update()
41
42# --- main ---
43
44# stop updating screen (to make it faster)
45turtle.tracer(0)
46
47# start
48s(4, 400)
49
50# event loop
51turtle.done()
52