# Créé par apatura, le 04/03/2014 en Python 3.2 from tkinter import * def creer(event): global x,y,rond1,x2,y2,rond2,nombreclic nombreclic=nombreclic+1 if nombreclic%2==1: c.delete(rond1) x,y=event.x,event.y rond1=c.create_oval(x-10,y-10,x+10,y+10,outline='red',fill='orange') deplacedroitebas1() else: c.delete(rond2) x2,y2=event.x,event.y rond2=c.create_oval(x2-10,y2-10,x2+10,y2+10,outline='red',fill='orange') deplacegauchehaut2() def creer2(event): globalx2,y2,rond2 x2,y2=event.x,event.y rond2=c.create_oval(x2-10,y2-10,x2+10,y2+10,outline='red',fill='orange') def vitesse(event): global v v= entree.get() def distance(): return (x-x2)*(x-x2)+(y-y2)*(y-y2) def deplacedroitebas1(): global x,y,d1 if distance()>=400 : d1=1 if x<290 and y<190: f.after(v,deplacedroitebas1) x=x+2 y=y+2 c.coords(rond1,x-10,y-10,x+10,y+10) elif x>=290: deplacegauchebas1() else: deplacedroitehaut1() else: if d2==2 : x=x-2 y=y+2 c.coords(rond1,x-10,y-10,x+10,y+10) deplacegauchebas1() elif d2==4: x=x+2 y=y-2 c.coords(rond1,x-10,y-10,x+10,y+10) deplacedroitehaut1() elif d2==3: x=x-2 y=y-2 c.coords(rond1,x-10,y-10,x+10,y+10) deplacegauchehaut1() else: x=x+2 y=y+2 c.coords(rond1,x-10,y-10,x+10,y+10) deplacedroitehaut1() def deplacegauchebas1(): global x,y,d1 if distance()>=400 : d1=2 if x>10 and y<190: f.after(v,deplacegauchebas1) x=x-2 y=y+2 c.coords(rond1,x-10,y-10,x+10,y+10) elif x<=10: deplacedroitebas1() else: deplacegauchehaut1() else: if d2==1: x=x+2 y=y+2 c.coords(rond1,x-10,y-10,x+10,y+10) deplacedroitebas1() elif d2==3: x=x-2 y=y-2 c.coords(rond1,x-10,y-10,x+10,y+10) deplacegauchehaut1() elif d2==4: x=x+2 y=y-2 c.coords(rond1,x-10,y-10,x+10,y+10) deplacedroitehaut1() else: x=x-2 y=y+2 c.coords(rond1,x-10,y-10,x+10,y+10) deplacegauchehaut1() def deplacegauchehaut1(): global x,y,d1 if distance()>=400 : d1=3 if x>10 and y>10: f.after(v,deplacegauchehaut1) x=x-2 y=y-2 c.coords(rond1,x-10,y-10,x+10,y+10) elif x<=10: deplacedroitehaut1() else: deplacegauchebas1() else: if d2==4 : x=x+2 y=y-2 c.coords(rond1,x-10,y-10,x+10,y+10) deplacedroitehaut1() elif d2==2: x=x-2 y=y+2 c.coords(rond1,x-10,y-10,x+10,y+10) deplacegauchebas1() elif d2==1: x=x+2 y=y+2 c.coords(rond1,x-10,y-10,x+10,y+10) deplacedroitebas1() else: x=x-2 y=y-2 c.coords(rond1,x-10,y-10,x+10,y+10) deplacedroitehaut1() def deplacedroitehaut1(): global x,y,d1 if distance()>=400 : d1=4 if x<290 and y>10: f.after(v,deplacedroitehaut1) x=x+2 y=y-2 c.coords(rond1,x-10,y-10,x+10,y+10) elif x>=290: deplacegauchehaut1() else: deplacedroitebas1() else: if d2==3 : x=x-2 y=y-2 c.coords(rond1,x-10,y-10,x+10,y+10) deplacegauchehaut1() elif d2==1: x=x+2 y=y+2 c.coords(rond1,x-10,y-10,x+10,y+10) deplacedroitebas1() elif d2==2: x=x-2 y=y+2 c.coords(rond1,x-10,y-10,x+10,y+10) deplacegauchebas1() else: x=x+2 y=y-2 c.coords(rond1,x-10,y-10,x+10,y+10) deplacegauchehaut1() def deplacedroitebas2(): global x2,y2,d2 d2=1 if x2<290 and y2<190: f.after(v,deplacedroitebas2) x2=x2+1 y2=y2+1 c.coords(rond2,x2-10,y2-10,x2+10,y2+10) elif x2>=290: deplacegauchebas2() else: deplacedroitehaut2() def deplacegauchebas2(): global x2,y2,d2 d2=2 if x2>10 and y2<190: f.after(v,deplacegauchebas2) x2=x2-1 y2=y2+1 c.coords(rond2,x2-10,y2-10,x2+10,y2+10) elif x2<=10: deplacedroitebas2() else: deplacegauchehaut2() def deplacegauchehaut2(): global x2,y2,d d2=3 if x2>10 and y2>10: f.after(v,deplacegauchehaut2) x2=x2-1 y2=y2-1 c.coords(rond2,x2-10,y2-10,x2+10,y2+10) elif x2<=10: deplacedroitehaut2() else: deplacegauchebas2() def deplacedroitehaut2(): global x2,y2,d2 d2=4 if x2<290 and y2>10: f.after(v,deplacedroitehaut2) x2=x2+1 y2=y2-1 c.coords(rond2,x2-10,y2-10,x2+10,y2+10) elif x2>=290: deplacegauchehaut2() else: deplacedroitebas2() d1,d2=0,0 #direction de chaque boule:1,2,3 ou 4 x,y,x2,y2,v,nombreclic=100,100,10,10,0,0 f=Tk() c=Canvas(f,height=200,width=300,bg='ivory') rond1= c.create_oval(x-10,y-10,x+10,y+10,outline='red',fill='black') #c.create_oval(0,00,20,20,outline='red') c.delete(rond1) rond2= c.create_oval(x2-10,y2-10,x2+10,y2+10,outline='red',fill='black') #c.create_oval(0,00,20,20,outline='red') c.delete(rond2) texte=Text(f, height=3, width=30) texte.pack() texte.insert(END,"Entrez la vitesse et validez , puis cliquez à deux endroits dans la fenêtre ") entree=Entry(f,background='white') entree.bind("",vitesse) entree.pack() c.bind("",creer) c.pack(side=TOP) b=Button(f,text="Quitter",command=f.destroy) b.pack() f.mainloop()