• 生命游戏(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()
  • 相关阅读:
    Scala中的构造器和高阶函数
    Scala中的元组
    Scala中的Map
    Scala中的If判断&While&For循环
    [改善Java代码]asList方法产生的List对象不可更改
    [改善Java代码]避开基本类型数组转换列表陷阱
    [改善Java代码]枚举和注解结合使用威力更大
    [改善Java代码]枚举项的数量限制在64个以内
    [改善Java代码]用枚举实现工厂方法模式更简洁
    [改善Java代码]在switch的default代码块中增加AssertionError错误
  • 原文地址:https://www.cnblogs.com/luhouxiang/p/11879664.html
Copyright © 2020-2023  润新知