8
8
9
9
from adafruit_led_animation .animation import Animation
10
10
from adafruit_led_animation .grid import PixelGrid , HORIZONTAL
11
- import random
12
11
13
12
14
13
class ConwaysLifeAnimation (Animation ):
@@ -21,12 +20,21 @@ class ConwaysLifeAnimation(Animation):
21
20
(1 , 1 ),
22
21
(- 1 , 1 ),
23
22
(1 , - 1 ),
24
- (- 1 , - 1 )
23
+ (- 1 , - 1 ),
25
24
]
26
25
LIVE = const (0x01 )
27
26
DEAD = const (0x00 )
28
27
29
- def __init__ (self , pixel_object , speed , color , width , height , initial_cells , equilibrium_restart = True ):
28
+ def __init__ (
29
+ self ,
30
+ pixel_object ,
31
+ speed ,
32
+ color ,
33
+ width ,
34
+ height ,
35
+ initial_cells ,
36
+ equilibrium_restart = True ,
37
+ ):
30
38
"""
31
39
Conway's Game of Life implementation. Watch the cells live and die based on the classic rules.
32
40
@@ -47,7 +55,9 @@ def __init__(self, pixel_object, speed, color, width, height, initial_cells, equ
47
55
self .initial_cells = initial_cells
48
56
49
57
# PixelGrid helper to access the strand as a 2D grid
50
- self .pixel_grid = PixelGrid (pixel_object , width , height , orientation = HORIZONTAL , alternating = False )
58
+ self .pixel_grid = PixelGrid (
59
+ pixel_object , width , height , orientation = HORIZONTAL , alternating = False
60
+ )
51
61
52
62
# size of the grid
53
63
self .width = width
@@ -59,7 +69,7 @@ def __init__(self, pixel_object, speed, color, width, height, initial_cells, equ
59
69
# counter to store how many turns since the last change
60
70
self .equilibrium_turns = 0
61
71
62
- #self._init_cells()
72
+ # self._init_cells()
63
73
64
74
def _is_pixel_off (self , pixel ):
65
75
return pixel [0 ] == 0 and pixel [1 ] == 0 and pixel [2 ] == 0
@@ -72,7 +82,7 @@ def _is_grid_empty(self):
72
82
"""
73
83
for y in range (self .height ):
74
84
for x in range (self .width ):
75
- if not self ._is_pixel_off (self .pixel_grid [x ,y ]):
85
+ if not self ._is_pixel_off (self .pixel_grid [x , y ]):
76
86
return False
77
87
78
88
return True
@@ -96,7 +106,9 @@ def _count_neighbors(self, cell):
96
106
neighbors = 0
97
107
for direction in ConwaysLifeAnimation .DIRECTION_OFFSETS :
98
108
try :
99
- if not self ._is_pixel_off (self .pixel_grid [cell [0 ] + direction [0 ], cell [1 ] + direction [1 ]]):
109
+ if not self ._is_pixel_off (
110
+ self .pixel_grid [cell [0 ] + direction [0 ], cell [1 ] + direction [1 ]]
111
+ ):
100
112
neighbors += 1
101
113
except IndexError :
102
114
pass
@@ -125,7 +137,7 @@ def draw(self):
125
137
for x in range (self .width ):
126
138
127
139
# check and set the current cell type, live or dead
128
- if self ._is_pixel_off (self .pixel_grid [x ,y ]):
140
+ if self ._is_pixel_off (self .pixel_grid [x , y ]):
129
141
cur_cell_type = ConwaysLifeAnimation .DEAD
130
142
else :
131
143
cur_cell_type = ConwaysLifeAnimation .LIVE
0 commit comments