Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Créé par vcanteloup, le 17/03/2015 en Python 3.2
- from tkinter import *
- import numpy as np
- from random import *
- fenetre=Tk()
- fenetre.title("Transhumance des pixels")
- tableau_quadrillage= np.zeros((20,20),dtype='i')
- tableau_comptage= np.zeros((20,20),dtype='i')
- H=0
- B=0
- R=0
- L=0
- nbCarre=0
- x3=0
- y3=0
- frame=Frame(height=5,bd=3,bg="black",relief=GROOVE)
- frame.pack(side=TOP,fill=X)
- bouton1=Button(frame,text="Quitter",bg="gray",command=fenetre.destroy)
- bouton1.pack(side=RIGHT)
- frame1 = Frame( bd=5,bg="White", relief=GROOVE)
- frame1.pack(side=LEFT,fill=X, padx=5, pady=5)
- Label(frame1,text="Paramètrage",bg="White").pack(side=TOP)
- frame2=Frame(frame1,height=2,bd=2,bg="White", relief=GROOVE)
- frame2.pack(side=TOP,fill=X,padx=5,pady=5)
- Label(frame2,text="Les options",bg="White").pack(padx=1, pady=1)
- frame3=Frame(frame1,height=2,bd=2,bg="White", relief=GROOVE)
- frame3.pack(side=BOTTOM,fill=X,padx=5,pady=5)
- Label(frame3,text="La taille",bg="White").pack(padx=1, pady=1)
- var=StringVar()
- scale = Scale(frame3,from_=0, to=150,orient=HORIZONTAL)
- scale.pack(anchor=W)
- zone_dessin=Canvas(fenetre,width=590,height=590,bg="white",bd=7,relief="ridge")
- zone_dessin.pack()
- label = Label(frame3, text="Nombre de boucle")
- label.pack()
- var=StringVar()
- valeur = Scale(frame3,from_=0, to=300,orient=HORIZONTAL)
- valeur.pack(anchor=W)
- def laboucle():
- for J in range(valeur.get()):
- isolation_surpopulation()
- fenetre.update()
- for x in range(100):
- H=H+30
- B=B+30
- R=R+30
- L=L+30
- zone_dessin.create_line(10,(R),610,(L))
- zone_dessin.create_line((H),10,(B),610)
- def clique (event):
- X,Y = event.x,event.y
- X=X/30
- Y=Y/30
- X=int(X)
- Y=int(Y)
- zone_dessin.create_rectangle(X*30,Y*30,X*30+30,Y*30+30,fill="black")
- tableau_quadrillage[Y,X]=1
- def Comptage():
- global tableau_comptage
- for y in range(len(tableau_quadrillage)):
- for x in range(len(tableau_quadrillage[0])):
- total = 0
- if(y > 0):
- total += tableau_quadrillage[y - 1][x]
- if(x > 0):
- total += tableau_quadrillage[y - 1][x - 1]
- if(x < 19):
- total += tableau_quadrillage[y - 1][x + 1]
- if(x > 0):
- total += tableau_quadrillage[y][x - 1]
- if(x < 19):
- total += tableau_quadrillage[y][x + 1]
- if(y < 19):
- total += tableau_quadrillage[y + 1][x]
- if(x > 0):
- total += tableau_quadrillage[y + 1][x - 1]
- if(x < 19):
- total += tableau_quadrillage[y + 1][x + 1]
- tableau_comptage[y][x]=int(total)
- #print(tableau_comptage)
- rendu()
- def point_aleatoire ():
- for k in range(scale.get()):
- while True:
- global x,y
- x=randint(0,599)
- y=randint(0,599)
- x=x/30
- y=y/30
- x=int(x)
- y=int(y)
- if not tableau_quadrillage[y][x]==1:break
- zone_dessin.create_rectangle(x*30,y*30,x*30+30,y*30+30,fill="black")
- x=x*30
- y=y*30
- global x1,y1
- x1=x+30
- y1=y+30
- x2=x+15
- y2=y+15
- naissance1(x2,y2)
- def naissance1 (x2,y2):
- global x3,y3
- x3=int((x2/30))
- y3=int((y2/30))
- global tableau_quadrillage
- tableau_quadrillage[y3,x3]=1
- global nbCarre
- nbCarre=tableau_quadrillage[y-2:y+1,x-2:x+1].sum()
- if nbCarre==3:
- pos=randTruc(x3,y3)
- tableau_quadrillage[pos[1]][pos[0]]=1
- def isolation_surpopulation ():
- global tableau_comptage
- Comptage()
- tableau_comptage= np.zeros((20,20),dtype='i')
- def rendu():
- for y in range(len(tableau_quadrillage)):
- for x in range(len(tableau_quadrillage[0])):
- if tableau_comptage[y][x]<=1:
- tableau_quadrillage[y][x]=0
- zone_dessin.create_rectangle(x*30,y*30,x*30+30,y*30+30,fill="white")
- if tableau_comptage[y][x]>=4:
- tableau_quadrillage[y][x]=0
- zone_dessin.create_rectangle(x*30,y*30,x*30+30,y*30+30,fill="white")
- if tableau_comptage[y][x]==3:
- tableau_quadrillage[y][x]=1
- zone_dessin.create_rectangle(x*30,y*30,x*30+30,y*30+30,fill="black")
- def reenitialisation():
- global tableau_quadrillage
- tableau_quadrillage= np.zeros((20,20),dtype='i')
- rendu()
- def randTruc (x1,y1):
- while True:
- y=randint(y1,y1+2)
- x=randint(x1,x1+2)
- if y==y1/2:
- x if randint(0,1)==0 else 0
- if x>=0 and y>=0 and x<20 and y<20 and tableau_quadrillage[y][x]!=1: break
- return x,y
- zone_dessin.bind('<Button-1>',clique)
- zone_dessin.pack
- bouton2=Button(frame,text="Placement",bg="gray",command=point_aleatoire)
- bouton2.pack(side=LEFT)
- bouton3=Button(frame,text="Lancer",bg="gray",command=laboucle)
- bouton3.pack(side=LEFT)
- bouton4=Button(frame,text="Un par un",bg="gray",command=isolation_surpopulation)
- bouton4.pack(side=LEFT)
- bouton5=Button(frame2,text="Réinitialisé",bg="gray",command=reenitialisation)
- bouton5.pack(anchor=W)
- fenetre.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement