Skip to content

Instantly share code, notes, and snippets.

@Berny23
Created May 25, 2021 16:55
Show Gist options
  • Save Berny23/9499b036f1823c96283cb35ecbb18fe0 to your computer and use it in GitHub Desktop.
Save Berny23/9499b036f1823c96283cb35ecbb18fe0 to your computer and use it in GitHub Desktop.
Game of Life Simulation (written in Python 3.9, needs "pygame" library)
# Game of Life Simulation
# Copyright © 2021 Berny23
#
# This file is released under the "MIT" license.
# Go to "https://choosealicense.com/licenses/mit" for full license details.
import pygame as game
import random
######## CONFIG ###########
WINDOW_WIDTH = 1600
WINDOW_HEIGHT = 900
GAME_LOOP_DELAY = 50
GENERATOR_PERCENTAGE = 35
BLOCK_WIDTH = 20
BLOCK_HEIGHT = 20
GRID_ROWS = 41
GRID_COLUMNS = 72
GRID_MARGIN = 2
###########################
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
game.init()
size = (WINDOW_WIDTH, WINDOW_HEIGHT)
screen = game.display.set_mode(size)
exitGame = False
game.display.set_caption("Game of Life Simulation © 2021 Berny23")
clock = game.time.Clock()
# Event that is triggered every x milliseconds:
CYCLE = game.USEREVENT + 1
game.time.set_timer(CYCLE, GAME_LOOP_DELAY)
# Game vars:
width = BLOCK_WIDTH
height = BLOCK_HEIGHT
margin = GRID_MARGIN
rows = GRID_ROWS
columns = GRID_COLUMNS
# Game init:
grid = []
for row in range(rows):
grid.append([])
for column in range(columns):
grid[row].append(1 if random.random() < GENERATOR_PERCENTAGE / 100 else 0)
while not exitGame:
# Game logic:
for event in game.event.get():
if event.type == game.QUIT:
exitGame = True
break
if event.type == CYCLE:
gridNew = []
for row in range(rows):
gridNew.append([])
for column in range(columns):
gridNew[row].append(0)
for row in range(rows):
for column in range(columns):
aliveCounter = 0
cells = list()
# clock-wise, starting here:
# ♦ • •
# • •
# • • •
cells.append(grid[row - 1][column - 1] if row - 1 > 0 and column - 1 else 0)
cells.append(grid[row - 1][column + 0] if row - 1 > 0 else 0)
cells.append(grid[row - 1][column + 1] if row - 1 > 0 and column + 1 < columns else 0)
cells.append(grid[row + 0][column + 1] if column + 1 < columns else 0)
cells.append(grid[row + 1][column + 1] if row + 1 < rows and column + 1 < columns else 0)
cells.append(grid[row + 1][column + 0] if row + 1 < rows else 0)
cells.append(grid[row + 1][column - 1] if row + 1 < rows and column - 1 > 0 else 0)
cells.append(grid[row + 0][column - 1] if column - 1 > 0 else 0)
for cell in cells:
if cell == 1: aliveCounter += 1
gridNew[row][column] = grid[row][column]
if grid[row][column] == 0 and aliveCounter == 3:
gridNew[row][column] = 1
continue
if grid[row][column] == 1 and aliveCounter < 2 or aliveCounter > 3:
gridNew[row][column] = 0
continue
grid = []
for row in range(rows):
grid.append([])
for column in range(columns):
grid[row].append(gridNew[row][column])
# Clear screen:
screen.fill(BLACK)
# Draw on screen:
for row in range(rows):
for column in range(columns):
color = BLACK if grid[row][column] == 0 else WHITE
game.draw.rect(screen, color, [(width + margin) * column + margin, (height + margin) * row + margin, width, height])
# Update screen:
game.display.flip()
# Limit FPS to 60:
clock.tick(60)
game.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment