1 changed files with 143 additions and 0 deletions
@ -0,0 +1,143 @@ |
|||
import tkinter as tk |
|||
from copy import deepcopy |
|||
from time import sleep |
|||
lst_c=[] |
|||
lst_grille=[] |
|||
f_cont=[] |
|||
def creer_grille(): |
|||
global taille_carré |
|||
global lst_c |
|||
global grille |
|||
global lst_grille |
|||
#####Détruire les carré précédent############ |
|||
for detruire_l in range(0,len(lst_c)): |
|||
for detruire_col in range(0,len(lst_c[0])): |
|||
zone.delete(lst_c[detruire_l][detruire_col]) |
|||
##############création_grille########## |
|||
nb_ligne=nb_ligne_choisi.get() |
|||
nb_colonne=nb_colonne_choisi.get() |
|||
grille =[] |
|||
lst_grille=[] |
|||
for ligne in range(0,nb_ligne): |
|||
grille.append([]) |
|||
for colonne in range(0,nb_colonne): |
|||
grille[ligne].append(0) |
|||
###############création_l_carré########## |
|||
lst_c=[] |
|||
for ligne in range(len(grille)): |
|||
lst_c.append([]) |
|||
|
|||
###############création_carré########## |
|||
carré_col,carré_ligne=1246//nb_colonne,795//nb_ligne |
|||
if carré_col<carré_ligne: |
|||
taille_carré=carré_col |
|||
else: |
|||
taille_carré=carré_ligne |
|||
|
|||
for ligne in range(len(grille)): |
|||
for colone in range(len(grille[0])): |
|||
c=zone.create_rectangle( colone *taille_carré , ligne *taille_carré , colone*taille_carré+taille_carré , ligne*taille_carré+taille_carré , fill ="white") |
|||
lst_c[ligne].append(c) |
|||
|
|||
def afficheCarte (): |
|||
for ligne in range (len(grille)): |
|||
for col in range (len(grille[0])): |
|||
if grille [ligne][col] == 1: |
|||
zone.itemconfig(lst_c[ligne][col],fill ="black") |
|||
fenetre.update() |
|||
else: |
|||
zone.itemconfig(lst_c[ligne][col],fill ="white") |
|||
fenetre.update() |
|||
|
|||
|
|||
|
|||
def change(event): |
|||
global grille |
|||
num_colone=event.x//taille_carré |
|||
num_ligne=event.y//taille_carré |
|||
if 0<event.x<1246 and 0<event.y<795: |
|||
if grille[num_ligne][num_colone]==0: |
|||
grille[num_ligne][num_colone]=1 |
|||
elif grille[num_ligne][num_colone]==1: |
|||
grille[num_ligne][num_colone]=0 |
|||
afficheCarte () |
|||
|
|||
def gen_suiv(): |
|||
global grille |
|||
global copy_grille |
|||
global lst_grille |
|||
copy_grille=deepcopy(grille) |
|||
lst_grille.append(copy_grille) |
|||
vie=0 |
|||
for ligne in range (0,len(grille)): |
|||
for col in range (0,len(grille[0])): |
|||
for l_contour in range(-1,2): |
|||
for col_contour in range(-1,2): |
|||
if l_contour==0 and col_contour==0: |
|||
pass |
|||
elif copy_grille[(ligne+l_contour)%len(grille)][(col+col_contour)%len(grille[0])]==1: |
|||
vie=vie+1 |
|||
if copy_grille[ligne][col]==1 and (vie==2 or vie==3): |
|||
grille[ligne][col]=1 |
|||
elif copy_grille[ligne][col]==1 and (vie<2 or vie>3): |
|||
grille[ligne][col]=0 |
|||
elif copy_grille[ligne][col]==0 and vie==3: |
|||
grille[ligne][col]=1 |
|||
vie=0 |
|||
afficheCarte () |
|||
|
|||
def gen_prev(): |
|||
global grille |
|||
global lst_grille |
|||
grille=deepcopy(lst_grille[len(lst_grille)-1]) |
|||
lst_grille.pop(len(lst_grille)-1) |
|||
afficheCarte () |
|||
|
|||
def gen_cont(): |
|||
global f_cont |
|||
f_cont=True |
|||
while f_cont==True: |
|||
gen_suiv() |
|||
|
|||
if f_cont==False: |
|||
break |
|||
def stop(): |
|||
global f_cont |
|||
f_cont=False |
|||
##########Programme principale############## |
|||
fenetre = tk.Tk() |
|||
fenetre.title("Jeu Amoa") |
|||
zone = tk.Canvas(fenetre , height =795 , width =1246 , bg="#ffffff") |
|||
zone.grid(row=1,column=2,rowspan=9) |
|||
|
|||
##################Grille#################### |
|||
nb_ligne_choisi=tk.IntVar() |
|||
mess_ligne=tk.Label(text="Nombre de ligne choisi") |
|||
mess_ligne.grid(row=1,column=1) |
|||
choix_l=tk.Entry(textvariable=nb_ligne_choisi,bd=5) |
|||
choix_l.grid(row=2,column=1) |
|||
|
|||
nb_colonne_choisi=tk.IntVar() |
|||
mess_col=tk.Label(text="Nombre de colone choisi") |
|||
mess_col.grid(row=3,column=1) |
|||
choix_col=tk.Entry(textvariable=nb_colonne_choisi,bd=5) |
|||
choix_col.grid(row=4,column=1) |
|||
|
|||
bout_g=tk.Button(text="Créer la grille",command= creer_grille) |
|||
bout_g.grid(row=5,column=1) |
|||
################Génération################## |
|||
suivant=tk.Button(text="Prochaine génération",command= gen_suiv) |
|||
suivant.grid(row=6,column=1) |
|||
|
|||
precedent=tk.Button(text="génération précédente",command= gen_prev) |
|||
precedent.grid(row=7,column=1) |
|||
|
|||
gen_continue=tk.Button(text="Génération en continue",command= gen_cont) |
|||
gen_continue.grid(row=8,column=1) |
|||
|
|||
stop=tk.Button(text="Stop",command=stop) |
|||
stop.grid(row=9,column=1) |
|||
############################################ |
|||
|
|||
zone.bind_all('<Button-3>', change) |
|||
fenetre.mainloop() |
Loading…
Reference in new issue