Hello,
I'm a French user (so my English is bad), and I make a "John Conway Life's Game".
I want to make a function that place where you want custom elements by clicking on the canvas (glider, blinker, and others...), but I'm going to be crazy!
Please, help me, here is the code :
The problems come from lines 102 to line 186, ignore the rest. I have this error :
If you want to help me, I think I have a problem with the variables appelation into the functions, the "return", and the buttons...
Thank you.
I'm a French user (so my English is bad), and I make a "John Conway Life's Game".
I want to make a function that place where you want custom elements by clicking on the canvas (glider, blinker, and others...), but I'm going to be crazy!

Please, help me, here is the code :
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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
from tkinter import * from time import * largeur = 60 hauteur = 60 cellules = [[ 0 for j in range (largeur)] for i in range (hauteur)] voisins = [[ 0 for j in range (largeur)] for i in range (hauteur)] def compte_voisins(i,j): if i = = 0 : if j = = 0 : return cellules[ 0 ][ 1 ] + cellules[ 1 ][ 1 ] + cellules[ 1 ][ 0 ] if j = = largeur - 1 : return cellules[ 0 ][largeur - 2 ] + cellules[ 1 ][largeur - 2 ] + \ cellules[ 1 ][largeur - 1 ] return cellules[ 0 ][j - 1 ] + cellules[ 0 ][j + 1 ] + \ cellules[ 1 ][j - 1 ] + cellules[ 1 ][j] + cellules[ 1 ][j + 1 ] if i = = hauteur - 1 : if j = = 0 : return cellules[hauteur - 1 ][ 1 ] + cellules[hauteur - 2 ][ 1 ] + cellules[hauteur - 2 ][ 0 ] if j = = largeur - 1 : return cellules[hauteur - 1 ][largeur - 2 ] + cellules[hauteur - 2 ][largeur - 2 ] + \ cellules[hauteur - 2 ][largeur - 1 ] return cellules[hauteur - 1 ][j - 1 ] + cellules[hauteur - 1 ][j + 1 ] + \ cellules[hauteur - 2 ][j - 1 ] + cellules[hauteur - 2 ][j] + cellules[hauteur - 2 ][j + 1 ] else : if j = = 0 : return cellules[i - 1 ][ 0 ] + cellules[i + 1 ][ 0 ] + \ cellules[i - 1 ][ 1 ] + cellules[i][ 1 ] + cellules[i + 1 ][ 1 ] if j = = largeur - 1 : return cellules[i - 1 ][largeur - 1 ] + cellules[i + 1 ][largeur - 1 ] + \ cellules[i - 1 ][largeur - 2 ] + cellules[i][largeur - 2 ] + cellules[i + 1 ][largeur - 2 ] else : return cellules[i - 1 ][j - 1 ] + cellules[i - 1 ][j] + cellules[i - 1 ][j + 1 ] + \ cellules[i ][j - 1 ] + cellules[i ][j + 1 ] + \ cellules[i + 1 ][j - 1 ] + cellules[i + 1 ][j] + cellules[i + 1 ][j + 1 ] def grille_voisins(): for i in range (hauteur): for j in range (largeur): voisins[i][j] = compte_voisins(i,j) def grille_lendemain(): for i in range (hauteur): for j in range (largeur): if cellules[i][j] = = 0 and voisins[i][j] = = 3 : cellules[i][j] = 1 if cellules[i][j] = = 1 and voisins[i][j] ! = 2 and voisins[i][j] ! = 3 : cellules[i][j] = 0 def affichage_grille(tab): for i in range ( len (tab)): print (tab[i]) def jeu(): dessine_etat() for i in range (regle_jours.get()): print ( " " ) grille_voisins() grille_lendemain() sleep( 0.1 ) dessine_etat() def dessine_cellule(i,j): can.create_rectangle(j * e, i * e,(j + 1 ) * e - 1 ,(i + 1 ) * e - 1 , fill = "red" , width = 0 ) def dessine_etat(): can.delete( "all" ) for i in range (hauteur): for j in range (largeur): if cellules[i][j] = = 1 : dessine_cellule(i,j) fen.update() def trace_quadrillage(): for i in range (hauteur): can.create_line( 0 , i * e - 1 , largeur * e + 1 , i * e - 1 , fill = 'black' ) for j in range (largeur): can.create_line(j * e - 1 , 0 , j * e - 1 , hauteur * e + 1 , fill = 'black' ) def action_clic_souris(event): b = regle_taille.get() f = regle_taillebis.get() can.focus_set() x = event.x / / e y = event.y / / e for c in range (b): for d in range (f): cellules[y + c][x + d] = 1 can.create_rectangle((x + d) * e, (y + c) * e,(x + d + 1 ) * e - 1 ,(y + c + 1 ) * e - 1 , fill = "red" , width = 0 ) def action_declic_souris(event): b = regle_taille.get() f = regle_taillebis.get() can.focus_set() x = event.x / / e y = event.y / / e for c in range (b): for d in range (f): cellules[y + c][x + d] = 0 can.create_rectangle((x + d) * e, (y + c) * e,(x + d + 1 ) * e - 1 ,(y + c + 1 ) * e - 1 , fill = "white" , width = 0 ) def create_perso(): figure_perso = perso_planeur() for i in range ( int ( len (figure_perso) / 2 )): cellules[figure_perso[i * 2 + 1 ]][figure_perso[i * 2 ]] = 1 can.create_rectangle(figure_perso[i * 2 ] * e, figure_perso[i * 2 + 1 ] * e,(figure_perso[i * 2 ] + 1 ) * e - 1 ,(figure_perso[i * 2 + 1 ] + 1 ) * e - 1 , fill = "red" , width = 0 ) def perso_planeur( type ): print ( type ) if type = = 1 : figure_perso = [ 0 , 1 , 1 , 2 , 2 , 0 , 2 , 1 , 2 , 2 ] elif type = = 2 : figure_perso = [ 5 , 0 , 6 , 1 , 6 , 2 , 6 , 3 , 5 , 3 , 4 , 3 , 3 , 3 , 2 , 2 ] elif type = = 3 : figure_perso = [ 5 , 0 , 6 , 1 , 6 , 2 , 6 , 3 , 5 , 3 , 4 , 3 , 3 , 3 , 2 , 3 , 1 , 2 ] elif type = = 4 : figure_perso = [ 5 , 0 , 6 , 1 , 6 , 2 , 6 , 3 , 5 , 3 , 4 , 3 , 3 , 3 , 2 , 3 , 1 , 3 , 0 , 2 ] return figure_perso def perso_vaisseau_leger(): figure_perso = [ 5 , 0 , 6 , 1 , 6 , 2 , 6 , 3 , 5 , 3 , 4 , 3 , 3 , 3 , 2 , 2 ] return figure_perso def perso_vaisseau_moyen(): figure_perso = [ 5 , 0 , 6 , 1 , 6 , 2 , 6 , 3 , 5 , 3 , 4 , 3 , 3 , 3 , 2 , 3 , 1 , 2 ] return figure_perso def perso_vaisseau_lourd(): figure_perso = [ 5 , 0 , 6 , 1 , 6 , 2 , 6 , 3 , 5 , 3 , 4 , 3 , 3 , 3 , 2 , 3 , 1 , 3 , 0 , 2 ] return figure_perso def action_planeur(event): can.focus_set() x = event.x / / e y = event.y / / e figure_perso = perso_planeur( type ) print (figure_perso) for i in range ( int ( len (figure_perso) / 2 )): cellules[figure_perso[i * 2 + 1 ] + y][figure_perso[i * 2 ] + x] = 1 can.create_rectangle((figure_perso[i * 2 ] + x) * e, (figure_perso[i * 2 + 1 ] + y) * e,(figure_perso[i * 2 ] + x + 1 ) * e - 1 ,(figure_perso[i * 2 + 1 ] + y + 1 ) * e - 1 , fill = "red" , width = 0 ) return figure_perso e = 10 figure_perso = [] fen = Tk() can = Canvas(fen, width = 600 , height = 800 , bg = "white" ) can.grid(row = 0 , column = 0 , rowspan = 6 ) can.bind( "<Button-1>" , action_clic_souris) can.bind( "<Button-2>" , action_planeur) can.bind( "<Button-3>" , action_declic_souris) bouton_lancer = Button(fen, text = "Lancer" ,command = jeu) bouton_lancer.grid(row = 4 , column = 2 ) regle_jours = Scale(fen,orient = 'vertical' , from_ = 5000 , to = 0 , resolution = 1 , tickinterval = 500 , length = 300 ) regle_jours.grid(row = 3 , column = 2 ) regle_jours. set ( 5000 ) regle_taille = Scale(fen,orient = 'vertical' , from_ = hauteur, to = 0 , resolution = 1 , tickinterval = int (hauteur / 10 ), length = 300 ) regle_taille.grid(row = 3 , column = 3 ) regle_taille. set ( 1 ) regle_taillebis = Scale(fen,orient = 'vertical' , from_ = largeur, to = 0 , resolution = 1 , tickinterval = int (largeur / 10 ), length = 300 ) regle_taillebis.grid(row = 3 , column = 4 ) regle_taillebis. set ( 1 ) bouton_planeur = Button(fen, text = "Planeur" ,command = lambda : perso_planeur( 1 )) bouton_planeur.grid(row = 0 , column = 3 ) bouton_planeur.place(rely = 0.1 , relx = 0.82 ) bouton_vaisseau_leger = Button(fen, text = "Vaisseau Léger" ,command = lambda : perso_planeur( 2 )) bouton_vaisseau_leger.grid(row = 0 , column = 3 ) bouton_vaisseau_leger.place(rely = 0.14 , relx = 0.82 ) bouton_vaisseau_moyen = Button(fen, text = "Vaisseau Moyen" ,command = lambda : perso_planeur( 3 )) bouton_vaisseau_moyen.grid(row = 0 , column = 3 ) bouton_vaisseau_moyen.place(rely = 0.18 , relx = 0.82 ) bouton_vaisseau_lourd = Button(fen, text = "Vaisseau Lourd" ,command = lambda : perso_planeur( 4 )) bouton_vaisseau_lourd.grid(row = 0 , column = 3 ) bouton_vaisseau_lourd.place(rely = 0.22 , relx = 0.82 ) dessine_etat() trace_quadrillage() fen.mainloop() |
Error:*** Console de processus distant Réinitialisée ***
<class 'type'>
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\EduPython\App\lib\tkinter\__init__.py", line 1538, in __call__
return self.func(*args)
File "C:\Users\meder\Downloads\jeu-de-la-vie-2020-06-10-test4.py", line 150, in action_planeur
figure_perso2 = perso_planeur(type)
File "C:\Users\meder\Downloads\jeu-de-la-vie-2020-06-10-test4.py", line 132, in perso_planeur
return figure_perso
UnboundLocalError: local variable 'figure_perso' referenced before assignment
>>>
What is does mean?If you want to help me, I think I have a problem with the variables appelation into the functions, the "return", and the buttons...
Thank you.
