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
| from tkinter import *
grille = [[0]*36,[0]*36,[0]*36,[0]*36,[0]*36,[0]*36,[0]*36,[0]*36,[0]*36,[0]*36,[0]*36,[0]*36,[0]*36,[0]*36,[0]*36,[0]*36,[0]*36,[0]*36,[0]*36,[0]*36,[0]*36,[0]*36,[0]*36,[0]*36,[0]*36,[0]*36,[0]*36,[0]*36,[0]*36,[0]*36,[0]*36,[0]*36,[0]*36,[0]*36,[0]*36,[0]*36]
grille2 = [[0]*36,[0]*36,[0]*36,[0]*36,[0]*36,[0]*36,[0]*36,[0]*36,[0]*36,[0]*36,[0]*36,[0]*36,[0]*36,[0]*36,[0]*36,[0]*36,[0]*36,[0]*36,[0]*36,[0]*36,[0]*36,[0]*36,[0]*36,[0]*36,[0]*36,[0]*36,[0]*36,[0]*36,[0]*36,[0]*36,[0]*36,[0]*36,[0]*36,[0]*36,[0]*36,[0]*36]
fenetre = Tk()
toile = Canvas(fenetre, width=560, height=560, bg = "white")
for i in range(10,551,15):
toile.create_line(i,10,i,550)
toile.create_line(10, i, 550, i)
def dessiner(grille):
for i in range(36):
for j in range(36):
if grille[j][i] == 1:
toile.create_rectangle(10+15*i, 10+15*j,10+15*i+15, 10+15*j+15, fill = "black")
if grille[j][i] == 0:
toile.create_rectangle(10 + 15 * i, 10 + 15 * j, 10 + 15 * i + 15, 10 + 15 * j + 15, fill="white")
def init(evt):
a , b = -1,-1
for i in range(10, 551, 15):
a +=1
if i <= evt.x <= i +14.5:
for d in range(10, 551, 15):
b += 1
if d<=evt.y<= d + 14.5:
toile.create_rectangle(10 + 15 * a, 10 + 15 * b, 10 + 15 * a + 15, 10 + 15 * b + 15, fill="black")
grille[b][a]=1
def count():
for i in range(1,35):
for j in range(1,35):
c = 0
for k in range(-1, 2, 1):
if grille[j+k][i-1] == 1:
c += 1
if grille[j + k][i] == 1:
c += 1
if grille[j+k][i+1] == 1:
c += 1
if grille[j][i] == 1:
c = c-1
grille2[j][i] = c
def etape(evt):
count()
for i in range(1,35):
for j in range(1,35):
if grille2[j][i] < 2:
grille[j][i] = 0
elif grille2[j][i] > 3:
grille[j][i] = 0
elif grille2[j][i] == 3:
grille[j][i] = 1
dessiner(grille)
def lie():
toile.bind("<Button-1>", etape)
toile.bind("<Button-1>", init)
button = Button(fenetre ,text='OK', command = lie, bg = "white")
button.pack()
toile.pack()
fenetre.mainloop() |
Partager