TI/Skrypty z zajęć/8

Z Brain-wiki
# -*- coding: utf-8 -*-
"""
Created on Mon Apr 20 15:14:18 2020

@author: Tomek
"""
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

R = np.zeros((11,18))
R[4:7,5:13]=1
R[5,6]=0
R[5,11]=0

def step_a(a):
    result = np.zeros(a.shape)
    for x in range(a.shape[0]):
        for y in range(a.shape[1]):
            #liczenie sasiadow
            nei = np.sum(a[max(x-1,0):x+2,max(y-1,0):y+2])-a[x,y]
            if nei==3: result[x,y] = 1
            if nei==2: result[x,y] = a[x,y]
    return result

def animuj(i):
    global R
    R = step_a(R)
    im.set_array(R)
    return [im] #magia tutaj!!!!
    

fig = plt.figure( figsize=(8,8) )
im = plt.imshow(R, cmap="Greys")
ani = animation.FuncAnimation(fig, animuj, frames = 20, interval = 1000)

plt.show()