生命遊戲(python實現,pygame顯示圖形)

# 遊戲規則:
# 生命遊戲(Game of Life),或者叫它的全稱John Conway's Game of Life。是英國數學家約翰·康威在1970年代所發明的一種元胞自動機。
# 1. 活細胞周圍的細胞數如果小於2個或多於3個則會死亡;(離羣或過度競爭導致死亡)
# 2. 活細胞周圍如果有2或3個細胞可以繼續存活;(正常生存)
# 3. 死細胞(空格)周圍如果恰好有3個細胞則會誕生新的活細胞。(繁殖)
# 這三條規則簡稱B3/S23。如果調整規則對應的細胞數量,還能衍生出其他類型的自動機。
 
主要想法就是先建立一個Cells類,先解決一個細胞的生死問題,然後將此細胞放入網格
CellGrid中, 然後再建立一個Game類,用來將活着的細胞顯示出來:
# 遊戲規則:
# 生命遊戲(Game of Life),或者叫它的全稱John Conway's Game of Life。是英國數學家約翰·康威在1970年代所發明的一種元胞自動機。
# 1. 活細胞周圍的細胞數如果小於2個或多於3個則會死亡;(離羣或過度競爭導致死亡)
# 2. 活細胞周圍如果有2或3個細胞可以繼續存活;(正常生存)
# 3. 死細胞(空格)周圍如果恰好有3個細胞則會誕生新的活細胞。(繁殖)
# 這三條規則簡稱B3/S23。如果調整規則對應的細胞數量,還能衍生出其他類型的自動機。
import random


class Cell:
    """
    細胞類,單個細胞
    """
    def __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
        return False

    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:
    """
    細胞網格類,所有細胞都處在一個長cx,寬cy的網格中
    """
    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()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章