#!/usr/bin/env python #------------------------------------------------------------------------------ # life100x75.py - by C.Fin - Python2&3 #------------------------------------------------------------------------------ try: import tkinter as tk except ImportError: import Tkinter as tk 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 = tk.Tk() self.form1.title("Life %s * %s" % (XGRID,YGRID)) self.form1.resizable(False,False) self.canvas1 = tk.Canvas( self.form1, highlightthickness=0, width=8*XGRID, height=8*YGRID, bg=OFF_COLOR) self.canvas1.grid(row=1, column=0) self.frame1 = tk.Frame( self.form1, width=8*XGRID, height=40, relief="raise", bd=2) self.frame1.grid_propagate(0) self.frame1.grid(row=0, column=0) self.bt_onestep = tk.Button( self.frame1, width=10, text="One step", command=self.cmd_onestep) self.bt_onestep.grid(row=0, column=0, padx=4) self.bt_start_stop = tk.Button( self.frame1, width=10, text="START", command=self.cmd_stasto) self.bt_start_stop.grid(row=0, column=1, padx=4) self.frame2 = tk.Frame( self.frame1, bd=2, relief="groove", width=2, height=35) self.frame2.grid(row=0, column=2, padx=8) self.bt_edit = tk.Button( self.frame1, width=10, text="Edit matrix", command=self.cmd_edit) self.bt_edit.grid(row=0, column=3, padx=4) self.bt_clear = tk.Button( self.frame1, width=10, text="Cancella", command=self.cmd_cancella) self.bt_clear.grid(row=0, column=4, padx=4) self.frame3 = tk.Frame( self.frame1, bd=2, relief="groove", width=2, height=35) self.frame3.grid(row=0, column=5, padx=16) self.bt_copia = tk.Button( self.frame1, width=10, text="Copia", command=self.cmd_copia) self.bt_copia.grid(row=0, column=6, padx=4) self.bt_incolla = tk.Button( self.frame1, width=10, text="Incolla", 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 range(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" else: self._start = True self.bt_start_stop["text"] = "STOP" 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._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.canvas1.delete("all") self.matrice = [ [0]*YGRID for x in range(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 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 range(XGRID): for iy in range(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]: return 1 if somma==3 else 0 else: return 0 if somma<2 or somma>3 else 1 #-------------------------------------------------------------------------- def disegna(self): self.canvas1.delete("all") for ix in range(XGRID): x1 = ix * 8 x1p6 = x1 + 6 for iy in range(YGRID): if self.matrice[ix][iy]: y1 = iy * 8 self.canvas1.create_oval( x1, y1, x1p6, y1+6, fill=LIFE_COLOR, outline=LIFE_COLOR) #------------------------------------------------------------------------------ appl = TAppl() appl.form1.mainloop()