Advertisement
Guest User

Untitled

a guest
Jul 26th, 2020
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 11.99 KB | None | 0 0
  1. #Conway Game of Life
  2.  
  3. import pygame
  4. import random
  5.  
  6.  
  7. #PYGAME INITIALIZATION
  8. success, failure = pygame.init()
  9.  
  10. screen_width = 800
  11. screen_height = 600
  12.  
  13. screen = pygame.display.set_mode((screen_width, screen_height)) #Init the screen
  14. time = pygame.time.Clock() #Time from startup
  15. FPS = 5
  16.  
  17. #Screen Area = 480000 px (width * height)
  18. #Area of a cell = 100px --> 4800 Cell
  19.  
  20. BLACK = (0, 0, 0)#Live cell
  21. WHITE = (255, 255, 255)#dead cell
  22.  
  23.  
  24. class Cell:
  25.     """ x: x coordinate
  26.        y: y coordinate
  27.        size: width and height (same)
  28.        alive: int (boolean, 0 o 1), to track the status of a cell (live or dead), at the startup is random
  29.    """
  30.     def __init__(self, x, y, alive):
  31.         self.x = x
  32.         self.y = y
  33.         self.size = 10 #it's a square
  34.         self.alive = alive
  35.         if self.alive == 1:
  36.             self.color = BLACK
  37.         elif self.alive == 0:
  38.             self.color = WHITE
  39.  
  40. #Function needed in the next function ------------------------------------------------
  41. def checkAlive(cell, cellArray, curr_x, curr_y, counter):
  42.     """ Check wheter the current cell near the original cell is alive. If it is alive it adds 1 to the counter
  43.        cell: instance of the original cell
  44.        cellArray: cell list with all the initialized cells
  45.        curr_x: x coordinate of the cell which will be examined
  46.        curr_y: y coordinate of the cell which will be examined
  47.        counter: variable that is updated whenever a cell near to original has the "alive" attribute == 1
  48.    """
  49.  
  50.     for current_cell in cellArray:
  51.         if (current_cell.x == curr_x and current_cell.y == curr_y):
  52.             if (current_cell.alive == 1):
  53.                 counter += 1
  54.  
  55.  
  56. #Function to find the neighbours of a cell ---------------------------------------------------
  57.  
  58. def neighbour(cells, cell):
  59.     """Give as output the number of neighbours of a cell (only neighbours with the alive attribute = 1)
  60.        cells: List containing all the instances of the initialized cells
  61.        cell: The single instance of cell which will be examined to determine the number of live neighbours
  62.        return: number of live neighbours
  63.    """
  64.     num_neighbours = 0 #Number of near live cells(Moore neighbourhood)
  65.     x = cell.x
  66.     y = cell.y
  67.  
  68.     #List of exceptions before the main algorithm
  69.     #Upper-left corner (x = 0, y = 0) !*!*!*!*!*!*!*!*!**!*!*!*!*!*!*!*
  70.     if (x == 0 and y == 0):
  71.         #Cell on the right -----------
  72.         current_x = 1
  73.         current_y = 0
  74.  
  75.         checkAlive(cell, cells, current_x, current_y, num_neighbours)
  76.  
  77.         #Cell below current ----------------------------------------
  78.         current_x = 1
  79.         current_y = 1
  80.  
  81.         checkAlive(cell, cells, current_x, current_y, num_neighbours)
  82.  
  83.         #Cell below original cell
  84.         current_x = 0
  85.         current_y = 1
  86.  
  87.         checkAlive(cell, cells, current_x, current_y, num_neighbours)
  88.  
  89.         #Return the number of neighbours
  90.         return num_neighbours
  91.  
  92.     #Upper-right corner (x = window, y = 0)!*!*!*!*!**!*!*!*!*!*!*!*!*!*!*!*!*!*!**!*!*!*!*!*!*!*!
  93.     elif (x == screen_width - cell.size and y == 0):
  94.         #Cell below -------------------------------------
  95.         current_x = screen_width - cell.size
  96.         current_y = 1
  97.  
  98.         checkAlive(cell, cells, current_x, current_y, num_neighbours)
  99.  
  100.         #Cell to the left of current -----------------------------------
  101.         current_x -= 1
  102.  
  103.         checkAlive(cell, cells, current_x, current_y, num_neighbours)
  104.  
  105.         #Cell to the left of original
  106.         current_y = 0
  107.  
  108.         checkAlive(cell, cells, current_x, current_y, num_neighbours)
  109.  
  110.         #Return the number of neighbours
  111.         return num_neighbours
  112.  
  113.     #Lower-left corner (x = 0, y = window) !*!*!*!**!*!!*!**!*!!**!*!*!*!*!*
  114.     elif(x == 0 and y == (screen_height - cell.size)):
  115.  
  116.         #Cell over original ----------------------
  117.         current_x = 0
  118.         current_y = (screen_height - cell.size) - 1
  119.  
  120.         checkAlive(cell, cells, current_x, current_y, num_neighbours)
  121.  
  122.         #Cell to the right of current ------------------------------------------
  123.         current_x += 1
  124.  
  125.         checkAlive(cell, cells, current_x, current_y, num_neighbours)
  126.  
  127.         #Cell below current ---------------------------------------------
  128.         current_y += 1
  129.  
  130.         checkAlive(cell, cells, current_x, current_y, num_neighbours)
  131.  
  132.         #Return the number of neighbours
  133.         return num_neighbours
  134.  
  135.  
  136.  
  137.     #Lower right corner !*!*!*!*!*!!*!*!*!*!*!*!**!!*!*!*
  138.     elif (x == (screen_width - cell.size) and y == (screen_height - cell.size)):
  139.  
  140.         #Cell to the left of original ------------------------------------------------
  141.         current_x = (screen_width - cell.size) - 1
  142.         current_y = screen_height - cell.size
  143.  
  144.         checkAlive(cell, cells, current_x, current_y, num_neighbours)
  145.  
  146.         #Cell on top of current -------------------------------------------------------
  147.         current_y -= 1
  148.  
  149.         checkAlive(cell, cells, current_x, current_y, num_neighbours)
  150.  
  151.         #Cell to the right of current
  152.         current_x += 1
  153.  
  154.         checkAlive(cell, cells, current_x, current_y, num_neighbours)
  155.  
  156.         #Return the number of neighbours
  157.         return num_neighbours
  158.  
  159.  
  160.     #If the cell is in the first row (y = 0) (2 corners excluded) !*!*!*!*!*!!*!!*!*!*!*!
  161.     elif (y == 0 and (x != 0 and x != (screen_width - cell.size))):
  162.         #Cell to the right of original
  163.         current_x = x + 1
  164.         current_y = 0
  165.  
  166.         checkAlive(cell, cells, current_x, current_y, num_neighbours)
  167.  
  168.         #Cell below current
  169.         current_y += 1
  170.  
  171.         checkAlive(cell, cells, current_x, current_y, num_neighbours)
  172.  
  173.         #Cell below original
  174.         current_x = x
  175.  
  176.         checkAlive(cell, cells, current_x, current_y, num_neighbours)
  177.  
  178.         #Cell to the left of current
  179.         current_x -= 1
  180.  
  181.         checkAlive(cell, cells, current_x, current_y, num_neighbours)
  182.  
  183.         #Cell to the left of original
  184.         current_y -= 1
  185.  
  186.         checkAlive(cell, cells, current_x, current_y, num_neighbours)
  187.  
  188.         #Return the number of neighbours
  189.         return num_neighbours
  190.  
  191.  
  192.     #If the cell is in the last row (y = screen_height) 2 corners excluded !*!*!*!*!*!*!*!!*!*
  193.     elif (y == (screen_height - cell.size) and (x != 0 and x != (screen_width - cell.size))):
  194.         #Cell to the left of original
  195.         current_x = x - 1
  196.         current_y = y
  197.  
  198.         checkAlive(cell, cells, current_x, current_y, num_neighbours)
  199.  
  200.         #Cell on top of current
  201.         current_y -= 1
  202.  
  203.         checkAlive(cell, cells, current_x, current_y, num_neighbours)
  204.  
  205.         #Cell to the right of current
  206.         current_x += 1
  207.  
  208.         checkAlive(cell, cells, current_x, current_y, num_neighbours)
  209.  
  210.         #Cell to the right of current
  211.         current_x += 1
  212.  
  213.         checkAlive(cell, cells, current_x, current_y, num_neighbours)
  214.  
  215.         #Cell below current
  216.         current_y += 1
  217.  
  218.         checkAlive(cell, cells, current_x, current_y, num_neighbours)
  219.  
  220.         #Return the number of neighbours
  221.         return num_neighbours
  222.  
  223.  
  224.     #If the cell is in the first column (2 corners excluded) !*!*!*!*!*!*!*!*!*!*!*!*
  225.     elif (x == 0 and (y != 0 and y != (screen_height - cell.size))):
  226.         #Cell on top of original
  227.         current_x = x
  228.         current_y = y - 1
  229.  
  230.         checkAlive(cell, cells, current_x, current_y, num_neighbours)
  231.  
  232.         #Cell to the right of current
  233.         current_x += 1
  234.  
  235.         checkAlive(cell, cells, current_x, current_y, num_neighbours)
  236.  
  237.         #Cell below current
  238.         current_y += 1
  239.  
  240.         checkAlive(cell, cells, current_x, current_y, num_neighbours)
  241.  
  242.         #Cell below current
  243.         current_y += 1
  244.  
  245.         checkAlive(cell, cells, current_x, current_y, num_neighbours)
  246.  
  247.         #Cell to the left of current
  248.         current_x -= 1
  249.  
  250.         checkAlive(cell, cells, current_x, current_y, num_neighbours)
  251.  
  252.  
  253.         return num_neighbours
  254.  
  255.  
  256.     #If the cell is in the last column (x = screen width) !*!*!*!*!*!*!*!!**!!*
  257.     elif (x == (screen_width - cell.size) and (y != 0 and y != (screen_height - cell.size))):
  258.         #Cell below original
  259.         current_x = x
  260.         current_y = y + 1
  261.  
  262.         checkAlive(cell, cells, current_x, current_y, num_neighbours)
  263.  
  264.         #Cell to the left of current
  265.         current_x -= 1
  266.  
  267.         checkAlive(cell, cells, current_x, current_y, num_neighbours)
  268.  
  269.         #Cell on top of current
  270.         current_y -= 1
  271.  
  272.         checkAlive(cell, cells, current_x, current_y, num_neighbours)
  273.  
  274.         #Cell on top of current
  275.         current_y -= 1
  276.  
  277.         checkAlive(cell, cells, current_x, current_y, num_neighbours)
  278.  
  279.         #Cell to the right of current
  280.         current_x += 1
  281.  
  282.         checkAlive(cell, cells, current_x, current_y, num_neighbours)
  283.  
  284.         return num_neighbours
  285.  
  286.  
  287.     #GENERAL RULE
  288.     else:
  289.         #8 Neighbours
  290.         #Cell on top of original
  291.         current_x = x
  292.         current_y = y - 1
  293.  
  294.         checkAlive(cell, cells, current_x, current_y, num_neighbours)
  295.  
  296.         #Cell to the right of original
  297.         current_x += 1
  298.  
  299.         checkAlive(cell, cells, current_x, current_y, num_neighbours)
  300.  
  301.         #Cell below current
  302.         current_y += 1
  303.  
  304.         checkAlive(cell, cells, current_x, current_y, num_neighbours)
  305.  
  306.         #Cell below current
  307.         current_y += 1
  308.  
  309.         checkAlive(cell, cells, current_x, current_y, num_neighbours)
  310.  
  311.         #Cell to the left of current
  312.         current_x -= 1
  313.  
  314.         checkAlive(cell, cells, current_x, current_y, num_neighbours)
  315.  
  316.         #Cell to the left of current
  317.         current_x -= 1
  318.  
  319.         checkAlive(cell, cells, current_x, current_y, num_neighbours)
  320.  
  321.         #Cell on top of current
  322.         current_y -= 1
  323.  
  324.         checkAlive(cell, cells, current_x, current_y, num_neighbours)
  325.  
  326.         #Cell on top of current
  327.         current_y -= 1
  328.  
  329.         checkAlive(cell, cells, current_x, current_y, num_neighbours)
  330.  
  331.         return num_neighbours
  332.  
  333.  
  334.  
  335.  
  336. #CELL INITIALIZATION
  337. cell_array = []
  338. #Useful variable in the for loop
  339. x = 0
  340. y = 0
  341. init = False #Become true when Initialization is completed
  342.  
  343.  
  344. #Initialization
  345. while not init:
  346.  
  347.     is_alive = random.choices([0,1], weights = (95, 5), k=1)[0]#Randomly spawn cells with probability (Dead 95%, Alive 5 %)
  348.     cell = Cell(x, y, is_alive)#Single object
  349.     x += cell.size
  350.     cell_array.append(cell)
  351.     if x == screen_width: #End of a row
  352.         x = 0
  353.         y += cell.size
  354.     if y == screen_height:#Last row
  355.         init = True
  356.  
  357.  
  358. #DRAWING CELLS
  359. for cl in cell_array:
  360.     pygame.draw.rect(screen, cl.color, pygame.Rect(cl.x, cl.y, cl.size, cl.size))#Draw any single cell
  361.  
  362. pygame.display.flip() #To update the screen
  363.  
  364. #Debug
  365. print("Initialization Completed.")
  366.  
  367.  
  368. done = False #Check whether the program should run
  369.  
  370. #Main loop
  371. while not done:
  372.     #FPS
  373.     time.tick(FPS)
  374.  
  375.     #EVENT HANDLER
  376.     for event in pygame.event.get():
  377.         if event.type == pygame.QUIT: #Exit button
  378.             print("Quitting.")
  379.             done = True
  380.  
  381.  
  382.  
  383.     #SIMULATION --------------------------------------------------------------------
  384.  
  385.     #Run the algorithm of the game and update the screen (Moore algorithm)
  386.     for cell in cell_array:
  387.         if neighbour(cell_array, cell) in (2, 3): #2 or 3 live neighbours (survive)
  388.             cell.alive = 1
  389.         elif neighbour(cell_array, cell) < 2: #Few than 2 live neighbours (dies)
  390.             cell.alive = 0
  391.         elif neighbour(cell_array, cell) > 3: #More than 3 live neighbours (dies)
  392.             cell.alive = 0
  393.         elif ((cell.alive == 0) and (neighbour(cell_array, cell) == 3)): #Dead cell with 3 live neigh (live)
  394.             cell.alive == 1
  395.  
  396.     #Debug
  397.     print("Algorithm succesful.")
  398.  
  399.     #DRAWING CELLS
  400.     for cl in cell_array:
  401.         pygame.draw.rect(screen, cl.color, pygame.Rect(cl.x, cl.y, cl.size, cl.size))
  402.     #Debug
  403.     print("Cell loaded to the screen")
  404.  
  405.     pygame.display.flip() #To update the screen
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement