diff --git a/src/pyturmite/constants.py b/src/pyturmite/constants.py index 62b9db5..dd529c3 100644 --- a/src/pyturmite/constants.py +++ b/src/pyturmite/constants.py @@ -1,6 +1,9 @@ -CANVAS_SIZE = 64 -N_STEPS = 8000 +CANVAS_SIZE = 8 +N_STEPS = 6000 PLOT_MODE = "animate" CMAP = "Oranges" -RULESET = "RLLR" +RULESET = "RL" ANIMATION_INTERVAL = 1 +PADDING_SIZE = 2 # expand in each direction by this amount of cells +SAVE_ANIMATION = False +FRAME_SKIP = 2 \ No newline at end of file diff --git a/src/pyturmite/turmites.py b/src/pyturmite/turmites.py index abe0057..b2bb2e7 100644 --- a/src/pyturmite/turmites.py +++ b/src/pyturmite/turmites.py @@ -1,6 +1,6 @@ import numpy as np import matplotlib.pylab as plt -from pyturmite.constants import CMAP, RULESET +from pyturmite.constants import CMAP, RULESET, PADDING_SIZE """ The colors are modified in a cyclic fashion. @@ -101,3 +101,17 @@ def update(self): self.turn(colour) self.change_colour(self.x, self.y) self.move() + + # expand canvas/grid if we moved outside it + if ( + (self.x >= self.grid.shape[0]) or + (self.x < 0) or + (self.y >= self.grid.shape[1]) or + (self.y < 0) + ): + self.expand_grid() + + def expand_grid(self): + self.grid = np.pad(self.grid, PADDING_SIZE) + self.x += PADDING_SIZE + self.y += PADDING_SIZE \ No newline at end of file diff --git a/src/pyturmite/utils/plotters.py b/src/pyturmite/utils/plotters.py index fe3a41e..cf135a3 100644 --- a/src/pyturmite/utils/plotters.py +++ b/src/pyturmite/utils/plotters.py @@ -2,7 +2,7 @@ import numpy as np from matplotlib.animation import FuncAnimation from functools import partial -from pyturmite.constants import ANIMATION_INTERVAL +from pyturmite.constants import ANIMATION_INTERVAL, SAVE_ANIMATION, FRAME_SKIP class Plotter: @@ -48,7 +48,7 @@ def animate(self, turmite, n_steps): ) plt.axis("off") - def update(step, turmite, skip=5): + def update(step, turmite, skip=FRAME_SKIP): for _ in range(skip): turmite.update() data = np.array(turmite.grid) @@ -68,15 +68,12 @@ def update(step, turmite, skip=5): repeat=False, ) - # writer = PillowWriter( - # fps=30, - # ) + if SAVE_ANIMATION: + anim.save( + "example.mp4", + writer="ffmpeg", + fps=120, + progress_callback=lambda i, n: print(f"Saving frame {i}/{n}"), + ) - anim.save( - "example.mp4", - writer="ffmpeg", - fps=120, - progress_callback=lambda i, n: print(f"Saving frame {i}/{n}"), - ) - - # plt.show() + plt.show()