Skip to content

Commit

Permalink
Create sketch_2025_01_09-10.py
Browse files Browse the repository at this point in the history
  • Loading branch information
villares committed Jan 11, 2025
1 parent 8823d51 commit 59cf070
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions 2025/sketch_2025_01_09/sketch_2025_01_09-10.py
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()

0 comments on commit 59cf070

Please sign in to comment.