Proposer une tortue python récursive réalisant :
- Avec p = 1 :
- Avec p = 2 :
- Avec p = 3 :
- Avec p = 10 :
- documentation
- un code
On trouvera la doc sur le module turtle ici.
import turtle as tl
from random import randint
from math import sqrt
def carre(cote) :
tl.pencolor(randint(0,255), randint(0,255), randint(0,255))
tl.fillcolor(randint(0,255), randint(0,255), randint(0,255))
tl.begin_fill()
for _ in range(4) :
tl.forward(cote)
tl.right(90)
tl.end_fill()
def pythagore( cote, profondeur ) :
if profondeur > 0 :
profondeur -= 1
carre(cote)
tl.forward(cote)
cote2 = cote/sqrt(2)
tl.left(45)
pythagore(cote2, profondeur)
tl.right(90)
tl.penup()
tl.forward(cote2)
tl.pendown()
pythagore(cote2, profondeur)
tl.penup()
tl.backward(cote2)
tl.pendown()
tl.left(45)
tl.backward(cote)
cote = 100
tl.setworldcoordinates( -3*cote, -1*cote, 3.5*cote,4.5*cote)
tl.setheading(90) # orientation intiale de la tête : vers le haut
tl.hideturtle() # on cache la tortue
tl.speed(0) # on accélère la tortue
tl.colormode(255) # pour codage rgb des couleurs
pythagore( cote, 3 )
tl.exitonclick() # pour garder ouverte la fenêtre