-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheden.py
148 lines (125 loc) · 3.93 KB
/
eden.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
"""The Eden app."""
from Tkinter import Frame, Tk
from simulation import Simulation
from ui import UI
import utility
from utility import log
from operator import attrgetter
import settings
class EdenApp():
"""The EdenApp class is the overall app.
When it runs it creates two objects:
The simulation, that runs the actual simulation.
The ui, that presents visuals of the simulation on the screen.
"""
def __init__(self, master):
"""Create the app."""
self.master = master
# create the simulation object
utility.log_welcome()
log("> Creating simulation")
self.simulation = Simulation()
# create the app
log("> Creating UI")
master.wm_title("Eden")
self.frame = Frame(master)
self.frame.grid()
# create the ui
self.ui = UI(self.master, self, self.frame)
self.create_key_bindings()
self.running = False
self.time = 0
def create_key_bindings(self):
"""Set up key bindings."""
def leftKey(event):
self.rotate_map(-10.0)
def rightKey(event):
self.rotate_map(10.0)
def upKey(event):
self.change_time_step(1)
def downKey(event):
self.change_time_step(-1)
def spaceKey(event):
self.toggle_running()
self.master.bind('<Left>', leftKey)
self.master.bind('<Right>', rightKey)
self.master.bind('<Up>', upKey)
self.master.bind('<Down>', downKey)
self.master.bind('<space>', spaceKey)
def step(self):
"""Advance one step in time."""
self.time += settings.time_step_size
self.ui.update_time_label(self.time)
self.simulation.step()
self.ui.paint_tiles()
self.master.update()
def rotate_map(self, degrees):
"""Spin the map."""
for c in self.simulation.world.cells:
c.longitude += degrees
if c.longitude < 0:
c.longitude += 360.0
elif c.longitude >= 360.0:
c.longitude -= 360.0
self.simulation.world.cells = sorted(
self.simulation.world.cells,
key=attrgetter("latitude", "longitude"))
self.ui.paint_tiles()
def change_time_step(self, direction):
"""Change the time_step_size."""
time_steps = [
1,
10,
60,
60*10,
60*60,
60*60*6,
60*60*24,
60*60*24*7,
60*60*24*30,
60*60*24*365,
60*60*24*365*10,
60*60*24*365*50,
60*60*24*365*100,
60*60*24*365*500,
60*60*24*365*1000,
60*60*24*365*10000,
60*60*24*365*100000,
60*60*24*365*1000000
]
step_descriptions = [
"1s",
"10s",
"1 minute",
"10 minutes",
"1 hour",
"6 hours",
"1 day",
"7 days",
"30 days",
"1 year",
"10 years",
"50 years",
"100 years",
"500 years",
"1,000 years",
"10,000 years",
"100,000 years",
"1,000,000 years"
]
index = time_steps.index(settings.time_step_size)
if direction > 0 and index != len(time_steps) - 1:
settings.time_step_size = time_steps[index + 1]
settings.time_step_description = step_descriptions[index + 1]
elif direction < 0 and index != 0:
settings.time_step_size = time_steps[index - 1]
settings.time_step_description = step_descriptions[index - 1]
self.ui.update_time_label(self.time)
def toggle_running(self):
"""Start/stop the simulation."""
self.running = not self.running
while self.running:
self.step()
root = Tk()
eden = EdenApp(master=root)
root.mainloop()