forked from matheusgomes28/pygame-life
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pygame_life.py
181 lines (132 loc) · 4.93 KB
/
pygame_life.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
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
# """
# Pygame of life module. Contains the short engine
# to simluate the grid of life.
# """
# import sys
# import time
# from collections import defaultdict
# from copy import deepcopy
# import pygame
# from example_grids import GOSPER_GLIDER
# from grid_defs import Grid, Neighbours
# def get_neighbours(grid: Grid, x: int, y: int) -> Neighbours:
# """
# Gets the neighbour states for a particular cell in
# (x, y) on the grid.
# """
# offsets = [(-1, -1), (0, -1), (1, -1), (-1, 0), (1, 0), (-1, 1), (0, 1), (1, 1)]
# possible_neighbours = {(x + x_add, y + y_add) for x_add, y_add in offsets}
# alive = {(pos[0], pos[1]) for pos in possible_neighbours if pos in grid.cells}
# return Neighbours(alive, possible_neighbours - alive)
# def update_grid(grid: Grid) -> Grid:
# """
# Given a grid, this function returns the next iteration
# of the game of life.
# """
# new_cells = deepcopy(grid.cells)
# undead = defaultdict(int)
# for x, y in grid.cells:
# alive_neighbours, dead_neighbours = get_neighbours(grid, x, y)
# if len(alive_neighbours) not in [2, 3]:
# new_cells.remove((x, y))
# for pos in dead_neighbours:
# undead[pos] += 1
# for pos, _ in filter(lambda elem: elem[1] == 3, undead.items()):
# new_cells.add((pos[0], pos[1]))
# return Grid(grid.dim, new_cells)
# def draw_grid(screen: pygame.Surface, grid: Grid) -> None:
# """
# This function draws the game of life on the given
# pygame.Surface object.
# """
# cell_width = screen.get_width() / grid.dim.width
# cell_height = screen.get_height() / grid.dim.height
# border_size = 2
# for x, y in grid.cells:
# pygame.draw.rect(
# screen,
# (255, 0, 0),
# (
# x * cell_width + border_size,
# y * cell_height + border_size,
# cell_width - border_size,
# cell_height - border_size,
# ),
# )
# def main():
# """
# Main entry point
# """
# grid = GOSPER_GLIDER
# pygame.init()
# screen = pygame.display.set_mode((600, 400))
# while True:
# if pygame.QUIT in [e.type for e in pygame.event.get()]:
# sys.exit(0)
# screen.fill((0, 0, 0))
# draw_grid(screen, grid)
# grid = update_grid(grid)
# pygame.display.flip()
# time.sleep(0.1)
# if __name__ == "__main__":
# main()
import sys
import time
from collections import defaultdict
from copy import deepcopy
import pygame
from example_grids import GOSPER_GLIDER
from grid_defs import Grid, Neighbours
def get_neighbours(grid: Grid, x: int, y: int) -> Neighbours:
offsets = [(-1, -1), (0, -1), (1, -1), (-1, 0), (1, 0), (-1, 1), (0, 1), (1, 1)]
possible_neighbours = {(x + x_add, y + y_add) for x_add, y_add in offsets}
alive = {(pos[0], pos[1]) for pos in possible_neighbours if pos in grid.cells}
return Neighbours(alive, possible_neighbours - alive)
def update_grid(grid: Grid) -> Grid:
new_cells = deepcopy(grid.cells)
"""
Given a grid, this function returns the next iteration
of the game of life.
"""
undead = defaultdict(int)
for x, y in grid.cells:
alive_neighbours, dead_neighbours = get_neighbours(grid, x, y)
if len(alive_neighbours) not in [2, 3]:
new_cells.remove((x, y))
for pos in dead_neighbours:
undead[pos] += 1
for pos, _ in filter(lambda elem: elem[1] == 3, undead.items()):
new_cells.add((pos[0], pos[1]))
return Grid(grid.dim, new_cells)
def draw_grid(screen: pygame.Surface, grid: Grid, color: tuple) -> None:
cell_width = screen.get_width() / grid.dim.width
cell_height = screen.get_height() / grid.dim.height
border_size = 2
for x, y in grid.cells:
pygame.draw.rect(
screen,
color,
(
x * cell_width + border_size,
y * cell_height + border_size,
cell_width - border_size,
cell_height - border_size,
),
)
def main():
grid = GOSPER_GLIDER
pygame.init()
screen = pygame.display.set_mode((600, 400))
color = (255, 0, 0) # Initial color
while True:
if pygame.QUIT in [e.type for e in pygame.event.get()]:
sys.exit(0)
screen.fill((0, 0, 0))
draw_grid(screen, grid, color)
grid = update_grid(grid)
pygame.display.flip()
# Change color (you can modify this logic based on your requirements)
color = (color[2], color[0], color[1]) # Simple color rotation
time.sleep(0.1)
if __name__ == "__main__":
main()