Fix/dead pixel lines

This commit is contained in:
Richard Feistenauer 2020-10-26 12:44:34 +00:00
parent 19be460edc
commit acbda2bcf2
5 changed files with 22 additions and 8 deletions

View File

@ -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! And for all others, don't hesitate to open issues when you have problems!
## Changelog ## Changelog
#### 1.0.5
- Fixes black lines in the display between some cell lines/columns
#### 1.0.4 #### 1.0.4
- Adds active state for automaton - Adds active state for automaton
- Adds reactivation method - Adds reactivation method

View File

@ -46,7 +46,7 @@ class PygameEngine:
self._height = window_size[1] self._height = window_size[1]
def write_text(self, pos, text, color=(0, 255, 0)): 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) update_rect = self.__screen.blit(label, pos)
self.update_rectangles(update_rect) self.update_rectangles(update_rect)
@ -67,6 +67,7 @@ class CAWindow:
def __init__(self, def __init__(self,
cellular_automaton: CellularAutomaton, cellular_automaton: CellularAutomaton,
window_size=(1000, 800), window_size=(1000, 800),
stretch_cells=False,
draw_engine=None, draw_engine=None,
state_to_color_cb=None, state_to_color_cb=None,
*args, **kwargs): *args, **kwargs):
@ -74,13 +75,15 @@ class CAWindow:
Creates a window to render a 2D CellularAutomaton. Creates a window to render a 2D CellularAutomaton.
:param cellular_automaton: The automaton to display and evolve :param cellular_automaton: The automaton to display and evolve
:param window_size: The Window size (default: 1000 x 800) :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 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) :param state_to_color_cb: A callback to define the draw color of CA states (default: red for states != 0)
""" """
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self._cellular_automaton = cellular_automaton 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] - 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.__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 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): def _not_at_the_end(self, last_evolution_step):
return self._cellular_automaton.evolution_step < last_evolution_step or last_evolution_step <= 0 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 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): def _redraw_dirty_cells(self):
self.__draw_engine.update_rectangles(list(self.__redraw_dirty_cells())) self.__draw_engine.update_rectangles(list(self.__redraw_dirty_cells()))

View File

@ -64,4 +64,5 @@ class ConwaysCA(CellularAutomaton):
if __name__ == "__main__": 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)

View File

@ -33,7 +33,7 @@ class StarFallAutomaton(CellularAutomaton):
""" Represents an automaton dropping colorful stars """ """ Represents an automaton dropping colorful stars """
def __init__(self): def __init__(self):
super().__init__(dimension=[100, 100], super().__init__(dimension=[200, 200],
neighborhood=MooreNeighborhood(EdgeRule.FIRST_AND_LAST_CELL_OF_DIMENSION_ARE_NEIGHBORS)) neighborhood=MooreNeighborhood(EdgeRule.FIRST_AND_LAST_CELL_OF_DIMENSION_ARE_NEIGHBORS))
def init_cell_state(self, __) -> Sequence: def init_cell_state(self, __) -> Sequence:
@ -52,4 +52,6 @@ def state_to_color(current_state: Sequence) -> Sequence:
if __name__ == "__main__": 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()

View File

@ -7,7 +7,7 @@ with open('README.md') as f:
setup( setup(
name="cellular_automaton", name="cellular_automaton",
version="1.0.4", version="1.0.5",
author="Richard Feistenauer", author="Richard Feistenauer",
author_email="r.feistenauer@web.de", author_email="r.feistenauer@web.de",
packages=find_packages(exclude=('tests', 'docs', 'examples')), packages=find_packages(exclude=('tests', 'docs', 'examples')),