Last active
August 29, 2015 14:07
-
-
Save devpruthvi/d1de0c0d2318482948b8 to your computer and use it in GitHub Desktop.
Conway's Game of Life
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pygame, sys | |
from pygame.locals import * | |
FPS = 4 | |
csize = 10 | |
wwidth = int(input("Enter width of the table: "))*csize | |
wheight = int(input("Enter size of the table: "))*csize | |
cwidth = wwidth / csize | |
cheight = wheight / csize | |
blk = (0, 0, 0) | |
wht = (255,255,255) | |
dgray = (40, 40, 40) | |
grn = (0,255,0) | |
def drawGrid(): | |
for x in range(0, wwidth, csize): | |
pygame.draw.line(dispsurf, dgray, (x,0),(x,wheight)) | |
for y in range (0, wheight, csize): | |
pygame.draw.line(dispsurf, dgray, (0,y), (wwidth, y)) | |
def colourGrid(item, mainDict): | |
x = item[0] | |
y = item[1] | |
y = y * csize | |
x = x * csize | |
if mainDict[item] == 0: | |
pygame.draw.rect(dispsurf, wht, (x, y, csize, csize)) | |
if mainDict[item] == 1: | |
pygame.draw.rect(dispsurf, grn, (x, y, csize, csize)) | |
return None | |
def blankGrid(): | |
gridDict = {} | |
for y in range (int(cheight)): | |
for x in range (int(cwidth)): | |
gridDict[x,y] = 0 | |
return gridDict | |
def startinggridinput(mainDict): | |
count = 0 | |
mainkeys = sorted(mainDict, key = lambda x:(x[0],x[1])) | |
for each in range(int(cheight)): | |
inp = input("Enter a full row(0 for dead and 1 for live cell): ") | |
for eac in inp: | |
mainDict[mainkeys[count]] = int(eac) | |
count+=1 | |
return mainDict | |
def getneighb(item,mainDict): | |
neighb = 0 | |
for x in range (-1,2): | |
for y in range (-1,2): | |
check = (item[0]+x,item[1]+y) | |
if check[0] < cwidth and check[0] >=0: | |
if check [1] < cheight and check[1]>= 0: | |
if mainDict[check] == 1: | |
if x == 0 and y == 0: | |
neighb += 0 | |
else: | |
neighb += 1 | |
return neighb | |
def tick(mainDict): | |
nTick = {} | |
for item in mainDict: | |
numberneighb = getneighb(item, mainDict) | |
if mainDict[item] == 1: | |
if numberneighb < 2: | |
nTick[item] = 0 | |
elif numberneighb > 3: | |
nTick[item] = 0 | |
else: | |
nTick[item] = 1 | |
elif mainDict[item] == 0: | |
if numberneighb == 3: | |
nTick[item] = 1 | |
else: | |
nTick[item] = 0 | |
return nTick | |
def main(): | |
pygame.init() | |
global dispsurf | |
FPSCLOCK = pygame.time.Clock() | |
dispsurf = pygame.display.set_mode((wwidth,wheight)) | |
pygame.display.set_caption('Game of Life') | |
dispsurf.fill(wht) | |
mainDict = blankGrid() | |
mainDict = startinggridinput(mainDict) | |
for item in mainDict: | |
colourGrid(item, mainDict) | |
drawGrid() | |
pygame.display.update() | |
while True: | |
for event in pygame.event.get(): | |
if event.type == QUIT: | |
pygame.quit() | |
sys.exit() | |
mainDict = tick(mainDict) | |
for item in mainDict: | |
colourGrid(item, mainDict) | |
drawGrid() | |
pygame.display.update() | |
FPSCLOCK.tick(FPS) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment