I have made a simple Conway's Game of Life program in Python and I need help making an animation with matplotlib because tbh I'm very lost and I can't seem to get my head around how's it done.
My code looks like this:
import matplotlib.pyplot as plt
import numpy as np
def initialize(size):
grid = np.random.choice([0, 1], size*size, p=[0.8, 0.2]).reshape(size, size)
plt.imshow(grid)
plt.show(block=False)
plt.pause(0.2)
return grid
def conway_step(grid, size):
new_grid = np.zeros_like(grid)
for x in range(size):
for y in range(size):
total = sum([grid[(x+i) % size, (y+j) % size] for i in range(-1, 2) for j in range(-1, 2)])
if grid[x, y] == 1 and total-1 in (2, 3):
new_grid[x, y] = 1
elif grid[x, y] == 0 and total == 3:
new_grid[x, y] = 1
else:
new_grid[x, y] = 0
grid = np.copy(new_grid)
return grid
def conway(random=True, size=100):
grid = initialize(size)
for i in range(30):
grid = conway_step(grid, size)
plt.imshow(grid)
plt.show(block=False)
plt.pause(0.2)
return
if __name__ == "__main__":
conway(size=100)
This works fine but I would like to implement this as an animation and possibly get an mp4 file out. I've tried something like this:
def conway(size):
grid = initialize(size)
fig, ax = plt.subplots()
img = ax.imshow(grid)
ani = animation.FuncAnimation(fig, conway_step, fargs=(grid, size))
plt.show()
But it doesn't work. Any help?
