-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
173 lines (156 loc) · 5.11 KB
/
main.py
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# Imports
import pygame, sys, random
# Variables
# Colors
BLUE = (10, 10, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
# Screen Size
SCREEN_X = 800
SCREEN_Y = 800
# grid cell size
blockSize = 20
def main():
global SCREEN, CLOCK, color, WHITE, liveCellPositions, running
pygame.init()
SCREEN = pygame.display.set_mode((SCREEN_X, SCREEN_Y))
CLOCK = pygame.time.Clock()
SCREEN.fill(BLACK)
liveCellPositions = []
color = 0
FPS = 30
running = False
drawGrid()
while True:
# loop through all events
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Detect keyboard input
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_0:
print("cleared")
liveCellPositions = []
updateAll(liveCellPositions)
running = False
if event.key == pygame.K_SPACE:
print("simulation has begun")
running = True
# Detect Mouse Input (on click)
if event.type == pygame.MOUSEBUTTONUP and running == False:
mousePos = pygame.mouse.get_pos()
color = SCREEN.get_at(pygame.mouse.get_pos())
# Rect needs to be filled if the color is not white
if color != WHITE:
liveCellPositions.append(toGridSpace(mousePos))
updateAll(liveCellPositions)
# Rect needs to be filled with black
elif color == WHITE:
pos = toGridSpace(mousePos)
print(pos)
index = liveCellPositions.index(pos)
liveCellPositions.remove(liveCellPositions[index])
updateAll(liveCellPositions)
# Simulate
if running == True:
updateAll(liveCellPositions)
pygame.display.update()
CLOCK.tick(FPS)
# Convert to gridSpace
def toGridSpace(val):
final = []
final.append(val[0] // blockSize)
final.append(val[1] // blockSize)
return tuple(final)
def updateAll(liveCells):
SCREEN.fill(BLACK)
global liveCellPositions
if running == True:
liveCells = simulateCells(liveCells)
liveCellPositions = liveCells
drawLiveCells(liveCells)
def drawLiveCells(liveCells):
for x in range(SCREEN_X // blockSize):
for y in range(SCREEN_Y // blockSize):
rect = pygame.Rect(x * blockSize, y * blockSize, blockSize,
blockSize)
pos = [x, y]
if tuple(pos) in liveCells:
pygame.draw.rect(SCREEN, WHITE, rect, 0)
else:
pygame.draw.rect(SCREEN, BLACK, rect, 0)
pygame.draw.rect(SCREEN, BLUE, rect, 1)
def drawGrid():
for x in range(SCREEN_X // blockSize):
for y in range(SCREEN_Y // blockSize):
rect = pygame.Rect(x * blockSize, y * blockSize, blockSize,
blockSize)
pygame.draw.rect(SCREEN, BLUE, rect, 1)
def simulateCells(lastCells):
newCells = []
for x in range(SCREEN_X // blockSize):
for y in range(SCREEN_Y // blockSize):
alive = False
if tuple([x, y]) in lastCells:
alive = True
totalNeighbors = 0
# cell top-mid check
# #?#
# ###
# ###
if tuple([x - 1, y]) in lastCells:
totalNeighbors += 1
# cell top-right check
# ##?
# ###
# ###
if tuple([x - 1, y + 1]) in lastCells:
totalNeighbors += 1
# cell mid-right check
# ###
# ##?
# ###
if tuple([x, y + 1]) in lastCells:
totalNeighbors += 1
# cell bottom-right check
# ###
# ###
# ##?
if tuple([x + 1, y + 1]) in lastCells:
totalNeighbors += 1
# cell bottom-mid check
# ###
# ###
# #?#
if tuple([x + 1, y]) in lastCells:
totalNeighbors += 1
# cell bottom-left check
# ###
# ###
# ?##
if tuple([x + 1, y - 1]) in lastCells:
totalNeighbors += 1
# cell mid-left check
# ###
# ?##
# ###
if tuple([x, y - 1]) in lastCells:
totalNeighbors += 1
# cell top-left check
# ?##
# ###
# ###
if tuple([x - 1, y - 1]) in lastCells:
totalNeighbors += 1
# apply rules based on neighbor count
if totalNeighbors == 2 and alive or totalNeighbors == 3 and alive:
newCells.append(tuple([x, y]))
elif totalNeighbors == 3 and not alive:
newCells.append(tuple([x, y]))
print("done")
return newCells
# Run This Script
main()