Created
December 8, 2020 04:57
-
-
Save EllieTheYeen/b266c9716992db8aeea419324c245e2e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from PIL import Image | |
import numpy as np | |
def getneigh(x, y): | |
return ( | |
(x, y - 1), # N | |
(x + 1, y - 1), # NE | |
(x + 1, y), # E | |
(x + 1, y + 1), # SE | |
(x, y + 1), # S | |
(x - 1, y + 1), # SW | |
(x - 1, y), # W | |
(x - 1, y - 1), # NW | |
) | |
os.chdir(os.path.split(__file__)[0] or '.') | |
imgs = [] | |
width, height = 20, 20 | |
#grid = np.random.randint(0, 2, (height, width), dtype=np.uint8) | |
grid = np.zeros((height, width), dtype=np.uint8) | |
gli = np.array([ | |
[0, 1, 0], | |
[0, 0, 1], | |
[1, 1, 1]]) | |
grid[:3, :3] = gli | |
print(grid.sum()) | |
for a in range(100): | |
newgrid = np.zeros(grid.shape, dtype=np.uint8) | |
for x in range(width): | |
for y in range(height): | |
alive = grid[y, x] | |
nearby = sum(grid[c[1], c[0]] for c in filter(lambda c: width > c[0] >= 0 and height > c[1] >= 0, getneigh(x, y))) | |
if nearby == 3 or (alive and nearby == 2): | |
newgrid[y, x] = 1 | |
grid = newgrid | |
ia = ((1 - grid) * 255).repeat(3).reshape(height, width, 3) | |
i = Image.fromarray(ia) #.crop((0, -30)) | |
imgs.append(i) | |
print(grid.sum()) | |
#ia = ((1 - grid) * 255).repeat(3).reshape(height, width, 3) | |
#i = Image.fromarray(ia) | |
imgs[0].save('game.gif', loop=0, save_all=True, append_images=imgs[1:]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment