![]() ![]() Self.draw_square(realx, realy, self.size_factor) # Draw a square on the game board for every live cell in the grid. The coordinates are worked out by multiplying the current cell number by the size_factor property that we set up at the start. With the object initialisation method in place we just need to create the code that will render the game board onto the canvas widget.Īll we need to do here is loop through the multi-dimensional array and if the current cell is alive (represented by a "1" in the cell) then we draw a square in the game board at the correct coordinates. # Create the class and kick off the Tkinter loop. Self.canvas = Canvas(self, width=self.width_and_height, height=self.width_and_height, bg='white') # Create the canvas widget and add it to the Tkinter application window. Self.geometry(str(self.width_and_height) + "x" + str(self.width_and_height)) Self.size_factor = self.width_and_height / self.resolution # Set the height and width of the applciation. # Prevent the application window from being resized. import tkinter as tkĭef _init_(self, width_and_height = 400, resolution = 100): We will use this array to represent the data behind the game, and will work on rendering that onto the canvas in the next step. The array represents a alive cell with a "1" and a dead cell with a "0", which we pick using the Python random module. Using these two properties we work out some geometry for the application, create the Canvas widget, and then generate a random game board using a multi-dimensional array. ![]() We use the resolution property to control how large each square on the game grid will be, which means we can make the grid as big or as small as we need. In the code below, the Canvas will always be a square and the size will be controlled through the width_and_height property. Doing this means we inherit all of the methods and attributes that the parent class has, which means we can create a modular Tkinter application quite easily. ![]() We do this by extending the Tk application class into our new game_of_life class. The first step is to create the objects needed for the application and draw the grid. The ability to add events also allows us to interact with the gameboard directly and add new cells. The Python Tkinter Canvas widget is an ideal environment for creating Conway's game of live as it allows is to create shapes in a grid structure needed for the game to play out. I have previously written about creating Conway's game of live in PHP, which was quite limited due to the limitations of PHP. It is often used as a teaching aid in computer science both in terms of data science but also as a very simple introduction to artificial intelligence. If you look up Conway's game of life you will see a large collection of examples and programs that can be used to generate the game. This is the simulation running on a 100 by 100 game board for 100 frames. Through these simple rules a great deal of complexity can be generated. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.įor every 'tick' of the game board the rules are evaluated and each cell is kept alive, given life or killed.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 by underpopulation.The rules of the game (taken from wikipedia) are as follows. The game is technically a zero player game in that the initial setup of the game dictates the eventual evolution of the board. The game takes place on a two dimensional board containing a grid of orthogonal cells. Conway's game of life, was devised by John Conway in 1970 and is a way of modelling very simple cell population dynamics. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |