from random import choice


class RandomWalk:

    def __init__(self, num_points=1000):
        # 随机漫步的最大距离
        self.num_poimts = num_points
        # 随机漫步的'脚步'
        self.x = [0]
        self.y = [0]

    def fill_walk(self):
        """生成随机漫步的脚印"""
        while len(self.x) < self.num_poimts:
            # 前进方向
            x_direction = choice([1, -1])
            # 前进的距离
            x_distance = choice([0, 1, 2, 3, 4])
            x_step = x_direction * x_distance

            # 前进方向
            y_direction = choice([1, -1])
            # 前进的距离
            y_distance = choice([0, 1, 2, 3, 4])
            y_step = y_direction * y_distance

            # 拒绝原地踏步
            if x_step == 0 and y_step == 0:
                continue
            # 生成下一步
            next_x = self.x[-1] + x_step
            next_y = self.y[-1] + y_step

            self.x.append(next_x)
            self.y.append(next_y)

 

import matplotlib.pyplot as plt
from random_walk import RandomWalk

rw = RandomWalk()
rw.fill_walk()
point_num = list(range(rw.num_poimts))
plt.scatter(rw.x, rw.y, c=point_num, cmap=plt.cm.Blues, edgecolors='none', s=15)
# 突出起点和终点
plt.scatter(0, 0, c='green', edgecolors='none', s=100)
plt.scatter(rw.x[-1], rw.y[-1], c='red', edgecolors='none', s=100)
# 隐藏坐标轴
plt.axes().get_xaxis().set_visible(False)
plt.axes().get_yaxis().set_visible(False)
plt.show()