PYTHON 2. 7 FEUILLE n° 24 onze MUSIC ET TURTLE
COURS:
# winsound fonctionne sous windows. Turtle lui utilise Python 2.7.
C'est un inconvénient.
Pour les animations avec Turtle cela amène un choix pour l'ordre.
Soit, on veut le son d'abord puis le dessin après, soit on veut le contraire.
# Rappel :
-----------------------------------------------------------------------------------------------------------------------------------
EXERCICE 1:
Donner un script, en Python 2.7, qui dessine le drapeau européen où les étoiles
sont des disques jaunes.
-------------------------------------------------------------------------------------------------------------------------------------------
REPONSE:
On peut proposer:
from turtle import*
import math
def deplace(x,y):
penup(),goto(x,y),pendown()
def drapeurop():
ht()
deplace(-60, 200)
width(1),speed(0)
fillcolor("blue")
begin_fill()
for i in range(2): # Pour le rectangle bleu du drapeau
forward(150),right(90),forward(100),right(90)
end_fill()
color("yellow")
for i in range(12): # pour la couronne de petits disques jaunes
deplace(18 +35*cos((2*pi/12)*i),147+35*sin((2*pi/12)*i))
fillcolor("yellow")
begin_fill()
circle(2)
end_fill()
mainloop()
En activant drapeurop() il vient:
>>>
>>> drapeurop()
>>>
Puis on a :
-------------------------------------------------------------------------------------------------------------------
EXERCICE 2 :
Donner un script, en Python 2.7 , qui dessine un V en drapeaux tricolors.
-----------------------------------------------------------------------------------------------------------
REPONSE :
On peut proposer :
from turtle import*
import math
def deplace(x,y):
penup(),goto(x,y),pendown()
def rect(x,y):
ht()
deplace(x,y)
width(1),speed(0)
fillcolor("blue")
begin_fill()
for i in range(2): # Pour le rectangle bleu
forward(30),right(90),forward(100),right(90)
penup(),forward(30),pendown()
end_fill()
for i in range(2): # Pour le rectangle blanc
color("white")
forward(30),right(90),forward(100),right(90)
penup(),forward(30),pendown()
fillcolor("red")
begin_fill()
for i in range(2): # pour le rectangle rouge
forward(30),right(90),forward(100),right(90)
end_fill()
def commande():
for i in range(3):
rect(-390+i*110,210-i*120)
for i in range(4):
rect(-40+i*110,-150+i*120)
mainloop()
En activant commande() il vient :
>>>
>>> commande()
>>>
Puis
-------------------------------------------------------------------------------------------------------------------------------------------
EXERCICE 3:
Donner en Python 2.7, un script qui dessine d'abord, le drapeau européen, puis
joue l'hymne à la Joie ( avec la précision permise par l'ordi ) et enfin dessine
un grand V en drapeaux tricolors.
----------------------------------------------------------------------------------------------------------------
REPONSE :
On peut proposer:
from turtle import*
import math
import binascii
import winsound
def deplace(x,y):
penup(),goto(x,y),pendown()
def drapeurop():
ht()
deplace(-60, 200)
width(1),speed(0)
fillcolor("blue")
begin_fill()
for i in range(2):
forward(150),right(90),forward(100),right(90)
end_fill()
color("yellow")
for i in range(12):
deplace(18 +35*cos((2*pi/12)*i),147+35*sin((2*pi/12)*i))
fillcolor("yellow")
begin_fill()
circle(2)
end_fill()
def rect(x,y):
ht()
deplace(x,y)
width(1),speed(0)
fillcolor("blue")
begin_fill()
for i in range(2):
forward(30),right(90),forward(100),right(90)
penup(),forward(30),pendown()
end_fill()
for i in range(2):
color("white")
forward(30),right(90),forward(100),right(90)
penup(),forward(30),pendown()
fillcolor("red")
begin_fill()
for i in range(2):
forward(30),right(90),forward(100),right(90)
end_fill()
def joie():
#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}
M1=['mi','mi','fa','sol'] # 4 noires donc 4 temps
M2=['sol','fa','mi','re' ] # idem
M3=['do','do','re','mi'] # idem
M4=['mi','re'] # 2 blanches donc 4 temps
N1=['mi','do'] # 2 blanches donc 4 temps
M5=['re','re','mi','do'] # 4 noires donc 4 temps
M6=['re'] # 1 noire donc 1 temps
M7=['mi','fa'] # 2 croches donc 1 temps
M8= ['mi' ,'do'] # 2 noires donc 2 temps
M9=['re'] # 1 noire donc 1 temps
M10=['mi','fa'] # 2 croches donc 1 temps
M11= ['mi' ,'re'] # 2 noires donc 2 temps
M12=[ 'do','mi'] # 2 noires donc 2 temps
M13=['la'] # 1 blanche donc 2 temps
M14=['re'] # 1 noire pointé donc 1,5 temps
M15=['do'] # 1 croche noire donc 0,5 temps
M16=['do'] # 1 blanche donc 2 temps
# M1 | M2 | M3 | M4 constituent une mesure chiffrée 4 / 4 c-à-d 4 fois 4 temps
# M1 | M2 | M3 | N1 constituent une mesure de 4 / 4
# M5 | M6 M7 M8 | M9 M10 M11 | M12 N2 M13 constituent une mesure de 4 / 4
# M1 | M2 | M3 | M14 M15 M16 constituent une mesure de 4 / 4
D=octave6 # L'octave 6 a les fréquences les plus exactes , non approchées
############################### M1 | M2 | M3 | M4
for i in M1:
winsound.Beep(D[i],300)
for i in M2:
winsound.Beep(D[i],300)
for i in M3:
winsound.Beep(D[i],300)
for i in M4:
winsound.Beep(D[i],600)
########################## M1| M2 | M3 | N1
for i in M1:
winsound.Beep(D[i],300)
for i in M2:
winsound.Beep(D[i],300)
for i in M3:
winsound.Beep(D[i],300)
for i in N1:
winsound.Beep(D[i],600)
########################### M5 | M6 M7 M8 | M9 M10 M11 | M12 M13
for i in M5:
winsound.Beep(D[i],300)
for i in M6:
winsound.Beep(D[i],300)
for i in M7:
winsound.Beep(D[i],150)
for i in M8:
winsound.Beep(D[i],300)
for i in M9:
winsound.Beep(D[i],300)
for i in M10:
winsound.Beep(D[i],300)
for i in M11:
winsound.Beep(D[i],460)
for i in M12:
winsound.Beep(D[i],300)
for i in M13:
winsound.Beep(D[i],400)
############################ M1 | M2 | M3 | M14 M15 M16
for i in M1:
winsound.Beep(D[i],300)
for i in M2:
winsound.Beep(D[i],300)
for i in M3:
winsound.Beep(D[i],300)
for i in M14:
winsound.Beep(D[i],450)
for i in M15:
winsound.Beep(D[i],100)
for i in M16:
winsound.Beep(D[i],600)
#####################################
def commande():
drapeurop()
joie()
for i in range(3):
rect(-390+i*110,210-i*120 )
for i in range(4):
rect(-40+i*110,-150+i*120)
mainloop()
En activant commande() il vient :
>>>
>>> commande()
>>>
Puis:
Le drapeau européen apparaît.
La musique se fait entendre.
Enfin le V en drapeaux tricolors se forme.
------------------------------------------------------------------------------------------------------------------------------------------------------------------