Demo entry 3627902

Science Fair code!!!

   

Submitted by anonymous on Feb 15, 2016 at 05:13
Language: Python. Code size: 6.0 kB.

import pygame, sys
from pygame.locals import *
import random

#Number of frames per second
FPS = 30

###Sets size of grid
WINDOWWIDTH = 400
WINDOWHEIGHT = 300
CELLSIZE = 10

#Check to see if the width and height are multiples of the cell size.
assert WINDOWWIDTH % CELLSIZE == 0, "Try a different window width"
assert WINDOWHEIGHT % CELLSIZE == 0, "Try a different window height"

#Determine number of cells in horizonatl and vertical plane
CELLWIDTH = WINDOWWIDTH / CELLSIZE # number of cells wide
CELLHEIGHT = WINDOWHEIGHT / CELLSIZE # Number of cells high

# set up the colours
BLACK =(0,  0,  0)
WHITE =(255,255,255)
DARKGRAY = (40, 40, 40)
GREEN =(0,255,0)

#Draws the grid lines
def drawGrid():
    for x in range(0, WINDOWWIDTH, CELLSIZE): # draw vertical lines
        pygame.draw.line(DISPLAYSURF, DARKGRAY, (x,0),(x,WINDOWHEIGHT))
    for y in range (0, WINDOWHEIGHT, CELLSIZE): # draw horizontal lines
        pygame.draw.line(DISPLAYSURF, DARKGRAY, (0,y), (WINDOWWIDTH, y))

#Colours the cells green for life and white for no life
def colourGrid(item, lifeDict):
    x = item[0]
    y = item[1]
    y = y * CELLSIZE # translates array into grid size
    x = x * CELLSIZE # translates array into grid size
    if lifeDict[item] == 0:
        pygame.draw.rect(DISPLAYSURF, WHITE, (x, y, CELLSIZE, CELLSIZE))
    if lifeDict[item] == 1:
        pygame.draw.rect(DISPLAYSURF, GREEN, (x, y, CELLSIZE, CELLSIZE))
    return None

#Creates a dictionary of all the cells
#Sets all cells as dead (0)
def blankGrid():
    gridDict = {}
    #creates dictionary for all cells
    for y in range (CELLHEIGHT):
        for x in range (CELLWIDTH):
            gridDict[x,y] = 0 #Sets cells as dead
    return gridDict

#Assigns a 0 or a 1 to all cells
def startingGridRandom(lifeDict):
    for item in lifeDict:
        lifeDict[item] = random.randint(0,1)
    return lifeDict


#Determines how many alive neighbors there are around each cell
def getNeighbours(item,lifeDict):
    neighbours = 0
    for x in range (-1,2):
        for y in range (-1,2):
            checkCell = (item[0]+x,item[1]+y)
            if checkCell[0] < CELLWIDTH  and checkCell[0] >=0:
                if checkCell [1] < CELLHEIGHT and checkCell[1]>= 0:
                    if lifeDict[checkCell] == 1:
                        if x == 0 and y == 0: # negate the central cell
                            neighbours += 0
                        else:
                            neighbours += 1
    return neighbours

#determines the next generation by running a 'tick'
def tick(lifeDict):
    newTick = {}
    for item in lifeDict:
        #get number of neighbours for that item
        numberNeighbours = getNeighbours(item, lifeDict)
        if lifeDict[item] == 1: # For those cells already alive
            if numberNeighbours < 2: # kill under-population
                newTick[item] = 0
            elif numberNeighbours > 4: #kill over-population
                newTick[item] = 0
            else:
                newTick[item] = 1 #stays alive
        elif lifeDict[item] == 0:
            if numberNeighbours == 3: # cell reproduces
                newTick[item] = 1
            else:
                newTick[item] = 0 #stays dead
    return newTick

def toggle(lifeDict, x_pos, y_pos):
    cell_x = x_pos // CELLSIZE
    cell_y = y_pos // CELLSIZE
    cells = (cell_x, cell_y)
    if lifeDict[cells] == 1: #changes cell from alive to dead
        lifeDict[cells] = 0
    elif lifeDict[cells] == 0: #changes cell from dead to alive
        lifeDict[cells] = 1

#main function
def main():
    pygame.init()
    global DISPLAYSURF
    FPSCLOCK = pygame.time.Clock()
    ms = 1000/FPS
    generations = 0
    global continuous
    continuous = False
    DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
    pygame.display.set_caption('Game of Life | Generations Elapsed: ' + str(generations))

    DISPLAYSURF.fill(WHITE)

    lifeDict = blankGrid() # creates library and Populates to match blank grid
    
    #Colors the live cells, blanks the dead
    for item in lifeDict:
        colourGrid(item, lifeDict)

    drawGrid()
    pygame.display.update()
    
    while True: #main game loop
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == MOUSEBUTTONDOWN and event.button == 1:
                toggle(lifeDict, event.pos[0], event.pos[1])
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_s:
                    continuous = False
                    lifeDict = tick(lifeDict)
                    generations += 1
                    pygame.display.set_caption('Game of Life | Generations Elapsed: ' + str(generations))
                elif event.key == pygame.K_c:
                    continuous = True
                    while continuous:
                        lifeDict = tick(lifeDict)
                        for item in lifeDict:
                            colourGrid(item, lifeDict)
                        drawGrid()
                        pygame.display.update()
                        generations += 1
                        pygame.display.set_caption('Game of Life | Generations Elapsed: ' + str(generations))
                        pygame.time.delay(ms)
                        for event in pygame.event.get():
                            if event.type == pygame.KEYDOWN:
                                if event.key == pygame.K_q:
                                    continuous = False
                                    break

        #Colours the live cells, blanks the dead
        for item in lifeDict:
            colourGrid(item, lifeDict)

        drawGrid()
        pygame.display.update()    
        FPSCLOCK.tick(FPS)
        
if __name__=='__main__':
    main()

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).