-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathca.py
159 lines (135 loc) · 5.08 KB
/
ca.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
import pygame, sys, random
from pygame.locals import *
Cells = {}
HEIGHT = 600
WIDTH = 600
class classCell:
def __init__(self, x, y, state):
self.x = x
self.y = y
self.w = 10
self.h = 10
self.Lneigh = 0
self.Dneigh = 0
self.state = state
def getState(self):
return self.state
def AddNeigh(self, state):
if state == 1:
self.Lneigh += 1
elif state == 0:
self.Lneigh += 0
def getNeigh(self):
return self.Lneigh
def Draw(self, surf):
if self.state == 0:
self.col = (255, 255, 255)
elif self.state == 1:
self.col = (0, 0, 0)
pygame.draw.rect(surf, self.col, (self.x * self.w, self.y * self.h, self.w, self.h), 0)
def UpdateNeighs():
x = 0
y = 0
for y in range(0, (HEIGHT / 10)):
for x in range(0, (WIDTH / 10)):
Cells[x, y].Lneigh = 0
if y - 1 > -1 and x - 1 > -1 and x + 1 < 40 and y + 1 < 40:
Cells[x, y].AddNeigh(Cells[(x - 1), (y - 1)].state) #Top Left
Cells[x, y].AddNeigh(Cells[(x), (y - 1)].state) #Top
Cells[x, y].AddNeigh(Cells[(x + 1), (y - 1)].state) #Top Right
Cells[x, y].AddNeigh(Cells[(x - 1), (y + 1)].state) #Bottom Left
Cells[x, y].AddNeigh(Cells[(x), (y + 1)].state) #Bottom
Cells[x, y].AddNeigh(Cells[(x + 1), (y + 1)].state) #Bottom Right
Cells[x, y].AddNeigh(Cells[(x - 1), (y)].state) #Left
Cells[x, y].AddNeigh(Cells[(x + 1), (y)].state) #Right
def UpdateStates():
""" the logic of the game """
x = 0
y = 0
for y in range(0, (HEIGHT / 10)):
for x in range(0, (WIDTH / 10)):
if Cells[x, y].Lneigh != 2 and Cells[x, y].Lneigh != 3: # Under/Over populated
Cells[x, y].state = 0
elif Cells[x, y].Lneigh == 2 and Cells[x, y].Lneigh == 3: #Lives to next cycle
Cells[x, y].state = 1
elif Cells[x, y].state == 0 and Cells[x, y].Lneigh == 3: #Dead cell comes to life
print ("a new cell is born")
Cells[x, y].state = 1
class Cursor:
def __init__(self, x, y, w, h, col):
self.x = x
self.y = y
self.w = w
self.h = h
self.col = col
def Move(self, x, y):
self.x = x
self.y = y
def Draw(self, surf):
pygame.draw.rect(surf, self.col, (self.x * self.w, self.y * self.h, self.w, self.h), 1)
def drawGrid(surf, size):
surf.fill((255, 255, 255))
x = 0
y = 0
for y in range(0, (HEIGHT / size)):
for x in range(0, (WIDTH / size)):
Cells[x, y] = classCell(x, y, 0)
pygame.draw.line(surf, (230, 230, 230), (x * size, 0), (x * size, HEIGHT))
pygame.draw.line(surf, (230, 230, 230), (0, y * size), (WIDTH, y * size))
def main():
pygame.init()
clock=pygame.time.Clock()
clock.tick(20) # 20 frames per second
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('Cellular Automata Simulation')
grid = pygame.surface.Surface((WIDTH, HEIGHT))
surf_buf = pygame.surface.Surface((WIDTH, HEIGHT))
CursRect = Cursor(0, 0, 10, 10, (255, 0, 0))
drawGrid(grid, 10)
Cycle = 0
mouse_state = False
continous = False
while True:
for event in pygame.event.get():
if event.type == QUIT:
# exiting ..
pygame.quit()
sys.exit()
return
elif event.type == MOUSEMOTION:
mx, my = pygame.mouse.get_pos()
mx = (mx / 10)
my = (my / 10)
CursRect.Move(mx, my)
if mouse_state:
Cells[mx, my].state = 1
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
print ("Cell %i, %i = 1" % (mx, my))
Cells[mx, my].state = 1
elif event.button == 1:
print Cells[mx, my].Lneigh
mouse_state = not mouse_state
elif event.type == pygame.KEYUP:
# pressing TAB clears the world
if event.key == K_TAB:
print ("tab pressed!, display grid goes forward for one Cycle!")
Cycle = Cycle + 1
print "Cycle ", Cycle
UpdateNeighs()
UpdateStates()
continous = False
if event.key == K_RETURN:
continous = not continous
if continous:
UpdateNeighs()
UpdateStates()
x = 0
y = 0
for y in range(0, (HEIGHT / 10)):
for x in range(0, (WIDTH / 10)):
Cells[x, y].Draw(screen)
CursRect.Draw(screen)
pygame.display.flip()
if __name__ == '__main__':
main()