From acbda2bcf2076711f0e43b6278689dacd7e89648 Mon Sep 17 00:00:00 2001 From: Richard Feistenauer Date: Mon, 26 Oct 2020 12:44:34 +0000 Subject: [PATCH] Fix/dead pixel lines --- README.md | 3 +++ cellular_automaton/display.py | 16 ++++++++++++---- examples/conways_game_of_life.py | 3 ++- examples/simple_star_fall.py | 6 ++++-- setup.py | 2 +- 5 files changed, 22 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 57c5933..65649cc 100644 --- a/README.md +++ b/README.md @@ -98,6 +98,9 @@ There ist still quite some work to do. And for all others, don't hesitate to open issues when you have problems! ## Changelog +#### 1.0.5 +- Fixes black lines in the display between some cell lines/columns + #### 1.0.4 - Adds active state for automaton - Adds reactivation method diff --git a/cellular_automaton/display.py b/cellular_automaton/display.py index 0d81879..d45ca48 100644 --- a/cellular_automaton/display.py +++ b/cellular_automaton/display.py @@ -46,7 +46,7 @@ class PygameEngine: self._height = window_size[1] def write_text(self, pos, text, color=(0, 255, 0)): - label = self.__font.render(text, 1, color) + label = self.__font.render(text, True, color) update_rect = self.__screen.blit(label, pos) self.update_rectangles(update_rect) @@ -67,6 +67,7 @@ class CAWindow: def __init__(self, cellular_automaton: CellularAutomaton, window_size=(1000, 800), + stretch_cells=False, draw_engine=None, state_to_color_cb=None, *args, **kwargs): @@ -74,13 +75,15 @@ class CAWindow: Creates a window to render a 2D CellularAutomaton. :param cellular_automaton: The automaton to display and evolve :param window_size: The Window size (default: 1000 x 800) + :param stretch_cells: Stretches cells to fit into window size. (default: false) + Activating it can result in black lines throughout the automaton. :param draw_engine: The draw_engine (default: pygame) :param state_to_color_cb: A callback to define the draw color of CA states (default: red for states != 0) """ 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.__calculate_cell_display_size() + self.__calculate_cell_display_size(stretch_cells) self.__draw_engine = PygameEngine(window_size) 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 @@ -116,9 +119,14 @@ class CAWindow: def _not_at_the_end(self, last_evolution_step): return self._cellular_automaton.evolution_step < last_evolution_step or last_evolution_step <= 0 - def __calculate_cell_display_size(self): + def __calculate_cell_display_size(self, stretch_cells): # pragma: no cover grid_dimension = self._cellular_automaton.dimension - self.__cell_size = [self.__rect.width / grid_dimension[0], self.__rect.height / grid_dimension[1]] + if stretch_cells: + self.__cell_size = [self.__rect.width / grid_dimension[0], + self.__rect.height / grid_dimension[1]] + else: + self.__cell_size = [int(self.__rect.width / grid_dimension[0]), + int(self.__rect.height / grid_dimension[1])] def _redraw_dirty_cells(self): self.__draw_engine.update_rectangles(list(self.__redraw_dirty_cells())) diff --git a/examples/conways_game_of_life.py b/examples/conways_game_of_life.py index b4e40cf..a49d1ff 100644 --- a/examples/conways_game_of_life.py +++ b/examples/conways_game_of_life.py @@ -64,4 +64,5 @@ class ConwaysCA(CellularAutomaton): if __name__ == "__main__": - CAWindow(cellular_automaton=ConwaysCA()).run(evolutions_per_second=40) + CAWindow(cellular_automaton=ConwaysCA(), + window_size=(1000, 830)).run(evolutions_per_second=40) diff --git a/examples/simple_star_fall.py b/examples/simple_star_fall.py index 3effad9..e8c13c4 100644 --- a/examples/simple_star_fall.py +++ b/examples/simple_star_fall.py @@ -33,7 +33,7 @@ class StarFallAutomaton(CellularAutomaton): """ Represents an automaton dropping colorful stars """ def __init__(self): - super().__init__(dimension=[100, 100], + super().__init__(dimension=[200, 200], neighborhood=MooreNeighborhood(EdgeRule.FIRST_AND_LAST_CELL_OF_DIMENSION_ARE_NEIGHBORS)) def init_cell_state(self, __) -> Sequence: @@ -52,4 +52,6 @@ def state_to_color(current_state: Sequence) -> Sequence: if __name__ == "__main__": - CAWindow(cellular_automaton=StarFallAutomaton(), state_to_color_cb=state_to_color).run() + CAWindow(cellular_automaton=StarFallAutomaton(), + window_size=(1000, 830), + state_to_color_cb=state_to_color).run() diff --git a/setup.py b/setup.py index ccdd9ce..f227d69 100644 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ with open('README.md') as f: setup( name="cellular_automaton", - version="1.0.4", + version="1.0.5", author="Richard Feistenauer", author_email="r.feistenauer@web.de", packages=find_packages(exclude=('tests', 'docs', 'examples')),