-
Notifications
You must be signed in to change notification settings - Fork 2
/
life-of-cells.py
140 lines (105 loc) · 4.08 KB
/
life-of-cells.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
import pygame
import random
pygame.init()
BLACK = (0, 0, 0)
WIDTH, HEIGHT = 600, 600
TILE_SIZE = 20
GRID_WIDTH = WIDTH // TILE_SIZE
GRID_HEIGHT = HEIGHT // TILE_SIZE
FPS = 60
screen = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()
def gen(num):
return set([(random.randrange(0, GRID_HEIGHT), random.randrange(0, GRID_WIDTH)) for _ in range(num)])
def draw_grid(positions, colors):
for position in positions:
col, row = position
top_left = (col * TILE_SIZE, row * TILE_SIZE)
cell_color = colors[position]
pygame.draw.rect(screen, cell_color, (*top_left, TILE_SIZE, TILE_SIZE))
for row in range(GRID_HEIGHT):
pygame.draw.line(screen, BLACK, (0, row * TILE_SIZE), (WIDTH, row * TILE_SIZE))
for col in range(GRID_WIDTH):
pygame.draw.line(screen, BLACK, (col * TILE_SIZE, 0), (col * TILE_SIZE, HEIGHT))
def adjust_grid(positions, colors):
all_neighbors = set()
new_positions = set()
new_colors = colors.copy()
for position in positions:
neighbors = get_neighbors(position)
all_neighbors.update(neighbors)
neighbors = list(filter(lambda x: x in positions, neighbors))
if len(neighbors) in [2, 3]:
new_positions.add(position)
for position in all_neighbors:
neighbors = get_neighbors(position)
neighbors = list(filter(lambda x: x in positions, neighbors))
if len(neighbors) == 3:
new_positions.add(position)
# Assign a random color to the newly born cell if it's not in the colors dictionary
if position not in new_colors:
new_colors[position] = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
return new_positions, new_colors
def get_neighbors(pos):
x, y = pos
neighbors = []
for dx in [-1, 0, 1]:
if x + dx < 0 or x + dx >= GRID_WIDTH:
continue
for dy in [-1, 0, 1]:
if y + dy < 0 or y + dy >= GRID_HEIGHT:
continue
if dx == 0 and dy == 0:
continue
neighbors.append((x + dx, y + dy))
return neighbors
def initialize_colors():
colors = {}
for col in range(GRID_WIDTH):
for row in range(GRID_HEIGHT):
colors[(col, row)] = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
return colors
def main():
running = True
playing = False
count = 0
update_freq = 120
positions = set()
colors = initialize_colors() # Initialize colors for all cells
while running:
clock.tick(FPS)
if playing:
count += 1
if count >= update_freq:
count = 0
positions, colors = adjust_grid(positions, colors)
pygame.display.set_caption("Playing" if playing else "Paused")
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
x, y = pygame.mouse.get_pos()
col = x // TILE_SIZE
row = y // TILE_SIZE
pos = (col, row)
if pos in positions:
positions.remove(pos)
else:
positions.add(pos)
colors[pos] = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
playing = not playing
if event.key == pygame.K_c:
positions = set()
colors = initialize_colors() # Reset colors for all cells
playing = False
count = 0
if event.key == pygame.K_g:
positions = gen(random.randrange(2, 5) * GRID_WIDTH)
colors = initialize_colors() # Reset colors for all cells
draw_grid(positions, colors)
pygame.display.update()
pygame.quit()
if __name__ == "__main__":
main()