![]() 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 = 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. I don't have pygame installed, so I can't run your code. #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. I haven't implemented any yet, but I'm not sure how much I need to account for - does every function need a try. ![]() I'm looking for feedback on how to make the code faster (the glider gun provided slows down after making about 3 gliders), as well as error handling. Most of my previous projects have been algorithm-based, for example writing an algorithm to crack the Caesar cipher. My skill level is a beginner - this is my first non-trivial project that makes use of OOP concepts like classes. #This gives the default pattern of a Gosper glider gun.Īpp = App(60, 5, initial_active_cells = [ X * self.cell_size, (y+1) * self.cell_size,įill = self.active_color, outline = "black")įill = self.inactive_color, outline = "black")Īpp.canvas_dict, fill = self.active_color)Īpp.canvas_dict, fill = self.inactive_color) (x+1) * self.cell_size, y * self.cell_size, #This specifies the corners of the polygon.Īpp.canvas_dict = _polygon( (50, self.refresh_display)ĭef render_canvas(self, canvas_created = False): Self.render_canvas(canvas_created = True) ![]() Self.render_canvas(canvas_created = False) Initial_active_cells = self.initial_active_cells) Self.initial_active_cells = initial_active_cells Initial_active_cells - List of cells that start alive. Inactive_color - Color of a cell when dead.Ĭondition_dict - Nested dictionary.Ĭoordinates as keys, dicts of conditions as values. Grid_size - Size of grid, in cells.Ĭell_size - Size of a cell, in pixels.Īctive_color - Color of a cell when alive. Self, coords, size, grid_size, conditions =, initial_active_cells = ): Thus, it reduces the number of cells the grid needs to simulate. """active_cells allows the grid to determine sim_cells. #ref_dict allows the coords to act as a pointer to the cell. It's not meant to be limited to just Game of Life, though, as it's meant to also be used for a simple physics simulation project. Using Python 3.7.0, I've programmed a 'Game of Life' simulator.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |