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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
|
class TkPeriodicCallMixin(object): ''' periodic call ''' interval = 300 running = False
def _loop(self): self.polling_callback() if self.running: self.after(self.interval, self._loop)
def polling_callback(self, *args, **kargs): ''' to be rewrite ''' print self, id(self)
def start_polling(self): self.running = True self._loop()
def stop_polling(self): self.running = False
class Grid(Canvas, TkPeriodicCallMixin): ''' the drawing widgets ''' COLOR_MAP = { 0: '#FFDDDD', 8: '#FF0000', 7: '#EE3333', 6: '#DD4444', 5: '#BB5555', 4: '#996666', 3: '#CCCC00', 2: '#FFEE00', 1: '#339999' }
def __init__(self, *args, **kargs): kargs['width'] = 600 kargs['height'] = 400 kargs['bg'] = 'grey' Canvas.__init__(self, *args, **kargs)
self._size = (60, 40) self.conway_map = Conway_Map(60, 40) self.draw() self.start_polling()
def polling_callback(self):
self.conway_map.update() self.draw()
def draw(self): self.delete(ALL) self.create_rectangle(0, 0, 600, 400, fill='grey') self.create_rectangle(10, 10, 590, 390, fill='white') for x in xrange(60): self.create_line( 10+10*x, 10, 10+10*x, 390, fill="grey", dash=(4, 4)) for y in xrange(40): self.create_line( 10, 10 + 10*y, 590, 10 + 10*y, fill="grey", dash=(4, 4)) for i in xrange(60): for j in xrange(40): if self.conway_map.map[i][j]: color = self.COLOR_MAP[self.conway_map.around_count[i][j]] self.create_rectangle( 10*i + 10, 10*j + 10, 10*i + 20, 10*j + 20, fill=color)
def god_click(self, x, y): ''' callback click by the god(user), x y are clicks by the user, ''' if x < 590 and x > 10 and y > 10 and y < 390: i, j = (x-15)/10, (y-15)/10 self.create_rectangle( 10*i + 10, 10*j + 10, 10*i + 20, 10*j + 20, fill='black') self.conway_map.god_click(i, j)
def right_click(self, x, y): ''' callback (right click) click by the god, x y are clicks by the user, ''' if x < 590 and x > 10 and y > 10 and y < 390: i, j = (x-15)/10, (y-15)/10 self.create_rectangle( 10*i + 10, 10*j + 10, 10*i + 20, 10*j + 20, fill='white') self.conway_map.map[i][j] = 0
class GridWindow(object): ''' main window ''' def __init__(self): self.root = Tk() self.root.minsize(650, 450) self.root.title("conway's game of life, python version")
descrition = u"conway 's game of life\n\ powered by Dawn http://orzdawn.com\n\n\ left click on the grid to make new lives\n\ right click to kill lives" self.lb2 = Label(text=descrition) self.lb2.pack()
self.grid = Grid(self.root) self.grid.pack() self.grid.bind("<Button-1>", self.left_click) self.grid.bind("<B1-Motion>", self.left_click) self.grid.bind("<Button-2>", self.right_click) self.grid.bind("<B2-Motion>", self.right_click)
self.paused = False self.go_pause_btn = Button(self.root, text=u"|| pause", command=self.go_pause) self.go_pause_btn.pack()
self.one_step_btn = Button(self.root, text=u'go one step', command=self.one_step) self.one_step_btn.pack()
self.clear_all_btn = Button(self.root, text=u'clear all', command=self.clear_all) self.clear_all_btn.pack()
self.lb1 = Label(text="update interval(ms):") self.lb1.pack() self.speed_scale = Scale(self.root, from_=10, to=1000, orient=HORIZONTAL, command=self.speed_change) self.speed_scale.set(300) self.speed_scale.pack()
def speed_change(self, val): print 'speed_scale=%r' % val self.grid.interval = int(val)
def go_pause(self): if self.paused: self.grid.start_polling() self.paused = False self.go_pause_btn['text'] = u'|| pause' else: self.grid.stop_polling() self.paused = True self.go_pause_btn['text'] = u'> go'
def one_step(self): self.grid.polling_callback()
def clear_all(self): self.grid.conway_map.clear_all() self.grid.draw()
def run(self): self.root.mainloop()
def left_click(self, event): print "left,mouse pos: %r,%r" % (event.x, event.y), self.grid.god_click(event.x, event.y)
def right_click(self, event): print "right,mouse pos: %r,%r" % (event.x, event.y), self.grid.right_click(event.x, event.y)
class Conway_Map(object): ''' data model ''' def __init__(self, x, y): self.size = (x, y) self.map = [[0 for i in range(y)] for j in range(x)]
self.around_count = [[0 for i in range(y)] for j in range(x)] print len(self.map), ' ', len(self.map[0])
def update(self): ''' update the self.map ''' self.count_map() for i in xrange(60): for j in xrange(40): if self.around_count[i][j] == 2: pass elif self.around_count[i][j] == 3: self.map[i][j] = 1 else: self.map[i][j] = 0 self.count_map()
def count_map(self): ''' count the map. store result in self.around_count around every block, count the alive blocks around it. ''' for i in xrange(60): for j in xrange(40): arounds = [ (i, (j+1) % 40), (i, (j-1) % 40), ((i-1) % 60, j), ((i+1) % 60, j), ((i+1) % 60, (j+1) % 40), ((i+1) % 60, (j-1) % 40), ((i-1) % 60, (j+1) % 40), ((i-1) % 60, (j-1) % 40), ]
self.around_count[i][j] = 0 for i_r, j_r in arounds: self.around_count[i][j] += self.map[i_r][j_r]
def god_click(self, x, y): ''' callback ''' print "grid_pos = (%d,%d)" % (x, y) self.map[x][y] = 1
def clear_all(self): for i in xrange(60): for j in xrange(40): self.map[i][j] = 0
if __name__ == "__main__": win = GridWindow() win.run()
|