# Créé par Fred, le 26/02/2013 from __future__ import division from lycee import * import Tkinter as Tk from random import randrange from time import sleep large, haut = 500,500 cl=['purple','cyan','maroon','green','red','blue','orange','black','pink','khaki'] maxcl=len(cl) class Trace_segments : def __init__ (self, fenetre) : self.fenetre = fenetre self.creation () self.L = [] self.maxL=20 self.deja=0 self.arret=False self.cible=False self.x1, self.y1= large//2, haut//2 self.R1=self.can.create_rectangle(self.x1-large//8,self.y1-haut//3,self.x1+large//8,self.y1+haut//3,fill='orange',state='hidden') self.R0=self.can.create_rectangle(self.x1-large//3,self.y1-haut//8,self.x1+large//3,self.y1+haut//8,fill='orange',state='hidden') self.R2=self.can.create_oval(self.x1-large//4,self.y1-haut//4,self.x1+large//4,self.y1+haut//4,fill='red',state='hidden') self.R=[self.R0,self.R1,self.R2] def creation (self) : self.can = Tk.Canvas(self.fenetre,bg='white',height=haut,width=large) b1 = Tk.Button (self.fenetre, text="Lancer", command=self.commande_bouton1) b2 = Tk.Button (self.fenetre, text="Arrêt", command=self.commande_bouton2) b4 = Tk.Button (self.fenetre, text="Quitter", command=self.fenetre.quit) b3 = Tk.Button (self.fenetre, text="Cible", command=self.commande_bouton3) self.can.grid(column=0,row=0,rowspan=40) b1.grid (row=1, column=1,sticky=Tk.W) b2.grid (row=2, column=1,sticky=Tk.W) b3.grid (row=3, column=1,sticky=Tk.W) b4.grid (row=4, column=1,sticky=Tk.W) def commande_bouton1 (self) : self.arret=False self.controle () def commande_bouton2 (self) : self.arret=True self.zero() self.controle () def affiche_cible(self): for k in range(3): if self.cible:self.can.itemconfigure(self.R[k],state='normal') else:self.can.itemconfigure(self.R[k],state='hidden') def commande_bouton3 (self) : self.cible= not self.cible self.affiche_cible() def zero (self) : if self.deja>0: for k in range(len(self.L)): self.can.delete(self.L[k]) self.deja=0 self.L = [] def nouvelle_ligne(self,k): a=int(randint(-2,2)) b=int(randint(-2,2)) x2=(self.x1+a*randrange(large//20)) if x2>large:x2=large elif x2<0: x2=0 y2=(self.y1+b*randrange(haut//20)) if y2>haut:y2=haut elif y2<0: y2=0 l=randint(2,6) self.L.append(self.can.create_line(self.x1,self.y1,x2,y2,width=l,fill=cl[k%maxcl])) self.x1, self.y1 = x2, y2 def tourne_couleur(self,k): try: self.can.delete(self.L[0]) del self.L[0] self.nouvelle_ligne(k) except: pass def controle (self) : if self.deja==0 and not self.arret: for k in range(self.maxL): self.nouvelle_ligne(k) sleep(0.1) self.fenetre.update() self.deja=self.deja+1 while not self.arret: self.tourne_couleur(self.deja%self.maxL) self.deja=self.deja+1 self.fenetre.update() sleep(0.1) if __name__ == "__main__" : fen = Tk.Tk () f = Trace_segments(fen) fen.mainloop ()