![]() ![]() It would likely add a little bit of computational overhead. Note: this might be a purely academic exercise. One advantage to this (admittedly, one you might never leverage) is that you can change the game simply by changing the behavior of the Square class your game logic itself won't have to change at all. ![]() You can do this by creating a tick method (or grow, or mutate or whatever) that implements the rules. Instead of building this behavioral logic into your game itself, you can build it into the Square object. That is the very definition of an object. Move behavior into the Square classĬonceptually, each square represents a single living cell with very well defined behaviors. Also, as stated earlier, you should use docstrings in the case of functions. These sorts of comments have no value, especially if you use good function names. Self.update_canvas(canvas_done=True, canvas_items=ems)ĭef update_canvas(self, canvas_done=False, canvas_items=): # Mainloop in tkinter, run the code and loop it until exit called Self.canvas = Canvas(self.root, height=self.length, width=self.length) id = Grid(self.length, self.size, tolerance, active_col='#008080', inactive_col='white') # Create a grid object which can manipulate the squares " Box size needs to be a factor of window size.") Raise Exception("The squares don't fit evenly on the screen." + # If the size of the boxes isn't a factor of the window size Self.length = length # Length of side of window # Checks if y value is >= 0 and if the bottom side of the square is not off the board as y value is top leftĭef _init_(self, length, size, tolerance=0.8): # Checks if x value is >= 0 and if the right side of the square is not off the board as x value is top left # Returns whether a coordinate is inbounds in the grid # Gives the bottom right values of square Self.inactive_colour = inactive_col # Colour if dead Self.active_colour = active_col # Colour if alive # Initialization function (all the precalled things)ĭef _init_(self, coords, length, size, state=False, active_col='black', inactive_col='white'): The rules continue to be applied repeatedly to create further generations. The first generation is created by applying the above rules simultaneously to every cell in the seed-births and deaths occur simultaneously,Īnd the discrete moment at which this happens is sometimes called a tick (in other words, each generation is a pure function of the preceding one). The initial pattern constitutes the seed of the system. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction. Any live cell with more than three live neighbours dies, as if by overpopulation.Ĥ. Any live cell with two or three live neighbours lives on to the next generation.ģ. Any live cell with fewer than two live neighbours dies, as if caused by underpopulation.Ģ. ![]() ![]() The universe of the Game of Life is an infinite two-dimensional orthogonal grid of square cells, each of which is in one of two possible states, alive or dead, or "populated" or "unpopulated".Įvery cell interacts with its eight neighbours, which are the cells that are horizontally, vertically, or diagonally adjacent.Īt each step in time, the following transitions occur: My Code (commented very heavily as I want to show this as a project at school): ''' What could I do better in terms of optimisation of speed and memory and PEP8 and cleaning up my code? Also, what can I add to make it more functioning as I only have it generating random maps every time? I have used tkinter in Python 3 to create my own version of Conway's Game of Life. ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |