| 
									
										
										
										
											2019-09-21 03:16:28 +00:00
										 |  |  | cell_size = 10 | 
					
						
							|  |  |  | from random import choice | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | clr = 255 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | NEIGHBOURS = ((-2,  0), ( 2,  0), | 
					
						
							| 
									
										
										
										
											2019-09-21 17:07:03 +00:00
										 |  |  |              (-1, -1), ( 0, -2), | 
					
						
							|  |  |  |              ( 1, -1), (-1,  1), | 
					
						
							|  |  |  |              ( 0,  2), ( 1,  1))  | 
					
						
							| 
									
										
										
										
											2019-09-21 03:16:28 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | def rule(s, v): | 
					
						
							|  |  |  |     if v < 2 or v > 3: | 
					
						
							|  |  |  |         return 0 | 
					
						
							|  |  |  |     elif v == 3: | 
					
						
							|  |  |  |         return 1 | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         return s | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | play = False | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def setup(): | 
					
						
							|  |  |  |     global grid, next_grid, rows, cols | 
					
						
							|  |  |  |     size(755, 500) | 
					
						
							|  |  |  |     colorMode(HSB) | 
					
						
							|  |  |  |     rows = height / cell_size | 
					
						
							|  |  |  |     cols = width / cell_size | 
					
						
							|  |  |  |     grid = empty_grid() | 
					
						
							|  |  |  |     next_grid = empty_grid() | 
					
						
							|  |  |  |     noStroke() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def draw(): | 
					
						
							|  |  |  |     # background(0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for i in range(cols): | 
					
						
							|  |  |  |         x = i * cell_size | 
					
						
							|  |  |  |         for j in range(rows): | 
					
						
							|  |  |  |             y  = j * cell_size | 
					
						
							|  |  |  |             current_state = grid[i][j] | 
					
						
							|  |  |  |             # fill(clr, 255, current_state * 255, 100) # translucent | 
					
						
							|  |  |  |          | 
					
						
							|  |  |  |             ngbs_alive = calc_ngbs_alive(i, j) | 
					
						
							|  |  |  |             result = rule(current_state, ngbs_alive) | 
					
						
							|  |  |  |             next_grid[i][j] = result   | 
					
						
							|  |  |  |             if current_state: | 
					
						
							|  |  |  |                 # circle(x, y, cell_size * 2) # overlapping circles | 
					
						
							| 
									
										
										
										
											2019-09-21 17:07:03 +00:00
										 |  |  |                 fill((clr+ next_grid[i][j]*128)%255, 255, 255) | 
					
						
							| 
									
										
										
										
											2019-09-21 03:16:28 +00:00
										 |  |  |             else: | 
					
						
							| 
									
										
										
										
											2019-09-21 17:07:03 +00:00
										 |  |  |                 fill(next_grid[i][j]*255, 128) | 
					
						
							| 
									
										
										
										
											2019-09-21 03:16:28 +00:00
										 |  |  |             square(x, y, cell_size) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |      | 
					
						
							| 
									
										
										
										
											2019-09-21 17:07:03 +00:00
										 |  |  |     if play and frameCount % 5 == 0: | 
					
						
							| 
									
										
										
										
											2019-09-21 03:16:28 +00:00
										 |  |  |         step() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def calc_ngbs_alive(i, j): | 
					
						
							|  |  |  |     alive = 0 | 
					
						
							|  |  |  |     for iv, jv in NEIGHBOURS: | 
					
						
							|  |  |  |         alive += grid[(i + iv) % cols][(j + jv) % rows] | 
					
						
							|  |  |  |     return alive | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def empty_grid(): | 
					
						
							|  |  |  |     grid = [] | 
					
						
							|  |  |  |     for _ in range(cols): | 
					
						
							|  |  |  |         grid.append([0] * rows) | 
					
						
							|  |  |  |     return grid | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def randomize_grid(): | 
					
						
							|  |  |  |     global clr | 
					
						
							|  |  |  |     clr = random(255) | 
					
						
							|  |  |  |     for i in range(cols): | 
					
						
							|  |  |  |         for j in range(rows): | 
					
						
							|  |  |  |             grid[i][j] = choice((0, 1)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def step(): | 
					
						
							|  |  |  |     global grid, next_grid | 
					
						
							|  |  |  |     grid = next_grid | 
					
						
							|  |  |  |     next_grid = empty_grid() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def keyPressed(): | 
					
						
							|  |  |  |     global grid | 
					
						
							|  |  |  |     if key == "e": | 
					
						
							|  |  |  |         grid = empty_grid() | 
					
						
							|  |  |  |     if key == "r": | 
					
						
							|  |  |  |         randomize_grid() | 
					
						
							|  |  |  |     if key == " ": | 
					
						
							|  |  |  |         global play | 
					
						
							|  |  |  |         play = not play | 
					
						
							|  |  |  |     if key == "s": | 
					
						
							|  |  |  |         saveFrame("#####.png")         | 
					
						
							|  |  |  |                  | 
					
						
							|  |  |  | def mouseReleased(): | 
					
						
							|  |  |  |     invert_on_mouse() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def mouseDragged():     | 
					
						
							|  |  |  |     invert_on_mouse() | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  | def invert_on_mouse():     | 
					
						
							|  |  |  |     for i in range(cols): | 
					
						
							|  |  |  |         x = i * cell_size | 
					
						
							|  |  |  |         for j in range(rows): | 
					
						
							|  |  |  |             y  = j * cell_size | 
					
						
							|  |  |  |             current_state = grid[i][j] | 
					
						
							|  |  |  |             if mouse_over(x, y): | 
					
						
							|  |  |  |                 grid[i][j] = (1, 0)[current_state] | 
					
						
							|  |  |  |                  | 
					
						
							|  |  |  | def mouse_over(x, y): | 
					
						
							|  |  |  |     return x < mouseX < x + cell_size and y < mouseY < y + cell_size |