-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCellularAutomata.py
143 lines (106 loc) · 3.46 KB
/
CellularAutomata.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
from pygame.locals import *
import numpy as np
from random import randint
import pygame
import module
rule = 30
rotateRules = False
#Define Number of Columns
columns = 159
rows = 75
#Define Cell Sizes
WIDTH = 10
HEIGHT = 10
MARGIN = WIDTH // 5
#Define Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREY = (128, 128, 128)
cells = [[module.Cell for i in range(rows)] for j in range(columns)]
for row in range(0, rows):
for column in range(0, columns):
cells[column][row] = (module.Cell(row, column))
for row in range(0, rows):
for column in range(0, columns):
print(cells[column][row].row,cells[column][row].column)
window_height = (((HEIGHT + MARGIN) * rows) + MARGIN)
window_width = (((WIDTH + MARGIN) * columns) + MARGIN)
screen = pygame.display.set_mode((window_width, window_height))
pygame.display.set_caption('Cellular Automata - Rule ' + str(rule))
module.StartSim(cells, rows, columns)
module.RunSim(cells, rows, columns, rule)
grid = []
for row in range(6):
grid.append([])
for column in range(8):
grid[row].append(0)
#pygame.init()
running = True
clock = pygame.time.Clock()
tick_count = 0
row_step = 0
pause = False
speed = 41
#Main pygame loop
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False # Exit loop/ close pygame when x is clicked
key=pygame.key.get_pressed() #checking pressed keys
if key[pygame.K_RETURN]:
row_step = 0
if key[pygame.K_SPACE]:
if pause: pause = False
else: pause = True
if key[pygame.K_DOWN]:
speed = int(speed * 2)
if key[pygame.K_UP]:
if speed > 1:
speed = int(speed * .75)
if key[pygame.K_LEFT]:
rule -= 1
if key[pygame.K_RIGHT]:
rule += 1
screen.fill(GREY)
#update next row every 20 ticks
if not pause:
if (tick_count) % speed == 0:
row_step += 1
for row in range(0, rows):
for column in range(0, columns):
if row <= row_step:
if cells[column][row].color == 0:
color = WHITE
elif cells[column][row].color == 1:
color = BLACK
else:
color = GREY
pygame.draw.rect(screen,
color,
[(MARGIN + WIDTH) * cells[column][row].column + MARGIN,
(MARGIN + HEIGHT) * cells[column][row].row + MARGIN,
WIDTH, HEIGHT])
else:
color = GREY
pygame.draw.rect(screen,
color,
[(MARGIN + WIDTH) * cells[column][row].column + MARGIN,
(MARGIN + HEIGHT) * cells[column][row].row + MARGIN,
WIDTH, HEIGHT])
if row_step == rows:
row_step = 0
if rotateRules:
if rule <= 255:
rule += 1
pygame.display.set_caption('Cellular Automata - Rule ' + str(rule))
module.StartSim(cells, rows, columns)
module.RunSim(cells, rows, columns, rule)
else:
rule = 0
else:
pygame.display.set_caption('Cellular Automata - Rule ' + str(rule))
module.StartSim(cells, rows, columns)
module.RunSim(cells, rows, columns, rule)
tick_count += 1
clock.tick(60)
pygame.display.flip()