Advertisement
1not

Conway's Game of Life

Oct 22nd, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.75 KB | None | 0 0
  1. import pygame
  2.  
  3. WIDTH = 1000
  4. HEIGHT = 720
  5. TITLE = "CONWAY'S GAME OF LIFE"
  6. TILESIZE = 20
  7. FPS = 2
  8. BACKGROUND = [20, 20, 20]   # default: [20, 20, 20]
  9. GRID = [0, 0, 0]            # default: [0, 0, 0]
  10. ORGANISM = [255, 255, 255]  # default: [255, 255, 255]
  11.  
  12. populated = []
  13. unpopulated = []
  14. location = []
  15.  
  16. i = 1
  17.  
  18. pygame.init()
  19.  
  20. RESOLUTION = [WIDTH, HEIGHT]
  21. screen = pygame.display.set_mode(RESOLUTION)
  22. pygame.display.set_caption(TITLE)
  23.  
  24. clock = pygame.time.Clock()
  25.  
  26.  
  27. def event():
  28.  
  29.     global running
  30.  
  31.     for event in pygame.event.get():
  32.  
  33.         if event.type == pygame.QUIT:
  34.             running = False
  35.  
  36.         if event.type == pygame.KEYDOWN:
  37.             if event.key == pygame.K_ESCAPE:
  38.                 running = False
  39.  
  40.  
  41. def grid():
  42.  
  43.     for x in range(0, WIDTH, TILESIZE):
  44.         pygame.draw.line(screen, GRID, (x, 0), (x, HEIGHT), 1)
  45.  
  46.     for y in range(0, HEIGHT, TILESIZE):
  47.         pygame.draw.line(screen, GRID, (0, y), (WIDTH, y), 1)
  48.  
  49.  
  50. def life(x, y):
  51.  
  52.     global populated
  53.  
  54.     X_TILESIZE = x * TILESIZE
  55.     Y_TILESIZE = y * TILESIZE
  56.  
  57.     location = [X_TILESIZE, Y_TILESIZE]
  58.  
  59.     populated.append(location)
  60.  
  61.     del location
  62.  
  63.  
  64. def death(location):
  65.  
  66.     global unpopulated
  67.  
  68.     unpopulated.append(location)
  69.  
  70.     del location
  71.  
  72.  
  73. def factory():
  74.  
  75.     life(10, 10)
  76.     life(10, 11)
  77.     life(10, 12)
  78.  
  79.     life(20, 20)
  80.     life(21, 20)
  81.     life(22, 20)
  82.     life(21, 19)
  83.     life(20, 19)
  84.     life(19, 19)
  85.  
  86.     life(38, 10)
  87.     life(38, 11)
  88.     life(38, 12)
  89.     life(38, 13)
  90.     life(38, 14)
  91.  
  92.     life(40, 10)
  93.     life(40, 14)
  94.  
  95.     life(42, 10)
  96.     life(42, 11)
  97.     life(42, 12)
  98.     life(42, 13)
  99.     life(42, 14)
  100.  
  101.  
  102. def render():
  103.  
  104.     global populated
  105.  
  106.     screen.fill(BACKGROUND)
  107.  
  108.     for organism in populated:
  109.  
  110.         pygame.draw.rect(screen, ORGANISM, (organism, [TILESIZE, TILESIZE]))
  111.  
  112.     grid()
  113.  
  114.     pygame.display.flip()
  115.  
  116.  
  117. def duplication():
  118.  
  119.     global location
  120.     global i
  121.  
  122.     duplicate = False
  123.  
  124.     if i == 1:
  125.         death(location)
  126.     else:
  127.         for empty in unpopulated:
  128.             if empty == location:
  129.                 duplicate = True
  130.         if duplicate == False:
  131.             death(location)
  132.         duplicate = False
  133.  
  134.  
  135. def rule():
  136.  
  137.     global unpopulated
  138.     global populated
  139.     global location
  140.     global running
  141.     global i
  142.    
  143.     unpopulated = []
  144.  
  145.     i = 1
  146.  
  147.     for organism in populated:
  148.        
  149.         numOfNeighbour = 0
  150.  
  151.         existR = False
  152.         existL = False
  153.         existU = False
  154.         existD = False
  155.         existCUR = False
  156.         existCDR = False
  157.         existCUL = False
  158.         existCDL = False
  159.  
  160.         for neighbour in populated:
  161.  
  162.             if neighbour != organism:
  163.                
  164.                 if neighbour[0] == organism[0] + TILESIZE and neighbour[1] == organism[1]:
  165.                     numOfNeighbour += 1
  166.                     existR = True
  167.        
  168.                 elif neighbour[0] == organism[0] - TILESIZE and neighbour[1] == organism[1]:
  169.                     numOfNeighbour += 1
  170.                     existL = True
  171.  
  172.                 elif neighbour[1] == organism[1] + TILESIZE and neighbour[0] == organism[0]:
  173.                     numOfNeighbour += 1
  174.                     existD = True
  175.  
  176.                 elif neighbour[1] == organism[1] - TILESIZE and neighbour[0] == organism[0]:
  177.                     numOfNeighbour += 1
  178.                     existU = True
  179.        
  180.                 elif neighbour[0] == organism[0] + TILESIZE and neighbour[1] == organism[1] - TILESIZE:
  181.                     numOfNeighbour += 1
  182.                     existCUR = True
  183.  
  184.                 elif neighbour[0] == organism[0] + TILESIZE and neighbour[1] == organism[1] + TILESIZE:
  185.                     numOfNeighbour += 1
  186.                     existCDR = True
  187.  
  188.                 elif neighbour[0] == organism[0] - TILESIZE and neighbour[1] == organism[1] - TILESIZE:
  189.                     numOfNeighbour += 1
  190.                     existCUL = True
  191.        
  192.                 elif neighbour[0] == organism[0] - TILESIZE and neighbour[1] == organism[1] + TILESIZE:
  193.                     numOfNeighbour += 1
  194.                     existCDL = True
  195.  
  196.         if existR == False:
  197.             location = [organism[0] + TILESIZE, organism[1]]
  198.             duplication()
  199.  
  200.         if existL == False:
  201.             location = [organism[0] - TILESIZE, organism[1]]
  202.             duplication()
  203.  
  204.         if existU == False:
  205.             location = [organism[0], organism[1] - TILESIZE]
  206.             duplication()
  207.  
  208.         if existD == False:
  209.             location = [organism[0], organism[1] + TILESIZE]
  210.             duplication()
  211.  
  212.         if existCUR == False:
  213.             location = [organism[0] + TILESIZE, organism[1] - TILESIZE]
  214.             duplication()
  215.  
  216.         if existCDR == False:
  217.             location = [organism[0] + TILESIZE, organism[1] + TILESIZE]
  218.             duplication()
  219.  
  220.         if existCUL == False:
  221.             location = [organism[0] - TILESIZE, organism[1] - TILESIZE]
  222.             duplication()
  223.  
  224.         if existCDL == False:
  225.             location = [organism[0] - TILESIZE, organism[1] + TILESIZE]
  226.             duplication()
  227.  
  228.         i += 1
  229.  
  230.         if numOfNeighbour < 2 or numOfNeighbour > 3:
  231.             organism.append("die")
  232.  
  233.     for empty in unpopulated:
  234.        
  235.         numOfNeighbour = 0
  236.  
  237.         for neighbour in populated:
  238.                
  239.             if neighbour[0] == empty[0] + TILESIZE and neighbour[1] == empty[1]:
  240.                 numOfNeighbour += 1
  241.                    
  242.             elif neighbour[0] == empty[0] - TILESIZE and neighbour[1] == empty[1]:
  243.                 numOfNeighbour += 1
  244.                
  245.             elif neighbour[1] == empty[1] + TILESIZE and neighbour[0] == empty[0]:
  246.                 numOfNeighbour += 1
  247.                
  248.             elif neighbour[1] == empty[1] - TILESIZE and neighbour[0] == empty[0]:
  249.                 numOfNeighbour += 1
  250.                    
  251.             elif neighbour[0] == empty[0] + TILESIZE and neighbour[1] == empty[1] - TILESIZE:
  252.                 numOfNeighbour += 1
  253.                
  254.             elif neighbour[0] == empty[0] + TILESIZE and neighbour[1] == empty[1] + TILESIZE:
  255.                 numOfNeighbour += 1
  256.                
  257.             elif neighbour[0] == empty[0] - TILESIZE and neighbour[1] == empty[1] - TILESIZE:
  258.                 numOfNeighbour += 1
  259.                    
  260.             elif neighbour[0] == empty[0] - TILESIZE and neighbour[1] == empty[1] + TILESIZE:
  261.                 numOfNeighbour += 1
  262.  
  263.         if numOfNeighbour != 3:
  264.             empty.append("empty")
  265.  
  266.     born = []
  267.  
  268.     for empty in unpopulated:
  269.         if len(empty) == 2:
  270.             born.append(empty)
  271.  
  272.     survive = []
  273.  
  274.     for organism in populated:
  275.         if len(organism) == 2:
  276.             survive.append(organism)
  277.  
  278.     populated = survive + born
  279.  
  280.     del unpopulated
  281.     del survive
  282.     del born
  283.  
  284.  
  285. factory()
  286.  
  287. running = True
  288. while running:
  289.  
  290.     clock.tick(FPS)
  291.     event()
  292.     render()
  293.  
  294.     if len(populated) == 0:
  295.         running = False
  296.  
  297.     rule()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement