📜 ⬆️ ⬇️

The practice of programming games in python: life



Recently it became known that python is recognized as the most popular language for teaching students in the United States. As a student of Technopark, I decided to keep up with the trend, study this trendy language in more detail and write several posts at the same time. To warm up, I decided to implement Conway's Game of Life . This is quite a fun “game” in which we can in some sense simulate the development of a group of organisms in the environment. The rules are as follows: divide the space into cells, which can be either alive or empty. And then at each step the state of the cell is updated depending on the number of living neighbors. For example, too much - the cell dies, and if not - it is born. You can experiment with the soul with configurations, you get different strange things, sometimes ships. Gliders are a separate topic, these are groups of cells that change and at the same time travel in space. In addition to ships, other groups of cells with cunning properties may also be formed, but about them - on Wikipedia.

So, python with us, we will assume, is installed. For drawing we will use the standard Tkinter library. The plan of action is as follows: we draw a two-dimensional array of squares, and when we press the button, we call a function to update the field in accordance with our rules of evolution. In addition, we will make it possible to intervene in the current picture, so that you can draw your panorama, with ships and other figures.

Now for some code. He will go out of order. So that was clear.
from Tkinter import Tk, Canvas, Button, Frame, BOTH, NORMAL, HIDDEN #    root = Tk() #      win_width = 350 win_height = 370 config_string = "{0}x{1}".format(win_width, win_height + 32) #  geometry()  ,     #    '350x370',     root.geometry(config_string) #    ,     cell_size = 20 #     ,    Canvas, #      , #         root canvas = Canvas(root, height=win_height) #  ,  show()    canvas.pack(fill=BOTH) #      field_height = win_height / a field_width = win_width / a #    ,  ,   cell_matrix = [] for i in xrange(field_height): for j in xrange(field_width): #         square = canvas.create_rectangle(2 + cell_size*j, 2 + cell_size*i, cell_size + cell_size*j - 2, cell_size + cell_size*i - 2, fill="green") canvas.itemconfig(square, state=HIDDEN, tags=('hid','0')) #    cell_matrix.append(square) #   ,       fict_square = canvas.create_rectangle(0,0,0,0, state=HIDDEN, tags=('hid','0')) cell_matrix.append(fict_square) #        ,   Canvas, #     frame = Frame(root) btn1 = Button(frame, text='Eval', command = step) btn2 = Button(frame, text='Clear', command = clear) #   btn1.pack(side='left') btn2.pack(side='right') #   frame.pack(side='bottom') #         canvas   draw_a canvas.bind('', draw_a) #  ,  c      root.mainloop() 

Now let's look at the functional part:
 #     def refresh(): for i in xrange(field_height): for j in xrange(field_width): k = 0 #    for i_shift in xrange(-1, 2): for j_shift in xrange(-1, 2): if (canvas.gettags(cell_matrix[addr(i + i_shift, j + j_shift)])[0] == 'vis' and (i_shift != 0 or j_shift != 0)): k += 1 current_tag = canvas.gettags(cell_matrix[addr(i, j)])[0] #         if(k == 3): canvas.itemconfig(cell_matrix[addr(i, j)], tags=(current_tag, 'to_vis')) # nota bene,        ""  if(k = 4): canvas.itemconfig(cell_matrix[addr(i, j)], tags=(current_tag, 'to_hid')) if(k == 2 and canvas.gettags(sm[addr(i, j)])[0] == 'vis'): canvas.itemconfig(cell_matrix[addr(i, j)], tags=(current_tag, 'to_vis')) #         def repaint(): for i in xrange(field_height): for j in xrange(field_width): if (canvas.gettags(sm[addr(i, j)])[1] == 'to_hid'): canvas.itemconfig(sm[addr(i, j)], state=HIDDEN, tags=('hid','0')) if (canvas.gettags(sm[addr(i, j)])[1] == 'to_vis'): canvas.itemconfig(sm[addr(i, j)], state=NORMAL, tags=('vis','0')) #  :     def step(): refresh() repaint() 

And auxiliary functions:
 #             def draw_a(e): ii = (ey - 3)/cell_size jj = (ex - 3)/cell_size #   canvas.itemconfig(cell_matrix[addr(ii, jj)], state=NORMAL, tags='vis') #            def addr(ii,jj): if(ii < 0 or jj < 0 or ii >= field_height or jj >= field_width): #     return len(cell_matrix) - 1 else: return ii*(win_width/a) + jj 

Well, something like this, I hope you were interested. Link to github .

')

Source: https://habr.com/ru/post/228379/


All Articles