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
| #!/usr/bin/python3.4
# -*- coding:Utf-8 -*-
from tkinter import *
# Matrice initiale
mat1 = [[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,1,1,1,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0]]
# Fonctions
# Moteur
def etat(matrice, x, y):
"Contrôle des voisins et envoi du nouvel état"
i, x1, y1 = 0, 0, 0
nb_voisins = 0
etat_suivant = 0
coord_voisin = [-1,-1,-1,0,-1,1,0,1,1,1,1,0,1,-1,0,-1]
#compte le nombre de voisin vivant
while i < 16:
x1 = x + coord_voisin[i]
y1 = y + coord_voisin[i+1]
i+=2
if (x1 < 0) or (y1 < 0) or (x1 > 9) or (y1 > 9):
continue
else:
if (matrice[x1][y1] == 1):
nb_voisins += 1
#détermination de l'état de la cellule pour la nouvelle génération
if (nb_voisins == 3):
etat_suivant = 1
elif (matrice[x][y] == 1):
if (nb_voisins < 2) or (nb_voisins > 3):
etat_suivant = 0
else:
etat_suivant = 1
return etat_suivant
def new_gen ():
"Création de la nouvelle génération"
global mat1
x, y = 0, 0
mat2 = list(mat1)
while x < 10:
y = 0
while y <10:
mat2[x][y] = etat(mat1, x, y)
y+=1
x+=1
mat1 = list(mat2)
# Graphique
def draw_rectangle(x, y, t=20, coul='black'):
"Création d'un rectangle prenant de début (x, y) et de taille t"
can.create_rectangle(x, y, x+t, y+t, fill=coul)
def affiche(mat=mat1):
"Affichage de la matrice"
can.delete(ALL)
x, y, recx, recy = 0, 0, 0, 0
while x < 10:
y = 0
recx = 0
while y < 10:
if mat[x][y] == 1:
draw_rectangle(recx, recy)
y+=1
recx+=20
x+=1
recy+=20
def new_gen_affiche():
"Affichage de la nouvelle matrice"
new_gen()
affiche()
# ---------- Programme Principal ----------
fen1 = Tk()
fen1.title("Jeu de la Vie !")
can = Canvas(fen1, bg='white', height=200, width=200)
can.pack(side=TOP, padx=15, pady=15)
affiche()
Button(fen1, text='New Gen', command=new_gen_affiche).pack(side=RIGHT)
fen1.mainloop() |
Partager