-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
315 lines (255 loc) · 9.98 KB
/
main.py
File metadata and controls
315 lines (255 loc) · 9.98 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import json
import math
import pygame
from pygame import Color
from pygame import Surface
from pygame import Vector2
# changeable constants
WINDOW_TITLE = "C.G.o.L"
FPS = 60.0
COLOR_BG = Color(64, 64, 64)
UNIT_COLORS = [Color(0, 0, 0),
Color(255, 255, 255)]
UNIT_ARRAY_SIZE = (50, 50)
UNIT_SIZE = 14
UNIT_BORDER = 1
def seconds_to_frames(seconds: float) -> int:
"""returns the amount of frames equivalent to the given seconds"""
return math.ceil(seconds * FPS)
# dependent constants
CLOCK = pygame.time.Clock()
UNIT_SIZE_HALF = math.ceil(UNIT_SIZE / 2)
UNIT_SIZE_VECTOR = Vector2(UNIT_SIZE)
UNIT_SIZE_VECTOR_HALF = UNIT_SIZE_VECTOR / 2
UNIT_ARRAY_SIZE_RANGE = (range(UNIT_ARRAY_SIZE[0]),
range(UNIT_ARRAY_SIZE[1]))
UNIT_BORDER_VECTOR = Vector2(UNIT_BORDER)
TOTAL_UNIT_SIZE = UNIT_SIZE + UNIT_BORDER
TOTAL_UNIT_SIZE_VECTOR = Vector2(TOTAL_UNIT_SIZE)
class Unit:
def __init__(self, position: Vector2):
self.active = False
self.active_last = False
self.position = position
def draw(self, surface: Surface) -> None:
"""draws the unit to the given surface at its position"""
color = UNIT_COLORS[1] if self.active else UNIT_COLORS[0]
match draw_mode:
case 0:
pygame.draw.rect(surface, color, (self.position, UNIT_SIZE_VECTOR))
case 1:
pygame.draw.rect(surface, color, (self.position, TOTAL_UNIT_SIZE_VECTOR))
case 2:
pygame.draw.circle(surface, color, self.position + UNIT_SIZE_VECTOR_HALF, UNIT_SIZE_HALF)
def redraw_all() -> None:
"""clears screen and adds all units to dirty_array to be redrawn"""
# refill surface
surface.fill(COLOR_BG)
# mark all units dirty
dirty_array.clear()
for _unit in unit_array:
for unit in _unit:
dirty_array.append(unit)
def update_units() -> None:
"""handles updating all units"""
for y in UNIT_ARRAY_SIZE_RANGE[1]:
for x in UNIT_ARRAY_SIZE_RANGE[0]:
# check for neighbors
unit_neighbors = 0
for offset_y in range(3):
for offset_x in range(3):
# skip check if offset is centered
if offset_x == 1 and offset_y == 1:
continue
# neighbor coordinates
check_x = x + (offset_x - 1)
check_y = y + (offset_y - 1)
# skip check if out of bounds
if check_x < 0 or check_y < 0 or \
check_x > UNIT_ARRAY_SIZE[0] - 1 or \
check_y > UNIT_ARRAY_SIZE[1] - 1:
continue
# if unit at check position was active, increment neighbor count
if unit_array[check_y][check_x].active_last:
unit_neighbors += 1
# get current unit
unit = unit_array[y][x]
# if unit is active and has too few or too many neighbors
if unit.active:
if unit_neighbors < 2 or unit_neighbors > 3:
# make unit inactive
unit.active = False
dirty_array.append(unit)
# if unit is not active and has exactly three neighbors
else:
if unit_neighbors == 3:
# make unit active
unit.active = True
dirty_array.append(unit)
def deactivate_all_units() -> None:
"""iterates through and deactivates all units"""
for _unit in unit_array:
for unit in _unit:
unit.active = False
unit.active_last = False
redraw_all()
def handle_slot(slot: int) -> None:
"""handles saving/loading to a file"""
save_name = f"save_{slot}.json"
# if holding shift, save to slot
if input_shift:
with open(save_name, "w") as file:
json.dump([[1 if unit.active else 0 for unit in _unit] for _unit in unit_array], file, indent = 2)
# if not holding shift, load slot
else:
try:
with open(save_name) as file:
unit_active_list = json.load(file)
for y in UNIT_ARRAY_SIZE_RANGE[1]:
for x in UNIT_ARRAY_SIZE_RANGE[0]:
unit = unit_array[y][x]
try:
unit_active = unit_active_list[y][x] == 1
unit.active = unit_active
unit.active_last = unit_active
except:
unit.active = False
unit.active_last = False
global simulating
simulating = False
redraw_all()
except:
deactivate_all_units()
# variables
running = True
simulating = False
unit_array = [[Unit(Vector2((x * TOTAL_UNIT_SIZE) + UNIT_BORDER,
(y * TOTAL_UNIT_SIZE) + UNIT_BORDER))
for x in UNIT_ARRAY_SIZE_RANGE[0]]
for y in UNIT_ARRAY_SIZE_RANGE[1]]
dirty_array = []
surface_size = TOTAL_UNIT_SIZE_VECTOR.copy()
surface_size.x *= UNIT_ARRAY_SIZE[0]
surface_size.y *= UNIT_ARRAY_SIZE[1]
surface_size += Vector2(UNIT_BORDER)
draw_mode = 0
input_shift = False
color_swap = False
# create surface
pygame.init()
pygame.display.set_caption(WINDOW_TITLE)
surface = pygame.display.set_mode(surface_size)
redraw_all()
# loop
while running:
# handle input
for event in pygame.event.get():
match event.type:
case pygame.QUIT:
# end script
running = False
case pygame.KEYDOWN:
match event.key:
case pygame.K_END:
# end script
running = False
case pygame.K_PAGEDOWN:
# minimize display
pygame.display.iconify()
case pygame.K_SPACE:
# step through one unit update
if not simulating:
update_units()
case pygame.K_RETURN | pygame.K_KP_ENTER:
# toggle simulation
simulating = not simulating
case pygame.K_TAB:
# toggle draw mode
if input_shift:
draw_mode -= 1
if draw_mode == -1:
draw_mode = 2
else:
draw_mode += 1
if draw_mode == 3:
draw_mode = 0
# redraw all units
redraw_all()
case pygame.K_LSHIFT | pygame.K_RSHIFT:
# mark shift as held
input_shift = True
case pygame.K_ESCAPE:
# clear screen
deactivate_all_units()
case pygame.K_BACKQUOTE:
# swap units color scheme
_color_hold = UNIT_COLORS[0]
UNIT_COLORS[0] = UNIT_COLORS[1]
UNIT_COLORS[1] = _color_hold
redraw_all()
# handle saving and loading slots with function keys
case pygame.K_F1:
handle_slot(1)
case pygame.K_F2:
handle_slot(2)
case pygame.K_F3:
handle_slot(3)
case pygame.K_F4:
handle_slot(4)
case pygame.K_F5:
handle_slot(5)
case pygame.K_F6:
handle_slot(6)
case pygame.K_F7:
handle_slot(7)
case pygame.K_F8:
handle_slot(8)
case pygame.K_F9:
handle_slot(9)
case pygame.K_F10:
handle_slot(10)
case pygame.K_F11:
handle_slot(11)
case pygame.K_F12:
handle_slot(12)
case pygame.KEYUP:
match event.key:
case pygame.K_LSHIFT | pygame.K_RSHIFT:
# mark shift as released
input_shift = False
case pygame.MOUSEBUTTONDOWN:
match event.button:
case pygame.BUTTON_LEFT:
# disable simulation
if simulating:
simulating = False
# toggle active state of selected unit
mouse_pos = pygame.mouse.get_pos()
mouse_pos = (int(mouse_pos[0] / TOTAL_UNIT_SIZE),
int(mouse_pos[1] / TOTAL_UNIT_SIZE))
# check click was in bounds
if mouse_pos[0] < UNIT_ARRAY_SIZE[0] and \
mouse_pos[1] < UNIT_ARRAY_SIZE[1]:
# find selected unity, modify active state, and add to dirty list
mouse_unit = unit_array[mouse_pos[1]][mouse_pos[0]]
mouse_unit.active = not mouse_unit.active
dirty_array.append(mouse_unit)
# update units if simulating
if simulating:
update_units()
# draw
if len(dirty_array) != 0:
for unit in dirty_array:
# redraw dirty units
unit.draw(surface)
# update dirty units last active state
unit.active_last = unit.active
# clear dirty unit array
dirty_array.clear()
# update display
pygame.display.flip()
# tick fps clock
CLOCK.tick(FPS)
# loops again if running is true
# quit pygame
pygame.quit()