0
私はこの迷路を描くプロセスをアニメートしようとしていますが、アニメートステートメントを挿入できるコードのどの位置にあるのか分かりません。この機能をどのように分割する必要がありますか?アニメーションimshow
import numpy
from numpy.random import random_integers as rand
import matplotlib.pyplot as plt
from matplotlib import colors
def maze(width=81, height=51, complexity=.5, density=.5):
# makes sure its only odd shapes
shape = ((width // 2) * 2 + 1, (height // 2) * 2 + 1) #(x, y)
# Adjust complexity and density relative to maze size
complexity = int(complexity * (5 * (shape[0] + shape[1])))
density = int(density * ((shape[0] // 2) * (shape[1] // 2)))
# Build actual maze
Z = numpy.zeros(shape, dtype=int)
# Fill borders
Z[0, :] = Z[-1, :] = 1
Z[:, 0] = Z[:, -1] = 1
# Make aisles
for i in range(density):
x, y = rand(0, shape[0] // 2) * 2, rand(0, shape[1] // 2) * 2
Z[x, y] = 1
for j in range(complexity):
neighbours = []
if x > 1: neighbours.append((x - 2, y))
if x < shape[0] - 2: neighbours.append((x + 2, y))
if y > 1: neighbours.append((x, y - 2))
if y < shape[1] - 2: neighbours.append((x, y + 2))
if len(neighbours):
x_, y_ = neighbours[rand(0, len(neighbours) - 1)]
if Z[x_, y_] == 0:
Z[x_, y_] = 1 # makes it black
Z[x_ + (x - x_) // 2, y_ + (y - y_) // 2] = 1
x, y = x_, y_ # switches to next neighbour
Z[1,1] = 2
Z[shape[0]-2, shape[1]-2] = 3
return Z
plt.figure(figsize=(10, 5))
cmap = colors.ListedColormap(["white", "black", "red", "green"])
bounds=[0, 1, 2, 3]
plt.imshow(maze(41, 51), cmap=cmap, interpolation='nearest')
plt.xticks([]), plt.yticks([])
plt.show()