sahaquiel-m

Conway's Game of Life Python Code

May 9th, 2021
502
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.55 KB | None | 0 0
  1. import cv2, numpy as np
  2. from scipy.ndimage import correlate
  3.  
  4. img = (cv2.cvtColor(cv2.imread("start.png"),cv2.COLOR_BGR2GRAY)/255+.5).astype("uint8")
  5. mask = np.array([[1,1,1],[1,0,1],[1,1,1]])
  6.  
  7. while True: # main loop
  8.     summed = correlate(img,mask,mode="constant") # neighbor count of each cell
  9.     img = (np.logical_and((summed == 2) + (summed == 3), img) + np.logical_and(summed == 3, 1-img)).astype("uint8") # game of life rules
  10.     cv2.imshow("Conway's Game of Life", img*255) # show image
  11.     if cv2.waitKey(1) == 27: # stop sim if esc key is pressed
  12.         break
Advertisement
Add Comment
Please, Sign In to add comment