J'ai un mis en œuvre jeu de la vie de Conway comme:Comment puis-je animer un ensemble de points avec matplotlib?
def neighbors(point):
x, y = point
for i, j in itertools.product(range(-1, 2), repeat=2):
if any((i, j)):
yield (x + i, y + j)
def advance(board):
newstate = set()
recalc = board | set(itertools.chain(*map(neighbors, board)))
for point in recalc:
count = sum((neigh in board)
for neigh in neighbors(point))
if count == 3 or (count == 2 and point in board):
newstate.add(point)
return newstate
Je veux visualiser le résultat, j'ai donc essayé de modifier l'exemple de Matplotlib animation example:
glider = set([(0, 0), (1, 0), (2, 0), (0, 1), (1, 2)])
fig, ax = plt.subplots()
x, y = zip(*glider)
mat, = ax.plot(x, y, 'o')
def animate(i):
glider = advance(glider)
x, y = zip(*glider)
mat.set_data(x, y)
return mat,
ani = animation.FuncAnimation(fig, animate, interval=50)
plt.show()
mais que les parcelles juste the initial points.
Vous pourriez être intéressé par d'autres implémentations matplotlib du jeu de la vie, comme [celui-ci] (https://stackoverflow.com/questions/45653550/ arrêt-animation-conways-jeu-de-vie) ou [celui-ci] (https://stackoverflow.com/questions/46196346/why-does-my-game-of-life-simulation-slow-down-to- a-crawl-within-seconds-matplot). – ImportanceOfBeingErnest