def change_color(self,color): #self.Canvas.delete(self) if color=="white": self.liveCell=True else: self.liveCell=False self.print()
def print(self): if self.liveCell: self.Canvas.create_rectangle(int(self.x1), int(self.y1), int(self.x2), int(self.y2), fill = "white", outline="") else: self.Canvas.create_rectangle(int(self.x1), int(self.y1), int(self.x2), int(self.y2), fill = "grey", outline="")
Global Variables
1 2 3 4 5 6 7 8 9 10 11 12 13 14
x_pos=0 #mouse posiotn recording y_pos=0
liveCell_list=[] #life cell list newBorn_list=[] #has 3 neighbors but no live cell right now (new live cell in next round)
cell_list=[] #2-D list memory cell_length=10 #length of singal cell cell_length_max=40 cell_length_min=3 cell_gap=0.1*cell_length #gap in two cells row_max=0 column_max=0 beginning=False
def wheel_rolling(event): global cell_list direction=event.delta rate=0.9
#check length border if direction>0: if abs(cell_list[0][0].x1-cell_list[0][0].x2)<cell_length_max: #not reach minimum length Can.delete(ALL) #remove all components rate=2-rate #1.1 times dx=event.x*(1-rate) #mouse position shift, zoom figure out from where the mouse is pointed dy=event.y*(1-rate)
for row in cell_list: for cell in row: cell.ZoomPosition(dx,dy,rate) #editing and painting cell #print("Zoom out") else: print("maximum size") elif direction<0: if abs(cell_list[0][0].x1-cell_list[0][0].x2)>cell_length_min: #not reach maximum length Can.delete(ALL) #remove all components rate=rate #0.9 times dx=event.x*(1-rate) dy=event.y*(1-rate)
for row in cell_list: for cell in row: cell.ZoomPosition(dx,dy,rate) #editing and painting cell #print("Zoom in") else: print("minimum size")
Left Key Pressing
查找cell坐标并switch指定cell颜色和状态,edit liveCell_list
1 2 3 4 5 6 7 8 9 10 11
def left_press(event): global cell_list global liveCell_list
def create_cell_matrix(row_num,column_num,cell_length,Can): global row_max global column_max global cell_list row_max=row_num #record max row number into global variable column_max=column_num
Can.delete(ALL)
new_cell_list=[[0 for _ in range(column_num)] for _ in range(row_num)] #2-D list init shift_length=cell_length+cell_gap pre_x=2 #start positoin on canvas pre_y=2
for row in range(row_num): for column in range(column_num): new_cell_list[row][column]=cell(pre_x, pre_y, pre_x+cell_length, pre_y+cell_length, "grey", Can) #create cell object in 2-D list system pre_x+=shift_length #move to next position pre_x=2 pre_y+=shift_length #move to next position
else: #draw vertical lines line_height=cell_list[row_max-1][column_max-1].y2 x1=cell_list[0][0].x1-line_width for i in range(column_max): Can.create_rectangle(x1, 0, x1+line_width, line_height, fill = "white", outline="") x1+=cell_distance
#draw horizonal lines line_height=cell_list[row_max-1][column_max-1].x2 y1=cell_list[0][0].y1-line_width for j in range(row_max): Can.create_rectangle(0, y1, line_height, y1+line_width, fill = "white", outline="") y1+=cell_distance
#paint white cell for row,column in liveCell_list: cell_list[row][column].print()