# Créé par alexanor, le 31/03/2016 en Python 3.2 # Créé par alexanor, le 30/03/2016 en Python 3.2 # Créé par Nous (les Bogoss), le 12/03/2016 en Python 3.2 from tkinter import* from time import* from random import* def tableau(): #crée un tableu de 40 cases ligne() colonne() def colonne(): #crée 40 lignes verticales x=0 while x!=400: canvas.create_line(x,0,x,400) x=x+10 def ligne(): #crée 40 lignes horizontales x=0 while x!=400: canvas.create_line(0,x,400,x) x=x+10 def liste(n): #une liste de 40 listes de 40 zéro (initialement) l=list(range(n)) for i in range(n): l[i]=list(range(n)) for j in range(n): l[i][j]=0 return l def aléatoire(): #rend vivante aléatoirement 100 cellules du tableau n=0 while n!=100: x=randint(0,39) y=randint(0,39) ini(x,y) n=n+1 def go(): #démarrage de l'animation comptage() f.after(temps,go) def ral(): global temps temps=temps+200 def acc(): global temps if temps>200: temps=temps-200 def creer(event): x,y=event.x,event.y x,y=int(x/10),int(y/10) ini(y+1,x+1) def ini(x,y): #active la cellule de coordonnée x,y l[x][y]=1 canvas.create_rectangle((y-1)*10,x*10,y*10,(x-1)*10,fill="black") def détruit(x,y): #détruit la celule de coordonnée x,y l[x][y]=0 canvas.create_rectangle((y-1)*10,x*10,y*10,(x-1)*10,fill="white") def comptage(): #fonction comptant le nombre de voisin de chaque cellule et remplit ainsi la liste c global m compt=0 for i in range(len(l)): for j in range(len(l[i])): if i==0 and j==0: #coin en haut à gauche compt=l[0][1]+l[1][0]+l[1][1] c[0][0]=compt elif i==0 and j==39: #coin en haut à droite compt=l[0][38]+l[1][38]+l[1][39] c[0][39]=compt elif i==39 and j==0: #coin en bas à gauche compt=l[38][0]+l[38][1]+l[39][1] c[39][0]=compt elif i==39 and j==39: #coin en bas à droite compt=l[38][39]+l[38][38]+l[39][38] c[39][39]=compt elif 03): détruit(i,j) #programme principale temps=5000 l=liste(40) c=liste(40) f=Tk() bouton1=Button(f,text="générer 100 cellules vivantes aléatoirement",command=aléatoire) bouton1.pack() bouton2=Button(f,text="Go!",command=go) bouton2.pack() bouton3=Button(f,text="Accélerer",command=acc) bouton3.pack() bouton4=Button(f,text="Ralentir",command=ral) bouton4.pack() canvas=Canvas(f,width=400,height=400,background="white") canvas.bind("",creer) canvas.pack() tableau() f.mainloop()