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
| import tkinter as tk import random, time
def inRange(n, start, end = 0): return start <= n <= end if end >= start else end <= n <= start
root = tk.Tk() root.title("Life") root.geometry("600x600") canvas = tk.Canvas(root,width=600,height=600,bg='black') canvas.pack()
class Game: def __init__(self): life_list = [] for x in range(60): newList = [] for y in range(60): newList.append(random.choice([0,1])) life_list.append(newList)
new_life_list = [] for x in range(60): newList = [] for y in range(60): newList.append(0) new_life_list.append(newList) self.running = 1 self.root = root self.canvas = canvas self.life_list = life_list self.new_life_list = new_life_list
self.canvas.bind("<Button-1>",self.mouseUpdate)
for x in range(1,59): for y in range(1,59): if self.life_list[x][y]: self.canvas.create_rectangle(10*x,10*y,10*x+10,10*y+10,fill="white") else: self.canvas.create_rectangle(10*x,10*y,10*x+10,10*y+10,fill="black")
def mouseUpdate(self,event): for x in range(1,59): for y in range(1,59): around = self.life_list[x-1][y-1]+self.life_list[x-1][y]+self.life_list[x-1][y+1]+self.life_list[x][y-1]+self.life_list[x][y+1]+self.life_list[x+1][y-1]+self.life_list[x+1][y]+self.life_list[x+1][y+1] if around == 3: self.new_life_list[x][y] = 1 elif around == 2: self.new_life_list[x][y] = self.life_list[x][y] else: self.new_life_list[x][y] = 0
cellX = int(event.x / 10) cellY = int(event.y / 10) if inRange(cellX, 1, 59) and inRange(cellY, 1, 59): self.new_life_list[cellX][cellY] = 1
for x in range(1,59): for y in range(1,59): if self.new_life_list[x][y]: self.canvas.create_rectangle(10*x,10*y,10*x+10,10*y+10,fill="white") else: self.canvas.create_rectangle(10*x,10*y,10*x+10,10*y+10,fill="black") self.life_list = self.new_life_list self.new_life_list = [] for x in range(60): newList = [] for y in range(60): newList.append(0) self.new_life_list.append(newList)
def update(self): for x in range(1,59): for y in range(1,59): around = self.life_list[x-1][y-1]+self.life_list[x-1][y]+self.life_list[x-1][y+1]+self.life_list[x][y-1]+self.life_list[x][y+1]+self.life_list[x+1][y-1]+self.life_list[x+1][y]+self.life_list[x+1][y+1] if around == 3: self.new_life_list[x][y] = 1 elif around == 2: self.new_life_list[x][y] = self.life_list[x][y] else: self.new_life_list[x][y] = 0
for x in range(1,59): for y in range(1,59): if self.new_life_list[x][y]: self.canvas.create_rectangle(10*x,10*y,10*x+10,10*y+10,fill="white") else: self.canvas.create_rectangle(10*x,10*y,10*x+10,10*y+10,fill="black") self.life_list = self.new_life_list self.new_life_list = [] for x in range(60): newList = [] for y in range(60): newList.append(0) self.new_life_list.append(newList)
def mainloop(self): self.update() self.root.update() time.sleep(0.1) self.canvas.delete("all")
g = Game() def allStop(): try: g.root.destroy() btnRoot.destroy() except: btnRoot.destroy() def nonstop(): g.__init__() g.running = 1 while g.running: g.mainloop() def stop(): g.running = 0 g.update()
btnRoot = tk.Tk() btn1 = tk.Button(btnRoot, text="Start Random Nonstop",command=nonstop) btn1.pack() btn2 = tk.Button(btnRoot, text="Stop Random Nonstop",command=stop) btn2.pack() btnRoot.protocol("WM_DELETE_WINDOW", allStop) btnRoot.mainloop()
|