-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdla.py
More file actions
233 lines (186 loc) · 7.72 KB
/
dla.py
File metadata and controls
233 lines (186 loc) · 7.72 KB
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
import os, math, pygame, grid_system, algorithms, renderer, recorder
GRID_HEIGHT, GRID_WIDTH = (100, 100)
MIN_SPEED = 1
PIXEL_SIZE = 6
white = (252, 251, 244)
black = (20, 25, 24)
purple = (186, 85, 255)
# Pygame init
pygame.init()
# Screen init
SCREEN_WIDTH = PIXEL_SIZE * GRID_WIDTH
SCREEN_HEIGHT = PIXEL_SIZE * GRID_HEIGHT
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Diffusion Limited Aggregation")
font = pygame.font.SysFont("Classic Console Neue", 16)
#UI texts init
UI_texts = []
UI_texts.append(font.render("Space bar - pause", True, purple))
UI_texts.append(font.render("Mousewheel - speed", True, purple))
UI_texts.append(font.render("LMB - draw", True, purple))
UI_texts.append(font.render("S - screenshot", True, purple))
UI_texts.append(font.render("C - clear", True, purple))
UI_texts.append(font.render("U - change UI", True, purple))
UI_texts.append(font.render("R - start/stop recording", True, purple))
def WriteGrid(grid):
for y in range(GRID_HEIGHT):
for x in range(GRID_WIDTH):
print(grid[x][y], end = " ")
print("")
def UpdateScreen(_grid, dynamic_UI, Show_UI = True):
# Clear screen
renderer.ClearScreen(screen)
# Render grid
renderer.DrawGrid(_grid, GRID_WIDTH, GRID_HEIGHT, screen, SCREEN_WIDTH, SCREEN_HEIGHT)
# Render UI
if Show_UI:
#Left
i= 0
for text in UI_texts:
screen.blit(text, (10, 10 + text.get_height() * i))
i += 1
#Right
j = 0
for text in dynamic_UI:
screen.blit(text, (SCREEN_WIDTH - 10 - text.get_width(), 10 + text.get_height() * j))
j += 1
else:
pass
# Update screen
pygame.display.flip()
def main():
# Variables init
ticking = True
clicked = False
recording = False
scroll = 0
steps = 0
UI_mode = 1
simulation_time = 0
last_step_time = 0
frame = 0
recording_date = ""
# Grid init
grid = grid_system.GridInit(GRID_WIDTH,GRID_HEIGHT)
# Clock init
clock = pygame.time.Clock()
# Main loop
running = True
while running:
clock.tick()
if recording:
recorder.CaptureVideoFrame(screen, frame, "Recording_" + recording_date)
frame += 1
# Inputs ########################################
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
break
if event.type == pygame.KEYDOWN:
# Pause if spacebar pressed
if event.key == pygame.K_SPACE:
ticking = not ticking
# Screenshot if S key pressed
if event.key == pygame.K_s:
recorder.Screenshot(screen, "Conways-" + str(GRID_WIDTH) + "x" + str(GRID_HEIGHT))
# Clear simulation if C key pressed
if event.key == pygame.K_c:
grid = grid_system.GridInit(GRID_WIDTH, GRID_HEIGHT)
steps = 0
simulation_time = 0
last_step_time = 0
if not UI_mode == 0: UpdateScreen(grid, dynamic_texts, True)
else: UpdateScreen(grid, dynamic_texts, False)
# Change UI mode if U key pressed
if event.key == pygame.K_u:
UI_mode += 1
if UI_mode > 2: UI_mode = 0
# Toggle recording with R
if event.key == pygame.K_r:
if not recording:
recording = True
recording_date = recorder.GetDate()
dir = "Recording_" + recording_date
os.mkdir(dir)
else:
recording = False
recorder.MakeMP4(dir, dir, 60)
try:
files = os.listdir(dir)
for file in files:
file_path = os.path.join(dir, file)
if os.path.isfile(file_path):
os.remove(file_path)
os.rmdir(dir)
print("All files deleted successfully.")
except OSError:
print("Error occurred while deleting files.")
# Change simulation speed with mousewheel
if event.type == pygame.MOUSEWHEEL:
scroll += int(event.y)
if scroll < 0: scroll = 0
elif scroll > 10: scroll = 10
# Draw if LMB pressed
if pygame.mouse.get_pressed(num_buttons=3)[0] == True:
selected_pixel_x = math.floor(pygame.mouse.get_pos()[0] / PIXEL_SIZE)
selected_pixel_y = math.floor(pygame.mouse.get_pos()[1] / PIXEL_SIZE)
ticking = False
if not clicked:
state = grid_system.Switch(grid, selected_pixel_x, selected_pixel_y)
if selected_pixel_x > 0 and selected_pixel_x < GRID_WIDTH and selected_pixel_y > 0 and selected_pixel_y < GRID_HEIGHT:
# Switch pixel state
grid[selected_pixel_x][selected_pixel_y] = state
# Add pixel on screen
if UI_mode == 0: UpdateScreen(grid, dynamic_texts, False)
else: UpdateScreen(grid, dynamic_texts, True)
clicked = True
else:
clicked = False
#########################################################3
# Dynamic variables
simulation_speed = MIN_SPEED - (scroll / 10)
# Dynamic texts
dynamic_texts = []
if UI_mode == 0:
UpdateScreen(grid, dynamic_texts, False)
pass
else:
# Speed stat
try:
steps_per_second = 1/simulation_speed
actual_speed_text = font.render("Speed: " + str(int(steps_per_second)) + " steps/second", True, purple)
except:
actual_speed_text = font.render("Speed: MAX", True, purple)
dynamic_texts.append(actual_speed_text)
if UI_mode == 2:
# Steps
steps_text = font.render("Steps: " + str(steps), True, purple)
dynamic_texts.append(steps_text)
# Simulation time
time_text = font.render("Simulation time: " + str(simulation_time/1000), True, purple)
dynamic_texts.append(time_text)
# Recording
if recording:
recording_text = font.render("Recording", True, purple)
dynamic_texts.append(recording_text)
# Paused
if not ticking:
paused_text = font.render("Simulation paused", True, purple)
dynamic_texts.append(paused_text)
UpdateScreen(grid, dynamic_texts, True)
# Simulation ######################################################
if ticking:
simulation_time += clock.get_time()
now = simulation_time / 1000
if now - last_step_time >= simulation_speed:
steps += 1
last_step_time = now
if grid_system.IsGridEmpty(grid, GRID_WIDTH, GRID_HEIGHT): grid[int(GRID_WIDTH/2)][int(GRID_HEIGHT/2)] = 1
# Next step
grid = algorithms.DLA(grid, GRID_WIDTH, GRID_HEIGHT)
# Update screen
if not UI_mode == 0: UpdateScreen(grid, dynamic_texts, True)
else: UpdateScreen(grid, dynamic_texts, False)
pygame.quit()
if __name__ == '__main__':
main()