-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameofLife
141 lines (119 loc) · 3.24 KB
/
GameofLife
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import random
import pygame, sys
from pygame.locals import *
from optparse import OptionParser
#options
parser = OptionParser()
parser.add_option('-w', '--width',
dest='width', default=500,
help='The width of the panel.')
parser.add_option('-f', '--fps',
dest='fps', default=10,
help='The frames per second.')
parser.add_option('-l', '--length',
dest='height', default=500,
help='The length (height) of the panel.')
parser.add_option('-s', '--cell_size',
dest='cell_size', default=10,
help='The size of each individual cell.')
(options, args) = parser.parse_args()
#setting variables from options
fps = int(options.fps)
width = int(options.width)
height = int(options.height)
cell_size = int(options.cell_size)
#other fixed variables
cwidth = width/cell_size
cheight = height/cell_size
black = (0, 0, 0)
white = (255,255,255)
gray = (40, 40, 40)
panel = pygame.display.set_mode((width, height))
#draws the grid on the panel
def drawGrid():
for x in range(0, width, cell_size):
pygame.draw.line(panel, gray, (x, 0), (x, height))
for y in range(0, height, cell_size):
pygame.draw.line(panel, gray, (0, y), (width, y))
#creates a grid of all dead (0) cells
def blankGrid():
grid = {}
for y in range (cheight):
for x in range (cwidth):
grid[x,y] = 0
return grid
#randomly sets each cell to alive (1) or dead (0)
def randomStates(cell_states):
for i in cell_states:
cell_states[i] = random.randint(0,1)
return cell_states
#colors the living cells black and the dead cells white
def colorLiving(i, cell_states):
x = i[0]
y = i[1]
y = y * cell_size
x = x * cell_size
if cell_states[i] == 0:
pygame.draw.rect(panel, white, (x, y, cell_size, cell_size))
if cell_states[i] == 1:
pygame.draw.rect(panel, black, (x, y, cell_size, cell_size))
return None
#counts how many living neighbors a cell has
def neighborCount (i, cell_states):
neighbors = 0
for x in range (-1,2):
for y in range (-1,2):
checkCell = (i[0] + x, i[1] + y)
if checkCell[0] < cwidth and checkCell[0] >= 0:
if checkCell[1] < cheight and checkCell[1] >= 0:
if cell_states[checkCell] == 1:
if x == 0 and y == 0:
neighbors += 0
else:
neighbors += 1
return neighbors
#creates the next generation of cells
def gen (cell_states):
newGen = {}
for i in cell_states:
neighbors = neighborCount(i, cell_states)
if cell_states[i] == 1:
if neighbors < 2:
newGen[i] = 0
elif neighbors > 3:
newGen[i] = 0
else:
newGen[i] = 1
elif cell_states[i] == 0:
if neighbors == 3:
newGen[i] = 1
else:
newGen[i] = 0
return newGen
def main():
#initializes pygame
pygame.init()
#creates a clock for how often to update the display
fpsclock = pygame.time.Clock()
#sets the pygame caption
pygame.display.set_caption('CA')
panel.fill(white)
cell_states = blankGrid()
cell_states = randomStates(cell_states)
for i in cell_states:
colorLiving(i, cell_states)
drawGrid()
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
cell_states = gen(cell_states)
for i in cell_states:
colorLiving(i, cell_states)
drawGrid()
pygame.display.update()
fpsclock.tick(fps)
if __name__ == '__main__':
main()