neuropercolation/scripts/main_ui.py

103 lines
3.9 KiB
Python
Raw Normal View History

2018-12-01 14:14:22 +00:00
#!/usr/bin/env python3
import pygame
import random
2018-12-02 16:45:08 +00:00
import time
2018-12-01 14:14:22 +00:00
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-01 14:14:22 +00:00
class WorldGeneratorWindow:
2018-12-02 16:45:08 +00:00
def __init__(self, windows_size: list, cellular_automaton: CellularAutomaton):
2018-12-01 14:14:22 +00:00
self.window_size = windows_size
2018-12-02 16:45:08 +00:00
self.grid_size = self.window_size.copy()
self.grid_size[1] -= 20
2018-12-01 14:14:22 +00:00
pygame.init()
pygame.display.set_caption("World Generator")
2018-12-02 16:45:08 +00:00
self.screen = pygame.display.set_mode(self.grid_size)
self._cellular_automaton = cellular_automaton
self.font = pygame.font.SysFont("monospace", 15)
def set_cellular_automaton(self, cellular_automaton):
self._cellular_automaton = cellular_automaton
def _display_cellular_automaton(self):
grid_dimension = self._cellular_automaton.grid.get_dimension()
cell_size = [x / y for x, y in zip(self.grid_size, grid_dimension)]
surfaces_to_update = []
2018-12-08 21:33:13 +00:00
for cell in self._cellular_automaton.grid.get_cells().values():
if not cell.is_set_for_redrawing():
2018-12-02 16:45:08 +00:00
continue
cell_coordinate = cell.coordinate
status = cell.get_status_for_iteration(self._cellular_automaton.get_iteration_index())
if status is None:
status = [0]
red = 0
if status[0] >= 10:
red = 255
cell_color = [red, 0, 0]
surface_pos = [x * y for x, y in zip(cell_size, cell_coordinate)]
surface_pos[1] += 20
surfaces_to_update.append(self.screen.fill(cell_color, (surface_pos, cell_size)))
2018-12-08 21:33:13 +00:00
cell.release_from_redraw()
2018-12-02 16:45:08 +00:00
pygame.display.update(surfaces_to_update)
def main_loop(self):
running = True
while running:
time_ca_start = time.time()
2018-12-08 21:33:13 +00:00
self._cellular_automaton.evolve_x_times(10)
2018-12-02 16:45:08 +00:00
time_ca_end = time.time()
self._display_cellular_automaton()
time_ds_end = time.time()
self._print_process_duration(time_ca_end, time_ca_start, time_ds_end)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
def _print_process_duration(self, time_ca_end, time_ca_start, time_ds_end):
self.screen.fill([0, 0, 0], ((0, 0), (self.window_size[0], 30)))
self._write_text((10, 5), "CA: " + "{0:.4f}".format(time_ca_end - time_ca_start) + "s")
self._write_text((310, 5), "Display: " + "{0:.4f}".format(time_ds_end - time_ca_end) + "s")
def _write_text(self, pos, text, color=(0, 255, 0)):
label = self.font.render(text, 1, color)
update_rect = self.screen.blit(label, pos)
pygame.display.update(update_rect)
class TestRule(Rule):
def evolve_cell(self, cell, neighbors, iteration_index):
active = False
2018-12-02 16:45:08 +00:00
last_iteration = iteration_index - 1
if cell.get_status_for_iteration(last_iteration) is None:
rand = random.randrange(0, 101, 1)
if rand <= 99:
rand = 0
cell.set_status_for_iteration([rand], iteration_index)
cell.set_status_for_iteration([rand], iteration_index + 1)
if rand != 0:
active = True
2018-12-08 21:33:13 +00:00
cell.set_for_redraw()
2018-12-02 16:45:08 +00:00
elif len(neighbors) == 8:
left_neighbour_status = neighbors[3].get_status_for_iteration(last_iteration)
active = cell.set_status_for_iteration(left_neighbour_status, 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-01 14:14:22 +00:00
if __name__ == "__main__":
2018-12-01 19:37:18 +00:00
rule = TestRule()
2018-12-08 21:33:13 +00:00
ca = CellularAutomaton([400, 400], MooreNeighborhood(EdgeRule.IGNORE_EDGE_CELLS), rule)
2018-12-02 16:45:08 +00:00
ca_window = WorldGeneratorWindow([1000, 730], ca)
ca_window.main_loop()