-
Notifications
You must be signed in to change notification settings - Fork 0
/
pylifeGUI.py
118 lines (95 loc) · 3.11 KB
/
pylifeGUI.py
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
from tkinter import *
from CellArray import *
import time
from tkinter import filedialog
class CellCanvas(Canvas):
def __init__(self,master,*args,**kwargs):
Canvas.__init__(self, master=master, *args, **kwargs)
self.buttonFrame = Frame(master)
self.buttonFrame.pack(side = BOTTOM)
self.bind("<Button-1>", self.click)
self.gameArray = CellArray(20)
self.stepButton = Button(self.buttonFrame, text = "Step", command = self.Step)
self.runButton = Button(self.buttonFrame, text = "Run", command = self.Run)
self.clearButton = Button(self.buttonFrame, text = "Clear", command = self.Clear)
self.saveButton = Button(self.buttonFrame, text = "Save", command = self.Save)
self.loadButton = Button(self.buttonFrame, text = "Load", command = self.Load)
self.exitButton = Button(self.buttonFrame, text = "Quit", command = self.Quit)
self.stepButton.pack(side = LEFT)
self.runButton.pack(side = LEFT)
self.clearButton.pack(side = LEFT)
self.saveButton.pack(side = LEFT)
self.loadButton.pack(side = LEFT)
self.exitButton.pack(side = LEFT)
self.Update()
def Step(self, i = 0):
self.gameArray.step()
self.Update()
if(i):
if(self.runButton["text"] == "Run"):
return
self.after(500, lambda: self.Step(i))
def Run(self):
if(self.runButton["text"] == "Run"):
self.runButton["text"] = "Stop"
self.Step(1)
else:
self.runButton["text"] = "Run"
def Clear(self):
self.gameArray = CellArray(20)
self.Update()
def Quit(self):
quit()
def Update(self):
for row in range(0, self.gameArray.size, 1):
for col in range(0, self.gameArray.size, 1):
if(self.gameArray.board[row][col]):
self.create_rectangle(row*25, col*25, row*25+25, col*25+25, fill="white", outline = "black")
else:
self.create_rectangle(row*25,col*25 , row*25+25, col*25+25, fill="black", outline = "white")
def click(self, event):
i = event.x // 25
j = event.y // 25
if(self.gameArray.board[i][j] == 1):
self.gameArray.board[i][j] = 0
else:
self.gameArray.board[i][j] = 1
self.Update()
def Save(self):
file = filedialog.asksaveasfile(mode='w',
defaultextension=".pylife",
title = "Select a file to save to, or enter your own",
filetypes=(("PyLife files", "*.pylife"),("All files", "*.*") ))
if(file == None):
return
file.write(str(self.gameArray.size)+"\n")
for i in self.gameArray.board:
for j in i:
file.write(str(j) + " ")
file.write("\n")
file.close()
def Load(self):
#file = open("test", "r")
file = filedialog.askopenfile(mode ='r',
title = "Select a file to load from",
filetypes=(("PyLife files", "*.pylife"),("All files", "*.*") ))
if(file == None):
return
nums = []
lines = file.readlines()
self.gameArray = CellArray(int(lines[0].rstrip("\n")))
lines = lines[1:]
for line in lines:
line = line[:-2]
nums.append(line.split(" "))
for i in range(0, len(nums), 1):
nums[i] = list(map(int, nums[i]))
self.gameArray.board = nums
file.close()
self.Update()
if __name__ == "__main__":
window = Tk()
window.geometry("500x550")
canvas = CellCanvas(window, width=500, height=500)
canvas.pack()
window.mainloop()