PYTHON 2.7 FEUILLE N° 24 Treize MUSIC TURTLE 19 juin 2019
COURS :
# Pour effectuer des dessins on peut jouer sur les recouvrements et les masques.
------------------------------------------------------------------------------------------------------------------
EXERCICE 1 :
Donner un script, en Python 2.7 , qui fabrique le drapeau du pays du Brexit .
--------------------------------------------------------------------------------------------------------------------------------
REPONSE :
Sur fond jaune par exemple on peut proposer :
from turtle import*
import math
def deplace(x,y):
penup(),goto(x,y),pendown()
def rect(x,y,couleur,longueur,largeur):
ht()
deplace(x,y)
speed(0)
color(couleur)
fillcolor(couleur)
begin_fill()
for i in range(2):
forward(longueur),right(90),forward(largeur),right(90)
end_fill()
def drapeau_UK():
ht()
bgcolor("yellow")
deplace(-60, 200)
speed(0)
color("violet")
fillcolor("blue") # Fond bleu
begin_fill()
for i in range(2):
forward(150),right(90),forward(100),right(90)
end_fill()
######################## Diagonale principale
right(33)
color("white")
rect(-60,210,"white",180,3)
color("red")
rect(-60,206,"red",183,15)
color("white")
rect(-60,193,"white",180,3)
left(33)
###################### Diagonale secondaire
deplace(-60,100)
left(33)
color("white")
rect(-60,112,"white",180,3)
color("red")
rect(-62,107,"red",183,15)
color("white")
rect(-60,92,"white",180,3)
right(33)
######## Rectification des quatre bords à l'aide de la couleur jaune du fond
rect(-65,210,"yellow",210,10) # haut
rect(-70,200,"yellow",10,110) # gauche
rect(-70,100,"yellow",210,10) # bas
rect(90,200,"yellow",12,110) # droite
######################### bande horizontale
deplace(-60, 200)
color("white")
rect(-60,160,"white",150,3)
color("red")
rect(-60, 156,"red",150,15)
color("white")
rect(-60, 140,"white",150,3)
####################
rect(7,200,"white",3,42) partie de bande verticale
color("red")
rect(11, 200,"red",15,42.5)
color("white")
rect(25, 200,"white",3,42)
rect(7,140,"white",3,40) # partie de bande verticale
color("red")
rect(11,140,"red",15,40)
color("white")
rect(25, 140,"white",3,40)
mainloop()
En activant drapeau_UK() il vient :
>>>
>>> drapeau_UK()
>>>
Puis :
( La construction est progressive , le fond bleu , les diagonales , la croix .)
---------------------------------------------------------------------------------------------------
EXERCICE 2:
Donner un script , en Python 2.7, qui fait apparaître le drapeau de l'UK, puis fait entendre le début
du God save the Queen.
------------------------------------------------------------------------------------------------------------------------------------------------------------
REPONSE :
On peut envisager :
from turtle import*
import math
import binascii
import winsound
def deplace(x,y):
penup(),goto(x,y),pendown()
def rect(x,y,couleur,longueur,largeur):
ht()
deplace(x,y)
speed(0)
color(couleur)
fillcolor(couleur)
begin_fill()
for i in range(2):
forward(longueur),right(90),forward(largeur),right(90)
end_fill()
def drapeau_UK():
ht()
bgcolor("white")
deplace(-60, 200)
speed(0)
color("blue")
fillcolor("blue") # Fond bleu
begin_fill()
for i in range(2):
forward(150),right(90),forward(100),right(90)
end_fill()
######################## Diagonale principale
right(33)
color("white")
rect(-60,210,"white",180,3)
color("red")
rect(-60,206,"red",183,15)
color("white")
rect(-60,193,"white",180,3)
left(33)
###################### Diagonale secondaire
deplace(-60,100)
left(33)
color("white")
rect(-60,112,"white",180,3)
color("red")
rect(-62,107,"red",183,15)
color("white")
rect(-60,92,"white",180,3)
right(33)
################ Rectification des bords. La couleur du fond est white
rect(-65,210,"white",210,10) # haut
rect(-70,200,"white",10,110) # gauche
rect(-70,100,"white",210,10) # bas
rect(90,200,"white",12,110) # droite
#########################" bande horizontale
deplace(-60, 200)
color("white")
rect(-60,160,"white",150,3)
color("red")
rect(-60, 156,"red",150,15)
color("white")
rect(-60, 140,"white",150,3)
rect(7,200,"white",3,42) # partie de bande verticale
color("red")
rect(11, 200,"red",15,42.5)
color("white")
rect(25, 200,"white",3,42)
rect(7,140,"white",3,40)
color("red")
rect(11,140,"red",15,40)
color("white")
rect(25, 140,"white",3,40)
def brit():
# octave7={'do':4186,'dodiese':4435, 're': 4698,'rediese':4978,'mi':5274,'fa':5588,'fadiese': 5920,'sol':6272,'soldiese':6645,'la':7040,'ladiese':7458,'si':7902}
# octave6={'do':2093,'dodiese':2217, 're': 2349,'rediese':2489,'mi':2637,'fa':2794,'fadiese': 2960,'sol':3136,'soldiese':3322,'la':3520,'ladiese':3729,'si':3951}
#octave5={'do':1047,'dodiese':1109, 're': 1175,'rediese':1245,'mi':1318,'fa':1397,'fadiese': 1480,'sol':1568,'soldiese':1661,'la':1760,'ladiese':1865,'si':1975}
#octave4 = {'do': 523,'dodiese':554, 're': 587,'rediese':622,'mi':659,'fa':698,'fadiese': 740,'sol':784,'soldiese':831,'la':880,'ladiese':932,'si':988}
octave3={'do': 262,'dodiese':277, 're': 294,'rediese':311,'mi':330,'fa':349,'fadiese': 370,'sol':392,'soldiese':415 , 'la':440 ,'ladiese':466,'si':494}
#octave2={'do': 131,'dodiese':139, 're': 147,'rediese':156,'mi':165,'fa':175,'fadiese': 185,'sol':196,'soldiese':208,'la':220,'ladiese':233,'si':247}
#octave1= {'do': 65,'dodiese':69, 're': 73,'rediese':78,'mi':82,'fa':87,'fadiese': 92,'sol':98,'soldiese':103,'la':110,'ladiese':116,'si':123}
#octave0={'do': 33, 're':37,'rediese':39,'mi':41,'fa':44,'fadiese': 46,'sol':49,'soldiese':52,'la':55,'ladiese':58,'si':62}
B=octave3
C=octave4
D=octave5
E=octave6
######################################
M1=['fa','fa','sol']
M2=['mi']
M3=['fa']
M4=['sol']
M5=['la']
M6=['la','ladiese']
M7=['la']
M8=['sol']
M9=['fa']
M10=['sol','fa']
M11=['mi']
####################################
for i in M1:
winsound.Beep(B[i],600)
for i in M2:
winsound.Beep(B[i],500)
for i in M3:
winsound.Beep(B[i],360)
for i in M4:
winsound.Beep(B[i],400)
for i in M5:
winsound.Beep(B[i],600)
for i in M6:
winsound.Beep(B[i],500)
for i in M7:
winsound.Beep(B[i],500)
for i in M8:
winsound.Beep(B[i],600)
for i in M9:
winsound.Beep(B[i],700)
for i in M10:
winsound.Beep(B[i],600)
for i in M11:
winsound.Beep(B[i],800)
def commande():
drapeau_UK()
brit()
mainloop()
En activant commande() on a:
>>>
>>> commande()
>>>
Puis, on voit se construire l'Union Jack et enfinle début de l'hymne de l'UK retentit.
------------------------------------------------------------------------------------------------------------------------------------------------------
EXERCICE 3:
Imaginez un script , en Python 2.7, qui montre un navire nommé BREXIT qui
coule au fond de la mer .
--------------------------------------------------------------------------------------------------------------------
REPONSE:
On peut proposer:
from turtle import*
import math
def deplace(x,y):
penup(),goto(x,y),pendown()
def rect(x,y,couleur,longueur,largeur): # Pour avoir un rectangle plein
ht()
deplace(x,y)
speed(0)
color(couleur)
fillcolor(couleur)
begin_fill()
for i in range(2):
forward(longueur),right(90),forward(largeur),right(90)
end_fill()
def coque(x,y,couleur): # Pour avoir la coque du navire sans les cheminées
width(1)
rect(x,y, couleur,100,30)
penup(),forward(27),left(90),forward(10),right(90),pendown()
def mer():
rect(-250,0,"blue",500,250)
def bateau(x,y,couleur,couleur_chem):
coque(x,y,couleur)
width(2)
fillcolor(couleur_chem) # Une cheminée
begin_fill()
for i in range(2):
forward(4),right(90),forward(10),right(90)
end_fill()
penup(),forward(30),pendown()
fillcolor(couleur_chem) # Une autre cheminée
begin_fill()
for i in range(2):
forward(4),right(90),forward(10),right(90)
end_fill()
def temporisateur():
for i in range(10**7):
i=i+1
for i in range(10**7):
i=i+1
def coule():
bateau(0,25,"black","red")
temporisateur()
penup(),forward(-20),right(90),forward(29),pendown()
color("white"),width(7)
write("BREXIT")
temporisateur()
temporisateur()
left(90)
rect(-10,35,"white",120,35)
rect(-245,0,"blue",490,50)
penup(),right(90),forward(35),left(90)
right(10)
speed(0)
bateau(0,-6,"black","red")
bateau(0,-6,"blue", "blue")
for i in range(1,3):
forward(i*10), right(i*20)
bateau(0,-6-4*i,"black","black")
bateau(0,-6-4*i,"blue","blue")
deplace(-30,-90),color("white"),width(5)
write("IT ' S A VERY STRONG BREXIT ! IS NOT IT ?")
left(70)
bateau(0,-220 ,"black","black") # Navire au fond de l'eau
left(15)
for i in range(5): # Pour les bulles
deplace(20,-210+i*20)
color("white")
circle(2*i)
deplace( 60,-130)
write(" WHAT GOOD IDEA !" )
def commande():
mer()
coule()
mainloop()
En activant commande il vient :
>>>
>>> commande()
>>>
Puis :
On voit le navire à la surface avant de plonger...
Le navire coule ensuite et se retrouve progressivement au fond.
-----------------------------------------------------------------------------------------------------