生命游戏蟒蛇 - 资源太多了

时间:2016-01-30 19:55:05

标签: python tkinter conways-game-of-life tkinter-canvas

我已经开始编程并尝试过生活中的游戏'在20x20电路板上一切都很好,但如果我采用100或更大的电路板尺寸,经过100代,该程序需要500mb的RAM和25%的CPU(并且每个版本需要更多),我想这很糟糕。所以我认为,我有一个逻辑错误,每代需要更多的RAM。发布以下代码

import time
from tkinter import *

class cell(object):
    def __init__(self,lives):
        self.lives = lives

    def set(self, alive):
        self.lives=alive


class grid(object):
    gen  = 0
    def __init__(self,height,width,file=None):
        if(file is None):
            self.root=Tk()
            self.root.title("Game of Life")
            self.canvas=Canvas(self.root,height =height*6, width=width*6)
            self.canvas.grid(row=0,column=0)
            self.matrix=[[cell(False) for x in range(width)] for y in range(height)]
            self.next_grid = [[cell(False) for x in range(width)] for y in range(height)]
            self.width= width
            self.height=height
        else:
            pass
            #comes later with fileinput

    def print_grid(self):
        for i in range(self.height):
            for j in range(self.width):
                if(self.matrix[i][j].lives):
                    pass
                    self.canvas.create_rectangle(j*6,i*6,j*6+4,i*6 + 4,fill="black")
                elif(not self.matrix[i][j].lives):
                    self.canvas.create_rectangle(j*6,i*6,j*6+5,i*6 + 5,fill="white",width=0)#invalid command name ".9727696"?!

    def set_cell(self, height,width,live):
        self.matrix[height][width].set(live)

    def count_neighbours(self,height,width):
        counter = 0
        for hi in range(height-1,height+2):
             for wi in range(width -1,width+2):
                if(hi < self.height and hi > -1 and wi < self.width and wi > -1):
                   if(self.matrix[hi][wi].lives):
                        counter = counter +1

        if(self.matrix[height][width].lives):
            counter = counter -1
        return counter

    def does_survive(self,i,j):
        neighbours = self.count_neighbours(i,j)
        result =False
        if(self.matrix[i][j].lives):
            if(neighbours == 2 or neighbours == 3):
               result = True
        elif (neighbours == 3):
            result =  True
        return result


    def next_gen(self):

    self.next_grid = [[cell(False) for x in range(self.width)] for y in range(self.height)]
        self.gen = self.gen +1
        for i in range(self.height):
            for j in range(self.width):
                self.next_grid[i][j].set(self.does_survive(i,j))
        self.matrix = self.next_grid


def test():
    cell1=cell(True)
    cell2=cell(False)
    cell2.set(True)
    place=grid(150,150)
    place.set_cell(50,60,True)
    place.set_cell(51,60,True)
    place.set_cell(51,59,True)
    place.set_cell(52,60,True)
    place.set_cell(50,61,True)
    for i in range(1000):
        place.print_grid()
        #time.sleep(0.01)
        place.next_gen()
        place.canvas.update()
    place.root.mainloop()
    test()

这是我第一次使用图形,是的...我希望你能帮助我:D

编辑:我发现了这个错误并且正在努力提高性能,但现在我遇到了问题,如果我不清楚next_gen网格中的几代人很奇怪......你能帮助我吗?

1 个答案:

答案 0 :(得分:0)

创建大小为150x150的网格时,您将创建22,500个画布项。 每次致电print_grid时,您都会创建另外 22,500个画布项目。由于您拨打print_grid 1000次,因此您创建了超过22个百万帆布项目。虽然画布相当强大,但它无法处理这么多项目。

您应该只创建一次网格,然后每次调用print_grid时,您只需修改已创建的矩形。创建矩形时,它返回一个id。然后,您可以将此id传递给canvas itemconfigure方法以更改该项的属性。