-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview_custom.py
126 lines (91 loc) · 3.21 KB
/
view_custom.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
118
119
120
121
122
123
124
125
126
from tkinter import *
import model
cell_size = 5
is_running = False
def setup():
global root, grid_view, cell_size, start_button, choice
root = Tk()
root.title('The Game of Life')
grid_view = Canvas(root,
width=model.width*cell_size,
height=model.height*cell_size,
borderwidth=0,
highlightthickness=0,
bg='black')
start_button = Button(root, text='Start', width=12)
start_button.bind('<Button-1>', start_handler)
clear_button = Button(root, text='Clear', width=12)
clear_button.bind('<Button-1>', clear_handler)
choice = StringVar(root)
choice.set('Choose a Pattern')
option = OptionMenu(root, choice, 'Choose a Pattern', 'glider', 'glider gun', 'random', command=option_handler)
option.config(width=20)
grid_view.grid(row=0, columnspan=3, padx=20, pady=20)
grid_view.bind('<Button-1>', grid_handler)
start_button.grid(row=1, column=0, sticky=W, padx=20, pady=20)
option.grid(row=1, column=1, padx=20)
clear_button.grid(row=1, column=2, sticky=E, padx=20, pady=20)
def option_handler(event):
global is_running, start_button, choice
is_running = False
start_button.configure(text='Start')
selection = choice.get()
if selection == 'glider':
model.load_pattern(model.glider_pattern, 10, 10)
elif selection == 'glider gun':
model.load_pattern(model.glider_gun_pattern, 10, 10)
elif selection == 'random':
model.randomize(model.grid_model, model.width, model.height)
update()
def start_handler(event):
global is_running, start_button
if is_running:
is_running = False
start_button.configure(text='Start')
else:
is_running = True
start_button.configure(text='Pause')
update()
def clear_handler(event):
global is_running, clear_button, start_button
is_running = False
for i in range(0, model.height):
for j in range(0, model.width):
model.grid_model[i][j] = 0
start_button.configure(text='Start')
update()
def grid_handler(event):
global grid_view, cell_size
x = int(event.x / cell_size)
y = int(event.y / cell_size)
if model.grid_model[x][y] == 1:
model.grid_model[x][y] == 0
draw_cell(x, y, 'black')
else:
model.grid_model[x][y] = 1
draw_cell(x, y, 'green')
def update():
global grid_view, root, is_running
grid_view.delete(ALL)
model.next_gen()
for i in range(0, model.height):
for j in range(0, model.width):
if model.grid_model[i][j] == 1:
draw_cell(i, j, 'green')
if is_running:
root.after(100,update)
def draw_cell(row, col, color):
global grid_view, cell_size
if color == 'green':
outline = 'gray'
else :
outline = 'black'
grid_view.create_rectangle(row*cell_size,
col*cell_size,
row*cell_size+cell_size,
col*cell_size+cell_size,
fill=color, outline=outline)
if __name__ == '__main__':
setup()
update()
mainloop()