neuropercolation/scripts/main_ui.py

49 lines
1.6 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):
2019-01-05 14:12:07 +00:00
def evolve_cell(self, cell, iteration_index):
active = False
2019-01-05 14:12:07 +00:00
neighbors = cell.neighbours
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)
2019-01-05 14:12:07 +00:00
cell.is_set_for_redraw = True
2018-12-09 10:20:16 +00:00
active = True
2018-12-02 16:45:08 +00:00
elif len(neighbors) == 8:
2019-01-05 13:54:06 +00:00
left_neighbour_state = neighbors[0].state.get_status_of_iteration(iteration_index - 1)
2018-12-09 14:25:12 +00:00
active = cell.state.set_status_of_iteration(left_neighbour_state, iteration_index)
if active:
2019-01-05 14:12:07 +00:00
cell.is_set_for_redraw = True
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
2019-01-05 14:45:09 +00:00
if self._state_slots[iteration % 2][0]:
2018-12-09 10:20:16 +00:00
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()
2019-01-05 13:54:06 +00:00
ca = CellularAutomaton([400, 400], MooreNeighborhood(EdgeRule.FIRST_AND_LAST_CELL_OF_DIMENSION_ARE_NEIGHBORS), rule)
2019-01-05 14:12:07 +00:00
ca_window = PyGameFor2D([1000, 800], ca, 5)
2018-12-02 16:45:08 +00:00
ca_window.main_loop()