1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
| from Tkinter import *
from random import randrange
duration=25
mes_couleurs=['blue','red','grey','purple','cyan','maroon','green','orange','yellow']
couleur='blue'
displayed=[]
def bye():
root.quit()
root.destroy()
def depl():
global duration
global couleur
global displayed
#Effacement triangle precedent
for i in range(len(displayed)):
graph.delete(displayed[i])
displayed=[]
#Deplacement des points
p1.move()
p2.move()
p3.move()
p4.move()
#Gestion couleur alleatoire
duration=duration+1
if duration >= 25:
c=randrange(9)
couleur=mes_couleurs[c]
duration=0
#Affichage triangle
displayed.append(graph.create_line(p1.o_x,p1.o_y,p2.o_x,p2.o_y,fill=couleur))
displayed.append(graph.create_line(p2.o_x,p2.o_y,p3.o_x,p3.o_y,fill=couleur))
displayed.append(graph.create_line(p3.o_x,p3.o_y,p1.o_x,p1.o_y,fill=couleur))
displayed.append(graph.create_line(p1.o_x,p1.o_y,p4.o_x,p4.o_y,fill=couleur))
displayed.append(graph.create_line(p2.o_x,p2.o_y,p4.o_x,p4.o_y,fill=couleur))
displayed.append(graph.create_line(p3.o_x,p3.o_y,p4.o_x,p4.o_y,fill=couleur))
#Gestion boucle infinie
root.after(50,depl)
class point:
o_x=0
o_y=0
e_x=0
e_y=0
speed_x=0
speed_y=0
def __init__(self):
self.o_x=randrange(0,600)
self.o_y=randrange(0,600)
self.e_x=randrange(0,600)
self.e_y=randrange(0,600)
self.speed_x=randrange(5,10)
self.speed_y=randrange(5,10)
def affic(self):
print "(",self.o_x,",",self.e_x,")"
print "(",self.o_y,",",self.e_y,")"
print ""
def move(self):
if (self.o_x >= 600) or (self.o_x <= 0):
self.e_x = 2*self.o_x - self.e_x
if (self.o_y >= 600) or (self.o_y <= 0):
self.e_y = 2*self.o_y - self.e_y
temp=self.o_x
self.o_x=self.o_x+0.005*self.speed_x*(self.e_x-self.o_x)
self.e_x=self.e_x+(self.o_x-temp)
temp=self.o_y
self.o_y=self.o_y+0.005*self.speed_y*(self.e_y-self.o_y)
self.e_y=self.e_y+(self.o_y-temp)
#Main
p1=point()
p2=point()
p3=point()
p4=point()
#Interface graphique
root=Tk()
root.title("Economiseur d\'ecran")
root.resizable(width=0,height=0)
root.protocol("WM_DELETE_WINDOW", bye)
graph=Canvas(root,width=600,height=600,bg='white')
graph.grid(row=0,column=0)
but=Button(root,text="Quit",command=bye)
but.grid(row=1,column=0)
depl()
root.mainloop() |
Partager