# Rules of the game:
# Game of Life (Game of Life), or call it the full name of John Conway's Game of Life. Cellular automata is a British mathematician John Conway invented in the 1970s.
# 1 the number of cells surrounding the living cells is less than two or more than three will death; (outliers excessive competition or death)
# viable cells surrounding 2. If there are two or three cells can survive; (normal survival)
around # 3. dead cells (spaces) If you happen to have three new cells living cells will be born. (Breeding)
# three rules referred to B3 / S23. If the adjustment rules corresponding to the number of cells, but also derived from other types of automatic machines.
The main idea is to first create a class Cells, first solve the problem of life and death of a cell, then put this grid cell
CellGrid, and then establishing a Game category for the displayed living cells:
# Rules of the game: # Game of Life (Game of Life), or call it the full name of John Conway's Game of Life. Cellular automata is a British mathematician John Conway invented in the 1970s. # 1 the number of viable cells surrounding cells is less than two or more than three will death; (outliers excessive competition or death) # surrounding live cells 2. If there are two or three cells can survive; (normal survival) # around 3. dead cells (spaces) If you happen to have three new cells living cells will be born. (Breeding) # three rules referred to B3 / S23. If the adjustment rules corresponding to the number of cells, but also derived from other types of automatic machines. Import Random class the Cell: "" " cell types, single cells " "" DEF the __init__ (Self, IX, iy, is_live): self.ix = IX self.iy = iy self.is_live = is_live self.neighbour_count= 0 def __str__(self): return "[{},{},{:5}]".format(self.ix, self.iy, str(self.is_live)) def calc_neighbour_count(self): count = 0 pre_x = self.ix - 1 if self.ix > 0 else 0 for i in range(pre_x, self.ix+1+1): pre_y = self.iy - 1 if self.iy > 0 else 0 for j in range(pre_y, self.iy+1+1): if i == self.ix and j == self.iy: continue if self.invalidate(i, j): continue # type() count += int(CellGrid.cells[i][j].is_live) self.neighbour_count = count return count def invalidate(self, x, y): if x >= CellGrid.cx or y >= CellGrid.cy: return True if x < 0 or y < 0: return True returnFalse DEF next_iter (Self): IF self.neighbour_count>. 3 or self.neighbour_count <2 : self.is_live = false elif self.neighbour_count ==. 3 : self.is_live = True elif self.neighbour_count == 2 : Print (Self. is_live) class CellGrid: "" " cell-based mesh, all the cells are in a long cx, cy wide grid " "" cells = [] CX = 0 cy = 0 DEF __init__(self, cx, cy): CellGrid.cx = cx CellGrid.cy = cy for i in range(cx): cell_list = [] for j in range(cy): cell = Cell(i, j, random.random() > 0.5) cell_list.append(cell) CellGrid.cells.append(cell_list) def next_iter(self): for cell_list in CellGrid.cells: for item in cell_list: item.next_iter() def calc_neighbour_count(self): for cell_list in CellGrid.cells: for item in cell_list: item.calc_neighbour_count()
import pygame import sys from life import CellGrid, Cell GREY = (190, 190, 190) RED = (255, 0, 0) class Game: screen = None def __init__(self, width, height, cx, cy): self.width = width self.height = height self.cx_rate = int(width / cx) self.cy_rate = int(height / cy) self.screen = pygame.display.set_mode([width, height]) self.cells = CellGrid(cx, cy) def show_life(self): for cell_list in self.cells.cells: for item in cell_list: x = item.ix y = item.iy if item.is_live: pygame.draw.rect(self.screen, RED, [x * self.cx_rate, y * self.cy_rate, self.cx_rate, self.cy_rate]) pygame.init() pygame.display.set_caption("绘图") game = Game(800, 800, 40, 40) clock = pygame.time.Clock() while True: game.screen.fill(GREY) clock.tick(1) # 每秒循环1次 for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() game.cells.calc_neighbour_count() for i in game.cells.cells: txt = "" for j in i: txt += str(j) print(txt) game.show_life() pygame.display.flip() game.cells.next_iter()