diff --git a/neuropercolation/__init__.py b/neuropercolation/__init__.py index 6b02add..0d08251 100644 --- a/neuropercolation/__init__.py +++ b/neuropercolation/__init__.py @@ -18,4 +18,4 @@ limitations under the License. from .neighborhood import Neighborhood, MooreNeighborhood, RadialNeighborhood, VonNeumannNeighborhood, \ HexagonalNeighborhood, EdgeRule from .automaton import Neuropercolation, NeuropercolationCoupled -from .display import CAWindow +from .display import Simulate2Layers diff --git a/neuropercolation/display.py b/neuropercolation/display.py index 19ee899..9e88d55 100644 --- a/neuropercolation/display.py +++ b/neuropercolation/display.py @@ -33,7 +33,7 @@ class PygameEngine: By initializing pygame lazy the dependency can be dropped. """ - def __init__(self, window_size, *args, **kwargs): + def __init__(self, window_size, top, *args, **kwargs): super().__init__(*args, **kwargs) import pygame self._pygame = pygame @@ -43,7 +43,7 @@ class PygameEngine: self.__font = pygame.font.SysFont("monospace", 15) self._width = window_size[0] - self._height = window_size[1] + self._height = window_size[1] + top def write_text(self, pos, text, color=(0, 255, 0)): label = self.__font.render(text, True, color) @@ -63,7 +63,7 @@ class PygameEngine: return True -class CAWindow: +class Simulate2Layers: def __init__(self, cellular_automaton: Neuropercolation, window_size=(1000, 800), @@ -82,9 +82,9 @@ class CAWindow: """ super().__init__(*args, **kwargs) self._cellular_automaton = cellular_automaton - self.__rect = _Rect(left=0, top=30, width=window_size[0], height=window_size[1] - 30) + self.__rect = _Rect(left=0, top=30, width=window_size[0], height=window_size[1]) self.__calculate_cell_display_size(stretch_cells) - self.__draw_engine = PygameEngine(window_size) if draw_engine is None else draw_engine + self.__draw_engine = PygameEngine(window_size, self.__rect.top) if draw_engine is None else draw_engine self.__state_to_color = self._get_cell_color if state_to_color_cb is None else state_to_color_cb def run(self, @@ -125,11 +125,11 @@ class CAWindow: def __calculate_cell_display_size(self, stretch_cells): # pragma: no cover grid_dimension = self._cellular_automaton.dimension if stretch_cells: - self.__cell_size = [self.__rect.width / grid_dimension[0], - self.__rect.height / grid_dimension[1]] + self.__cell_size = [(self.__rect.width) / (grid_dimension[0]), + (self.__rect.height) / (grid_dimension[1]*2)] else: - self.__cell_size = [int(self.__rect.width / grid_dimension[0]), - int(self.__rect.height / grid_dimension[1])] + self.__cell_size = [int((self.__rect.width) / (grid_dimension[0])), + int((self.__rect.height) / (grid_dimension[1]*2))] def _redraw_dirty_cells(self): self.__draw_engine.update_rectangles(list(self.__redraw_dirty_cells())) @@ -141,21 +141,27 @@ class CAWindow: def __redraw_cell(self, cell, coordinate): cell_color = self.__state_to_color(cell.state) + if coordinate[2]==1: + cell_color = cell_color[::-1] cell_pos = self.__calculate_cell_position_in_the_grid(coordinate) surface_pos = self.__calculate_cell_position_on_screen(cell_pos) cell.is_dirty = False return self.__draw_cell_surface(surface_pos, cell_color) - + def _get_cell_color(self, current_state: Sequence) -> Sequence: """ Returns the color of the cell depending on its current state """ return 255 if current_state[0] else 0, 0, 0 - def __calculate_cell_position_in_the_grid(self, coordinate): - return list(map(operator.mul, self.__cell_size, coordinate)) + def __calculate_cell_position_in_the_grid(self, coord): + return list(map(operator.add, + map(operator.mul, + self.__cell_size, + coord[:2]), + [0,(self.__rect.height/2+4)*(coord[2])])) def __calculate_cell_position_on_screen(self, cell_pos): return [self.__rect.left + cell_pos[0], self.__rect.top + cell_pos[1]] - + def __draw_cell_surface(self, surface_pos, cell_color): return self.__draw_engine.fill_surface_with_color((surface_pos, self.__cell_size), cell_color)