### pygame像素级生命游戏模拟game of life animation

```"""pygame像素级生命游戏模拟.py
Conway's Game of Life），又称康威生命棋，是英国数学家约翰·何顿·康威在1970年发明的细胞自动机。

"""
import pygame
from pygame.locals import *
from random import randint

pygame.init()
life_color = (222,211,0,255)          #有颜色的代表活的,黑色代表死的
width,height = 200,200
screen = pygame.display.set_mode((width,height))
pygame.display.set_caption("生命游戏模拟_风火轮少儿编程李兴球")

life_image = pygame.Surface((width,height))

for i in range(5000):                 #初始化5000个活的
x = randint(0,width)
y = randint(0,height)
life_image.set_at((x,y),life_color)

def get_numbers(x,y):
"""得到周围颜色点数量,也就是活的数量"""
counter = 0
for i in range(-1,2):
for j in range(-1,2):
if i==0 and j ==0 :continue    #自己就跳过
x1 = x + i   ; y1 = y + j
if x1 >= 0 and x1< width and y1>=0 and y1<height:
color = life_image.get_at((x1,y1))
if color == life_color:counter = counter + 1

return counter

def event_check():
"""事件检测,加了它在循环中不会无响应"""
for event in pygame.event.get():pass

running = True
while running:
for  x in range(width):
for y in range(height):
event_check()
cell = life_image.get_at((x,y))
cell_number = get_numbers(x,y)         #得到周围活的细胞数量
if cell == life_color:                 #该细胞为活的
if cell_number == 3 or cell_number==2 :continue   #周围有2个或3个,它还是活的
if cell_number < 2 or cell_number >3 :life_image.set_at((x,y),(0,0,0)) #小于2个,大于3个,它死了
else:
if cell_number == 3 : life_image.set_at((x,y),life_color)  #周围有3个,复活

screen.blit(life_image,(0,0))   #把life_image渲染到screen上.
pygame.display.update()         #更新屏幕显示

"""
1、规则

---------------------
"""

pygame.image.save(life_image,"te.png")
```