neuropercolation/scripts/main_ui.py

48 lines
1.5 KiB
Python
Raw Normal View History

2018-12-01 14:14:22 +00:00
#!/usr/bin/env python3
import random
2018-12-01 19:37:18 +00:00
from cellular_automaton.cellular_automaton import CellularAutomaton
2018-12-02 16:45:08 +00:00
from cellular_automaton.ca_rule import Rule
from cellular_automaton.ca_neighborhood import MooreNeighborhood, EdgeRule
2018-12-09 10:20:16 +00:00
from cellular_automaton.ca_display import PyGameFor2D
from cellular_automaton.ca_cell_state import CellState
2018-12-01 14:14:22 +00:00
2018-12-02 16:45:08 +00:00
class TestRule(Rule):
def evolve_cell(self, cell, neighbors, iteration_index):
active = False
2018-12-09 10:20:16 +00:00
if cell.state is None:
2018-12-02 16:45:08 +00:00
rand = random.randrange(0, 101, 1)
if rand <= 99:
2018-12-09 10:20:16 +00:00
cell.state = MyStatus(0)
else:
cell.state = MyStatus(1)
2018-12-08 21:33:13 +00:00
cell.set_for_redraw()
2018-12-09 10:20:16 +00:00
active = True
2018-12-02 16:45:08 +00:00
elif len(neighbors) == 8:
2018-12-09 14:25:12 +00:00
left_neighbour_state = neighbors[3].state.get_status_of_iteration(iteration_index - 1)
active = cell.state.set_status_of_iteration(left_neighbour_state, iteration_index)
if active:
2018-12-08 21:33:13 +00:00
cell.set_for_redraw()
return active
2018-12-01 19:37:18 +00:00
2018-12-09 10:20:16 +00:00
class MyStatus(CellState):
def __init__(self, initial_state):
super().__init__(initial_state)
def get_state_draw_color(self, iteration):
red = 0
if self._state[iteration % 2][0]:
red = 255
return [red, 0, 0]
2018-12-01 14:14:22 +00:00
if __name__ == "__main__":
2018-12-09 14:25:12 +00:00
random.seed(1000)
2018-12-01 19:37:18 +00:00
rule = TestRule()
2018-12-09 14:25:12 +00:00
ca = CellularAutomaton([500, 500], MooreNeighborhood(EdgeRule.IGNORE_EDGE_CELLS), rule)
2018-12-09 10:20:16 +00:00
ca_window = PyGameFor2D([1000, 730], ca)
2018-12-02 16:45:08 +00:00
ca_window.main_loop()