

You may also like to check out this moderately efficient version I wrote that uses Numpy: numpy_life.py. Take a look at this answer I wrote earlier this month for an alternative.Įventually, you should give your program the ability to read the common RLE format used by many Life programs. Your CELL_MAP isn't a convenient way to put Life patterns into your program, but I guess it's ok for testing purposes. Here's the same thing as a list comprehension: CELL_LIST = [Cell(bool(v), x, y)īut as I said earlier, it's probably a good idea to make CELL_LIST a 2D list: cell_list =
GAME OF LIFE PYTHON CODE
Here's a more compact way to build your CELL_LIST than what your code currently does: CELL_LIST = Your checkNeighbours method is extremely inefficient: for each cell, it scans the entire grid looking for a cell's neighbours! A simple alternative is to store your cells in a 2D list so you can quickly locate a cell's neighbours. I have a few more comments about your code. Self.alive = self.neighbours = 3 or self.alive and self.neighbours = 2 Here's a more compact version of that method: def breed(self): You should probably do that resetting in the. So in each generation each cell's new neighbour count gets added to the previous accumulated neighbour count. However, the bug that's causing your error is that you don't reset a cell's neighbour count to zero after you've determined whether it'll be alive or dead in the next generation. Stage 2 - Creating a blank grid on the pygame screen. Stage 1 - Creating a blank pygame screen. I am going to run through the process of producing the Game of Life in 4 stages. I don't have pygame installed, so I can't run your code. This should avoid any confusion about where you should be typing the code, and will show you the required level of indentation for each line. #dead cell ressurects if neighbours equals 3 If self.alive = False and self.neighbours = 3:

If cell.x = self.x-1 and cell.y = self.y and cell.alive = True:Įlif cell.x = self.x+1 and cell.y = self.y and cell.alive = True:Įlif cell.x = self.x-1 and cell.y = self.y-1 and cell.alive = True:Įlif cell.x = self.x and cell.y = self.y-1 and cell.alive = True:Įlif cell.x = self.x+1 and cell.y = self.y-1 and cell.alive = True:Įlif cell.x = self.x-1 and cell.y = self.y+1 and cell.alive = True:Įlif cell.x = self.x and cell.y = self.y+1 and cell.alive = True:Įlif cell.x = self.x+1 and cell.y = self.y+1 and cell.alive = True: I posted the whole code below as I dont know where my mistake is, but I'd highly appreciate if someone would point me in the right direction. I read the code over and over and I dont really get what I'm missing here. in the code below (CELL_MAP)) it does not work the way it should. The problem is that when I test it with some known starting patterns (e.g. The way it works is that I have a list of cell-instances, which then check how many neighbours they have and then either stay alive or die, based on their neighbours. So I read about Conways Game of Life and tried to implement it with PyGame.
