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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
| from Tkinter import *
def AddPoint():
#Fonction pour l'ajout de point en début de jeu
global repartition, L, H
can.create_rectangle(10*int(Abs.get()),10*int(Ord.get()),10*(int(Abs.get())+1),10*(int(Ord.get())+1),fill='white')
repartition[int(Abs.get())+L*int(Ord.get())] = 1
def CreatePoint(x,y,color):
#Fonction pour la création d'un nouveau point
global repartition, L, H
can.create_rectangle(10*x,10*y,10*(x+1),10*(y+1),fill=color)
repartition[x+L*y] = 1
def DestructPoint(x,y):
#Fonction pour la création d'un nouveau point
global repartition, L, H
can.create_rectangle(10*x,10*y,10*(x+1),10*(y+1),fill='black')
repartition[x+L*y] = 0
def coord(X):
#Fonction qui renvoit le nombre de point voisin à la position X, note: X=x+y*L
global repartition, L, H
if X<L:
if X%L==0:
voisins = repartition[X+1] + repartition[X+L] + repartition[X+L+1]
elif X%L==L-1:
voisins = repartition[X-1] + repartition[X+L-1] + repartition[X+L]
else:
voisins = repartition[X-1] + repartition[X+1] + repartition[X+L-1]\
+ repartition[X+L] + repartition[X+L+1]
elif X>H*L-L-1:
if X%L==0:
voisins = repartition[X-L] + repartition[X-L+1] + repartition[X+1]
elif X%L==L-1:
voisins = repartition[X-L-1] + repartition[X-L] + repartition[X-1]
else:
voisins = repartition[X-L-1] + repartition[X-L] + repartition[X-L+1]\
+ repartition[X-1] + repartition[X+1]
elif X%L==0:
voisins = repartition[X-L] + repartition[X-L+1] + repartition[X+1]\
+ repartition[X+L] + repartition[X+L+1]
elif X%L==L-1:
voisins = repartition[X-L-1] + repartition[X-L] + repartition[X-1]\
+ repartition[X+L-1] + repartition[X+L]
else:
voisins = repartition[X-L-1] + repartition[X-L] + repartition[X-L+1]\
+ repartition[X-1] + repartition[X+1]\
+ repartition[X+L-1] + repartition[X+L] + repartition[X+L+1]
return voisins
def analyse():
#Analyse les points qui vont être créés et ceux qui vont disparaitres à ce tour-ci
global repartition
next = [-1 if (repartition[x]==1)*(coord(x)>3 or coord(x)<2) else\
1 if (coord(x)==3)*(repartition[x]==0) else 0 for x in range(len(repartition))]
return next
def NextStep(next):
#Fonction qui détruits les points morts et qui génére les nouveaux points
global L,H
for x in range(len(next)):
if next[x]==-1:
DestructPoint(x%L,(x-x%L)/L)
repartition[x]=0
elif next[x]==1:
CreatePoint(x%L,(x-x%L)/L,'green')
repartition[x]=1
def GreenWhite(next):
#Fonction qui transforme les points verts neufs en points blanch anciens
global L,H
for x in range(len(next)):
if next[x]==1:
CreatePoint(x%L,(x-x%L)/L,'white')
repartition[x]=1
def RunNext():
global n, next
if n > 0:
#Au premier lancement il n'y a pas de points verts
GreenWhite(next)
next=analyse()
NextStep(next)
n += 1
fn = Tk()
fn.title("Jeu de la vie")
L = 50
H = 50
n = 0
Lcanvas = L*10
Hcanvas = H*10
repartition = [0]*L*H
# Creation de points
#Absisse
Label1 = Label(fn, text = 'Absisse :')
Label1.pack(side = LEFT, padx = 5, pady = 5)
Abs= StringVar()
Champ = Entry(fn, textvariable= Abs)
Champ.focus_set()
Champ.pack(side = LEFT, padx = 5, pady = 5)
Abs.set("0")
#Ordonnée
Label2 = Label(fn, text = 'Ordonnee :')
Label2.pack(side = LEFT, padx = 5, pady = 5)
Ord= StringVar()
Champ = Entry(fn, textvariable= Ord)
Champ.focus_set()
Champ.pack(side = LEFT, padx = 5, pady = 5)
Ord.set("0")
#bouton d'ajout
BoutonAjout = Button(fn, text ='Ajouter point', command = AddPoint)
BoutonAjout.pack(side = LEFT, padx = 5, pady = 5)
#Canvas
can = Canvas(fn, width = Lcanvas, height = Hcanvas, bg = 'black')
can.pack()
#bouton de lancement
boutonRun = Button(fn, text = 'Run', command = RunNext)
boutonRun.pack()
fn.mainloop() |
Partager