SHARE
TWEET

game of life finally working




Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
- import random
- import pygame as pg
- import copy
- pg.init()
- screen = pg.display.set_mode((300, 300))
- clock = pg.time.Clock()
- grid_size = 100
- old_grid = [[0 for x in range(grid_size)] for y in range(grid_size)]
- for i in range(0, grid_size):
- for j in range(0, grid_size):
- old_grid[i][j] = random.randrange(0, 2)
- new_grid = copy.deepcopy(old_grid)
- def cell_block(x, y, state):
- cell_size = 3
- pg.draw.rect(screen, (255, 255, 255) if state == 1 else (0, 0, 0), (x * cell_size, y * cell_size, cell_size, cell_size))
- def wrapindex(i):
- return (i % grid_size + grid_size) % grid_size
- running = True
- while running:
- screen.fill((0, 0, 0))
- clock.tick(60)
- for event in pg.event.get():
- if event.type == pg.QUIT:
- running = False
- for x in range(len(old_grid)):
- for y in range(len(old_grid[x])):
- neighbors = old_grid[wrapindex(x - 1)][wrapindex(y - 1)] + old_grid[wrapindex(x)][wrapindex(y - 1)] + old_grid[wrapindex(x + 1)][wrapindex(y - 1)] + \
- old_grid[wrapindex(x - 1)][wrapindex(y)] + 0 + old_grid[wrapindex(x + 1)][wrapindex(y)] + \
- old_grid[wrapindex(x - 1)][wrapindex(y + 1)] + old_grid[wrapindex(x)][wrapindex(y + 1)] + old_grid[wrapindex(x + 1)][wrapindex(y + 1)]
- if neighbors == 3:
- new_grid[x][y] = 1
- elif neighbors > 3:
- new_grid[x][y] = 0
- elif neighbors < 2:
- new_grid[x][y] = 0
- cell_block(x, y, new_grid[x][y])
- pg.display.flip()
- old_grid = copy.deepcopy(new_grid)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.