Modifications

Sauter à la navigation Sauter à la recherche
2 171 octets ajoutés ,  19 mars 2020 à 21:56
Ligne 1 : Ligne 1 :  
{{Python-Turtle-Online-NAV}}
 
{{Python-Turtle-Online-NAV}}
   −
== Introduction ==
+
== Démarrer Turtle ==
 
Voici une petite série d'exemples Turtle dont la complexité du code augmente progressivement.  
 
Voici une petite série d'exemples Turtle dont la complexité du code augmente progressivement.  
   Ligne 8 : Ligne 8 :  
{{download-box|Démarrer Python Turtle en ligne|https://repl.it/languages/python_turtle}}
 
{{download-box|Démarrer Python Turtle en ligne|https://repl.it/languages/python_turtle}}
   −
== Faire un carré ==   
+
Avant de débuter le tracé graphique, point de départ de la tortue est celui ci:
 +
* Au centre de la zone de dessin
 +
* Tourné vers la droite
 +
 
 +
[[Fichier:Python-Turtle-Exemple-00b.png]]
 +
 
 +
== Dessiner une ligne ==
 +
 
 +
<syntaxhighlight lang="python">
 +
# Etape 1: rendre les instructions "turtle"
 +
#          disponible pour le script.
 +
import turtle
 +
 
 +
# Etape 2: Créer une nouvelle tortue. Elle s'appelle "t"
 +
t = turtle.Turtle()
 +
 
 +
# Etape 3: Déplacer la tortue "t" vers
 +
#          l'avant de 50 pixels
 +
t.forward(50)
 +
 
 +
# Etape 4: Indiquer que c'est fini (OPTIONNEL)
 +
turtle.done()
 +
</syntaxhighlight>
 +
 
 +
Résultat attendu:
 +
 
 +
[[Fichier:Python-Turtle-Exemple-00a.png]]
 +
 
 +
A noter que la flèche en bout de ligne est la dite "tortue" et indique la direction dans laquelle est pointe.
 +
 
 +
Ce second exemple ci-dessous dessine deux sections de lignes avec une rotation de la tortue de 35 degrés sur la gauche.
 +
 
 +
<syntaxhighlight lang="python">
 +
import turtle
 +
 
 +
t = turtle.Turtle()
 +
 
 +
t.forward( 50 )
 +
 
 +
# Tourner à gauche de 35 degrés
 +
t.left(35)
 +
# Avancer de 25 pixels
 +
t.forward( 25 )
 +
</syntaxhighlight>
 +
 
 +
[[Fichier:Python-Turtle-Exemple-00c.png]]
 +
 
 +
== Dessiner un carré ==   
 +
 
 +
Dessiner un carré se résume a parcourir 4 fois la même distance en effectuant une rotation de 90° entre chaque tronçon.
    
<syntaxhighlight lang="python">
 
<syntaxhighlight lang="python">
Ligne 29 : Ligne 78 :  
t.right( 90 ) # Tourner droite 90 degrés
 
t.right( 90 ) # Tourner droite 90 degrés
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
Résultat attendu:
    
[[Fichier:Python-Turtle-Exemple-00.png]]
 
[[Fichier:Python-Turtle-Exemple-00.png]]
   −
== Faire un carré : syntaxe abrégée ==
+
== Dessiner un carré : syntaxe abrégée ==
    
<syntaxhighlight lang="python">
 
<syntaxhighlight lang="python">
Ligne 53 : Ligne 104 :  
t.rt( 90 ) # Tourner droite 90 degrés
 
t.rt( 90 ) # Tourner droite 90 degrés
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
Résultat attendu:
    
[[Fichier:Python-Turtle-Exemple-00.png]]
 
[[Fichier:Python-Turtle-Exemple-00.png]]
   −
== faire un carré : avec plusieurs couleurs ==
+
== Dessiner un carré : avec plusieurs couleurs ==
    
<syntaxhighlight lang="python">
 
<syntaxhighlight lang="python">
Ligne 69 : Ligne 122 :  
     t.left(90)    # Tourner a gauche de 90 degrés
 
     t.left(90)    # Tourner a gauche de 90 degrés
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
Résultat attendu:
    
[[Fichier:Python-Turtle-Exemple-01.png]]
 
[[Fichier:Python-Turtle-Exemple-01.png]]
   −
== Faire un carré : avec une fonction ==  
+
== Dessiner un carré : avec une fonction ==  
    
<syntaxhighlight lang="python">
 
<syntaxhighlight lang="python">
Ligne 88 : Ligne 143 :  
faire_carre() # appel de la fonction
 
faire_carre() # appel de la fonction
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
Résultat attendu:
    
[[Fichier:Python-Turtle-Exemple-02.png]]
 
[[Fichier:Python-Turtle-Exemple-02.png]]
 +
 +
== Dessiner une étoile ==
 +
 +
<syntaxhighlight lang="python">
 +
import turtle
 +
 +
t = turtle.Turtle()
 +
 +
for i in range(5): # répéter 5 fois
 +
    t.forward(50)
 +
    t.right(144)
 +
</syntaxhighlight>
 +
 +
Résultat attendu:
 +
 +
[[Fichier:Python-Turtle-Exemple-10.png]]
 +
 +
== Dessiner une étoile spiralée ==
 +
 +
<syntaxhighlight lang="python">
 +
import turtle
 +
 +
t = turtle.Turtle()
 +
 +
for i in range(20): # i de 0 à 19
 +
    t.forward(i * 10)
 +
    t.right(144)
 +
</syntaxhighlight>
 +
 +
Résultat attendu:
 +
 +
[[Fichier:Python-Turtle-Exemple-11.png]]
 +
 +
'''Note''': à chaque fois que l'on déplace la tortue vers l'avant d'une quantité différente. Comme {{fname|i}} varie de 0 à 19, la distance parcourue de par {{fname|forward}} à chaque itération sera de {{fname|i * 10}} soit la suite 0, 10, 20, 30, 40, ... 180, 190.
    
== SpiroGraphe simple ==   
 
== SpiroGraphe simple ==   
Ligne 112 : Ligne 203 :  
faire_fleur()
 
faire_fleur()
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
Résultat attendu:
    
[[Fichier:Python-Turtle-Exemple-03.png]]
 
[[Fichier:Python-Turtle-Exemple-03.png]]
Ligne 130 : Ligne 223 :  
done()
 
done()
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
Résultat attendu:
    
[[Fichier:Python-Turtle-Exemple-04.png]]
 
[[Fichier:Python-Turtle-Exemple-04.png]]
    
{{Python-Turtle-Online-TRAILER}}
 
{{Python-Turtle-Online-TRAILER}}
29 917

modifications

Menu de navigation