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.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |