Advertisement
Guest User

Untitled

a guest
May 26th, 2015
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.36 KB | None | 0 0
  1. # Créé par vcanteloup, le 17/03/2015 en Python 3.2
  2. from tkinter import *
  3. import numpy as np
  4. from random import *
  5. fenetre=Tk()
  6. fenetre.title("Transhumance des pixels")
  7.  
  8.  
  9. tableau_quadrillage= np.zeros((20,20),dtype='i')
  10. tableau_comptage= np.zeros((20,20),dtype='i')
  11.  
  12.  
  13. H=0
  14. B=0
  15. R=0
  16. L=0
  17. nbCarre=0
  18. x3=0
  19. y3=0
  20.  
  21.  
  22. frame=Frame(height=5,bd=3,bg="black",relief=GROOVE)
  23. frame.pack(side=TOP,fill=X)
  24. bouton1=Button(frame,text="Quitter",bg="gray",command=fenetre.destroy)
  25. bouton1.pack(side=RIGHT)
  26.  
  27.  
  28. frame1 = Frame( bd=5,bg="White", relief=GROOVE)
  29. frame1.pack(side=LEFT,fill=X, padx=5, pady=5)
  30. Label(frame1,text="Paramètrage",bg="White").pack(side=TOP)
  31.  
  32.  
  33. frame2=Frame(frame1,height=2,bd=2,bg="White", relief=GROOVE)
  34. frame2.pack(side=TOP,fill=X,padx=5,pady=5)
  35. Label(frame2,text="Les options",bg="White").pack(padx=1, pady=1)
  36.  
  37.  
  38.  
  39.  
  40. frame3=Frame(frame1,height=2,bd=2,bg="White", relief=GROOVE)
  41. frame3.pack(side=BOTTOM,fill=X,padx=5,pady=5)
  42. Label(frame3,text="La taille",bg="White").pack(padx=1, pady=1)
  43.  
  44.  
  45.  
  46. var=StringVar()
  47. scale = Scale(frame3,from_=0, to=150,orient=HORIZONTAL)
  48. scale.pack(anchor=W)
  49.  
  50.  
  51. zone_dessin=Canvas(fenetre,width=590,height=590,bg="white",bd=7,relief="ridge")
  52. zone_dessin.pack()
  53.  
  54. label = Label(frame3, text="Nombre de boucle")
  55. label.pack()
  56.  
  57. var=StringVar()
  58. valeur = Scale(frame3,from_=0, to=300,orient=HORIZONTAL)
  59. valeur.pack(anchor=W)
  60.  
  61. def laboucle():
  62. for J in range(valeur.get()):
  63. isolation_surpopulation()
  64. fenetre.update()
  65.  
  66. for x in range(100):
  67. H=H+30
  68. B=B+30
  69. R=R+30
  70. L=L+30
  71. zone_dessin.create_line(10,(R),610,(L))
  72. zone_dessin.create_line((H),10,(B),610)
  73.  
  74. def clique (event):
  75. X,Y = event.x,event.y
  76. X=X/30
  77. Y=Y/30
  78. X=int(X)
  79. Y=int(Y)
  80. zone_dessin.create_rectangle(X*30,Y*30,X*30+30,Y*30+30,fill="black")
  81. tableau_quadrillage[Y,X]=1
  82.  
  83.  
  84.  
  85.  
  86. def Comptage():
  87. global tableau_comptage
  88. for y in range(len(tableau_quadrillage)):
  89. for x in range(len(tableau_quadrillage[0])):
  90. total = 0
  91. if(y > 0):
  92. total += tableau_quadrillage[y - 1][x]
  93. if(x > 0):
  94. total += tableau_quadrillage[y - 1][x - 1]
  95. if(x < 19):
  96. total += tableau_quadrillage[y - 1][x + 1]
  97.  
  98. if(x > 0):
  99. total += tableau_quadrillage[y][x - 1]
  100. if(x < 19):
  101. total += tableau_quadrillage[y][x + 1]
  102.  
  103. if(y < 19):
  104. total += tableau_quadrillage[y + 1][x]
  105. if(x > 0):
  106. total += tableau_quadrillage[y + 1][x - 1]
  107. if(x < 19):
  108. total += tableau_quadrillage[y + 1][x + 1]
  109. tableau_comptage[y][x]=int(total)
  110. #print(tableau_comptage)
  111. rendu()
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120. def point_aleatoire ():
  121. for k in range(scale.get()):
  122. while True:
  123. global x,y
  124. x=randint(0,599)
  125. y=randint(0,599)
  126. x=x/30
  127. y=y/30
  128. x=int(x)
  129. y=int(y)
  130. if not tableau_quadrillage[y][x]==1:break
  131. zone_dessin.create_rectangle(x*30,y*30,x*30+30,y*30+30,fill="black")
  132. x=x*30
  133. y=y*30
  134. global x1,y1
  135. x1=x+30
  136. y1=y+30
  137. x2=x+15
  138. y2=y+15
  139. naissance1(x2,y2)
  140.  
  141.  
  142.  
  143. def naissance1 (x2,y2):
  144. global x3,y3
  145. x3=int((x2/30))
  146. y3=int((y2/30))
  147. global tableau_quadrillage
  148. tableau_quadrillage[y3,x3]=1
  149. global nbCarre
  150. nbCarre=tableau_quadrillage[y-2:y+1,x-2:x+1].sum()
  151. if nbCarre==3:
  152. pos=randTruc(x3,y3)
  153. tableau_quadrillage[pos[1]][pos[0]]=1
  154.  
  155.  
  156.  
  157. def isolation_surpopulation ():
  158. global tableau_comptage
  159. Comptage()
  160. tableau_comptage= np.zeros((20,20),dtype='i')
  161.  
  162.  
  163.  
  164.  
  165.  
  166. def rendu():
  167. for y in range(len(tableau_quadrillage)):
  168. for x in range(len(tableau_quadrillage[0])):
  169. if tableau_comptage[y][x]<=1:
  170. tableau_quadrillage[y][x]=0
  171. zone_dessin.create_rectangle(x*30,y*30,x*30+30,y*30+30,fill="white")
  172. if tableau_comptage[y][x]>=4:
  173. tableau_quadrillage[y][x]=0
  174. zone_dessin.create_rectangle(x*30,y*30,x*30+30,y*30+30,fill="white")
  175. if tableau_comptage[y][x]==3:
  176. tableau_quadrillage[y][x]=1
  177. zone_dessin.create_rectangle(x*30,y*30,x*30+30,y*30+30,fill="black")
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185. def reenitialisation():
  186. global tableau_quadrillage
  187. tableau_quadrillage= np.zeros((20,20),dtype='i')
  188. rendu()
  189.  
  190.  
  191.  
  192.  
  193. def randTruc (x1,y1):
  194. while True:
  195. y=randint(y1,y1+2)
  196. x=randint(x1,x1+2)
  197. if y==y1/2:
  198. x if randint(0,1)==0 else 0
  199. if x>=0 and y>=0 and x<20 and y<20 and tableau_quadrillage[y][x]!=1: break
  200. return x,y
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209. zone_dessin.bind('<Button-1>',clique)
  210. zone_dessin.pack
  211. bouton2=Button(frame,text="Placement",bg="gray",command=point_aleatoire)
  212. bouton2.pack(side=LEFT)
  213. bouton3=Button(frame,text="Lancer",bg="gray",command=laboucle)
  214. bouton3.pack(side=LEFT)
  215. bouton4=Button(frame,text="Un par un",bg="gray",command=isolation_surpopulation)
  216. bouton4.pack(side=LEFT)
  217. bouton5=Button(frame2,text="Réinitialisé",bg="gray",command=reenitialisation)
  218. bouton5.pack(anchor=W)
  219.  
  220.  
  221.  
  222. fenetre.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement