Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- WIDTH = 1000
- HEIGHT = 720
- TITLE = "CONWAY'S GAME OF LIFE"
- TILESIZE = 20
- FPS = 2
- BACKGROUND = [20, 20, 20] # default: [20, 20, 20]
- GRID = [0, 0, 0] # default: [0, 0, 0]
- ORGANISM = [255, 255, 255] # default: [255, 255, 255]
- populated = []
- unpopulated = []
- location = []
- i = 1
- pygame.init()
- RESOLUTION = [WIDTH, HEIGHT]
- screen = pygame.display.set_mode(RESOLUTION)
- pygame.display.set_caption(TITLE)
- clock = pygame.time.Clock()
- def event():
- global running
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- running = False
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_ESCAPE:
- running = False
- def grid():
- for x in range(0, WIDTH, TILESIZE):
- pygame.draw.line(screen, GRID, (x, 0), (x, HEIGHT), 1)
- for y in range(0, HEIGHT, TILESIZE):
- pygame.draw.line(screen, GRID, (0, y), (WIDTH, y), 1)
- def life(x, y):
- global populated
- X_TILESIZE = x * TILESIZE
- Y_TILESIZE = y * TILESIZE
- location = [X_TILESIZE, Y_TILESIZE]
- populated.append(location)
- del location
- def death(location):
- global unpopulated
- unpopulated.append(location)
- del location
- def factory():
- life(10, 10)
- life(10, 11)
- life(10, 12)
- life(20, 20)
- life(21, 20)
- life(22, 20)
- life(21, 19)
- life(20, 19)
- life(19, 19)
- life(38, 10)
- life(38, 11)
- life(38, 12)
- life(38, 13)
- life(38, 14)
- life(40, 10)
- life(40, 14)
- life(42, 10)
- life(42, 11)
- life(42, 12)
- life(42, 13)
- life(42, 14)
- def render():
- global populated
- screen.fill(BACKGROUND)
- for organism in populated:
- pygame.draw.rect(screen, ORGANISM, (organism, [TILESIZE, TILESIZE]))
- grid()
- pygame.display.flip()
- def duplication():
- global location
- global i
- duplicate = False
- if i == 1:
- death(location)
- else:
- for empty in unpopulated:
- if empty == location:
- duplicate = True
- if duplicate == False:
- death(location)
- duplicate = False
- def rule():
- global unpopulated
- global populated
- global location
- global running
- global i
- unpopulated = []
- i = 1
- for organism in populated:
- numOfNeighbour = 0
- existR = False
- existL = False
- existU = False
- existD = False
- existCUR = False
- existCDR = False
- existCUL = False
- existCDL = False
- for neighbour in populated:
- if neighbour != organism:
- if neighbour[0] == organism[0] + TILESIZE and neighbour[1] == organism[1]:
- numOfNeighbour += 1
- existR = True
- elif neighbour[0] == organism[0] - TILESIZE and neighbour[1] == organism[1]:
- numOfNeighbour += 1
- existL = True
- elif neighbour[1] == organism[1] + TILESIZE and neighbour[0] == organism[0]:
- numOfNeighbour += 1
- existD = True
- elif neighbour[1] == organism[1] - TILESIZE and neighbour[0] == organism[0]:
- numOfNeighbour += 1
- existU = True
- elif neighbour[0] == organism[0] + TILESIZE and neighbour[1] == organism[1] - TILESIZE:
- numOfNeighbour += 1
- existCUR = True
- elif neighbour[0] == organism[0] + TILESIZE and neighbour[1] == organism[1] + TILESIZE:
- numOfNeighbour += 1
- existCDR = True
- elif neighbour[0] == organism[0] - TILESIZE and neighbour[1] == organism[1] - TILESIZE:
- numOfNeighbour += 1
- existCUL = True
- elif neighbour[0] == organism[0] - TILESIZE and neighbour[1] == organism[1] + TILESIZE:
- numOfNeighbour += 1
- existCDL = True
- if existR == False:
- location = [organism[0] + TILESIZE, organism[1]]
- duplication()
- if existL == False:
- location = [organism[0] - TILESIZE, organism[1]]
- duplication()
- if existU == False:
- location = [organism[0], organism[1] - TILESIZE]
- duplication()
- if existD == False:
- location = [organism[0], organism[1] + TILESIZE]
- duplication()
- if existCUR == False:
- location = [organism[0] + TILESIZE, organism[1] - TILESIZE]
- duplication()
- if existCDR == False:
- location = [organism[0] + TILESIZE, organism[1] + TILESIZE]
- duplication()
- if existCUL == False:
- location = [organism[0] - TILESIZE, organism[1] - TILESIZE]
- duplication()
- if existCDL == False:
- location = [organism[0] - TILESIZE, organism[1] + TILESIZE]
- duplication()
- i += 1
- if numOfNeighbour < 2 or numOfNeighbour > 3:
- organism.append("die")
- for empty in unpopulated:
- numOfNeighbour = 0
- for neighbour in populated:
- if neighbour[0] == empty[0] + TILESIZE and neighbour[1] == empty[1]:
- numOfNeighbour += 1
- elif neighbour[0] == empty[0] - TILESIZE and neighbour[1] == empty[1]:
- numOfNeighbour += 1
- elif neighbour[1] == empty[1] + TILESIZE and neighbour[0] == empty[0]:
- numOfNeighbour += 1
- elif neighbour[1] == empty[1] - TILESIZE and neighbour[0] == empty[0]:
- numOfNeighbour += 1
- elif neighbour[0] == empty[0] + TILESIZE and neighbour[1] == empty[1] - TILESIZE:
- numOfNeighbour += 1
- elif neighbour[0] == empty[0] + TILESIZE and neighbour[1] == empty[1] + TILESIZE:
- numOfNeighbour += 1
- elif neighbour[0] == empty[0] - TILESIZE and neighbour[1] == empty[1] - TILESIZE:
- numOfNeighbour += 1
- elif neighbour[0] == empty[0] - TILESIZE and neighbour[1] == empty[1] + TILESIZE:
- numOfNeighbour += 1
- if numOfNeighbour != 3:
- empty.append("empty")
- born = []
- for empty in unpopulated:
- if len(empty) == 2:
- born.append(empty)
- survive = []
- for organism in populated:
- if len(organism) == 2:
- survive.append(organism)
- populated = survive + born
- del unpopulated
- del survive
- del born
- factory()
- running = True
- while running:
- clock.tick(FPS)
- event()
- render()
- if len(populated) == 0:
- running = False
- rule()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement