#-------------------------------------------------------------------- # life.py - by Claudio Fin marzo 2008 #-------------------------------------------------------------------- import Tkinter import copy import random #-------------------------------------------------------------------- XGRID = 100 YGRID = 75 MINXCURS = 0 MAXXCURS = XGRID - 1 MINYCURS = 0 MAXYCURS = YGRID - 1 ON_COLOR = "red" OFF_COLOR = "blue" LIFE_COLOR = "yellow" #-------------------------------------------------------------------- class TAppl: def __init__(self): self.form1 = Tkinter.Tk() self.form1.title("Life %s * %s" % (XGRID,YGRID)) self.form1.resizable(False,False) self.frame1 = Tkinter.Frame(self.form1, width=8*XGRID+5, height=40, relief="raise", bd=2) self.frame1.grid_propagate(0) self.frame1.grid(row=0, column=0) self.canvas1 = Tkinter.Canvas(self.form1, width=8*XGRID+1, height=8*YGRID+1, bg=OFF_COLOR) self.canvas1.grid(row=1, column=0) self.bt_onestep = Tkinter.Button( self.frame1, text="One step", width=15, command=self.cmd_onestep) self.bt_onestep.grid(row=0, column=0, padx=4) self.bt_start_stop = Tkinter.Button( self.frame1, text="START", width=15, command=self.cmd_stasto) self.bt_start_stop.grid(row=0, column=1, padx=4) self.frame2 = Tkinter.Frame(self.frame1, bd=2, relief="groove", width=2, height=35) self.frame2.grid(row=0, column=2, padx=8) self.bt_edit = Tkinter.Button( self.frame1, text="Edit matrix", width=15, command=self.cmd_edit) self.bt_edit.grid(row=0, column=3, padx=4) self.bt_clear = Tkinter.Button( self.frame1, text="Cancella", width=15, command=self.cmd_cancella) self.bt_clear.grid(row=0, column=4, padx=4) self.frame3 = Tkinter.Frame(self.frame1, bd=2, relief="groove", width=2, height=35) self.frame3.grid(row=0, column=5, padx=16) self.bt_copia = Tkinter.Button( self.frame1, text="Copia", width=15, command=self.cmd_copia) self.bt_copia.grid(row=0, column=6, padx=4) self.bt_incolla = Tkinter.Button( self.frame1, text="Incolla", width=15, command=self.cmd_incolla) self.bt_incolla.grid(row=0, column=7, padx=4) self.bindform() # crea matrice XGRID*YGRID: matrice[x][y] self.matrice = [ [0]*YGRID for x in xrange(XGRID) ] self.matrice_p = copy.deepcopy(self.matrice) self.matrice_t = copy.deepcopy(self.matrice) # variabili self._start = self._edit = False self._xcurs = self._ycurs = 0 self._curs_color = ON_COLOR def cmd_stasto(self): self._edit = False if self._start: self._start = False self.bt_start_stop["text"] = "START" self.bt_start_stop["bg"] = "SystemButtonFace" else: self._start = True self.bt_start_stop["text"] = "STOP" self.bt_start_stop["bg"] = "red" self.canvas1.delete("all") self.life_run() def cmd_onestep(self): self._edit = False self.life_step() def cmd_edit(self): if self._edit : return self._start = False self.bt_start_stop["text"] = "START" self.bt_start_stop["bg"] = "SystemButtonFace" self._edit = True self._xcurs = MINXCURS self._ycurs = MINYCURS self.creacurs() self.curs_blink() def cmd_copia(self): self.matrice_p = copy.deepcopy(self.matrice) def cmd_incolla(self): self.canvas1.delete("all") self.matrice = copy.deepcopy(self.matrice_p) self.disegna() self.canvas1.update() if self._edit: self.creacurs() def cmd_cancella(self): self._start = False self.bt_start_stop["text"] = "START" self.bt_start_stop["bg"] = "SystemButtonFace" self.canvas1.delete("all") self.matrice = [ [0]*YGRID for x in xrange(XGRID) ] if self._edit: self.creacurs() #-------------------------------------------------------------------- def bindform(self): self.form1.bind('', self.c_up) self.form1.bind('', self.c_dn) self.form1.bind('', self.c_sx) self.form1.bind('', self.c_dx) self.form1.bind('', self.c_pup) self.form1.bind('', self.c_pdn) self.form1.bind('', self.c_psx) self.form1.bind('', self.c_pdx) def c_up(self,event): if not self._edit: return self.matrice[self._xcurs][self._ycurs] = 0 self.disegna() self._ycurs -= 1 if self._ycursMAXYCURS: self._ycurs = MINYCURS self.creacurs() def c_sx(self,event): if not self._edit: return self.matrice[self._xcurs][self._ycurs] = 0 self.disegna() self._xcurs -= 1 if self._xcursMAXXCURS: self._xcurs = MINXCURS self.creacurs() def c_pup(self,event): if not self._edit: return self.matrice[self._xcurs][self._ycurs] = 1 self.disegna() self._ycurs -= 1 if self._ycursMAXYCURS: self._ycurs = MINYCURS self.creacurs() def c_psx(self,event): if not self._edit: return self.matrice[self._xcurs][self._ycurs] = 1 self.disegna() self._xcurs -= 1 if self._xcursMAXXCURS: self._xcurs = MINXCURS self.creacurs() def creacurs(self): x1 = self.xymap(self._xcurs) y1 = self.xymap(self._ycurs) self._curs_color = ON_COLOR self.cursore = self.canvas1.create_oval(x1, y1, x1+6, y1+6, fill=ON_COLOR, outline=ON_COLOR ) def xymap(self, z): return 3 + z*8 def curs_blink(self): if not self._edit : return self.canvas1.itemconfig(self.cursore, fill=self._curs_color, outline=self._curs_color) if self._curs_color==ON_COLOR: self._curs_color=OFF_COLOR else: self._curs_color=ON_COLOR self.canvas1.after(150, self.curs_blink) #-------------------------------------------------------------------- def life_run(self): if not self._start: return self.life_step() self.canvas1.after(20, self.life_run) def life_step(self): for ix in xrange(XGRID): for iy in xrange(YGRID): self.matrice_t[ix][iy] = self.lione(ix,iy) self.matrice = copy.deepcopy(self.matrice_t) self.disegna() def lione(self,ix,iy): ym1 = iy - 1 if ym1MAXYCURS: yp1 = MINYCURS xm1 = ix - 1 if xm1MAXXCURS: xp1 = MINXCURS somma = self.matrice[xm1][ym1] + \ self.matrice[ix][ym1] + \ self.matrice[xp1][ym1] + \ self.matrice[xm1][iy] + \ self.matrice[xp1][iy] + \ self.matrice[xm1][yp1] + \ self.matrice[ix][yp1] + \ self.matrice[xp1][yp1] if not self.matrice[ix][iy]: if somma==3: return 1 return 0 else: if somma<2 or somma>3: return 0 return 1 #-------------------------------------------------------------------- def disegna(self): self.canvas1.delete("all") for ix in xrange(XGRID): x1 = 3 + ix*8 x1p6 = x1 + 6 for iy in xrange(YGRID): if self.matrice[ix][iy]: y1 = 3 + iy*8 self.canvas1.create_oval(x1, y1, x1p6, y1+6, fill=LIFE_COLOR, outline=LIFE_COLOR) #-------------------------------------------------------------------- def main(): appl = TAppl() appl.form1.mainloop() #-------------------------------------------------------------------- if __name__ == '__main__': main()