-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# Wolfram's cellular automata + some color | ||
# Inspired by something made by Carlos @vamoss a long time ago. | ||
from random import choice | ||
import numpy as np | ||
import py5 | ||
|
||
rows = cols = 100 | ||
|
||
def setup(): | ||
py5.size(800, 800) | ||
py5.no_smooth() | ||
start() | ||
|
||
def start(): | ||
global matrix, gen, ruleset | ||
matrix = np.zeros((cols, rows), dtype=int) | ||
matrix[cols // 2, 0] = 1 # Starts with "1" in the middle of the first line | ||
#matrix[:, 0] = [choice((0, 1)) for _ in range(cols)] | ||
gen = 0 | ||
#ruleset = [choice((0, 1)) for _ in range(8)] | ||
ruleset = [1, 0, 1, 0, 0, 1, 1, 0] | ||
|
||
print(ruleset) | ||
|
||
|
||
def draw(): | ||
py5.background(200) | ||
py5.scale(8) | ||
offset = gen % rows | ||
for i in range(cols): | ||
for j in range(rows): | ||
y = j - offset | ||
if y <= 0: | ||
y = rows + y | ||
if matrix[i, j] == 1: # Se a célula i, está viva/ativa/1 | ||
left = matrix[(i + cols - 1) % cols, j - 1] | ||
centre = matrix[i, j - 1] | ||
right = matrix[(i + 1) % cols, j - 1] | ||
c = py5.color(55 + left * 100, 55 + centre * 100, 55 + right * 100) | ||
py5.stroke(c) | ||
else: | ||
py5.stroke(200) | ||
py5.point(i, y - 1) | ||
new_gen() | ||
|
||
def new_gen(): | ||
global gen | ||
for i in range(cols): | ||
left = matrix[(i + cols - 1) % cols, gen % rows] | ||
centre = matrix[i, gen % rows] | ||
right = matrix[(i + 1) % cols, gen % rows] | ||
result = ruleset[left * 4 + centre * 2 + right * 1] | ||
matrix[i, (gen + 1) % rows] = result | ||
gen += 1 | ||
|
||
def key_pressed(): | ||
if py5.key == ' ': | ||
start() | ||
else: | ||
py5.save_frame('####.png') | ||
|
||
py5.run_sketch() |