import tkinter as tk
window = tk.Tk()
#创建窗口
window.title('my window')
#窗口标题
window.geometry('500x200')
#窗口大小
UNIT = 40
# pixels (之后画图中内容用,表示一个小格子占几个像素)
MAZE_H = 4
# grid height(之后画图中内容用,表示纵向有几个小格子)
MAZE_W = 4
# grid width(之后画图中内容用,表示横向有几个小格子)
canvas = tk.Canvas(
bg='white',
height=MAZE_H * UNIT,
width=MAZE_W * UNIT)
for c in range(0, MAZE_W * UNIT, UNIT):
x0, y0, x1, y1 = c, 0, c, MAZE_H * UNIT
canvas.create_line(x0, y0, x1, y1)
#横线 (起点xy 终点xy)
for r in range(0, MAZE_H * UNIT, UNIT):
x0, y0, x1, y1 = 0, r, MAZE_W * UNIT, r
canvas.create_line(x0, y0, x1, y1)
#竖线(起点xy,终点xy)
每一个creat_line都表示画一条从(x0,y0)到(x1,y1)的直线
比如第一个,画的是[(0,0)->(0,160) ; (40,0)->(40,160) ; (80,0)->(80,160) ; (120,0)->(120,160),也就是所有的水平线
第二个,画的是[(0,0)->(160,0) ; (0,40)->(160,40) ; (0,80)->(160,80) ; (0,120)->(160,120),也就是所有的竖直线
hell1_center = origin + np.array([UNIT * 2, UNIT])
hell1 = canvas.create_rectangle(
hell1_center[0] - 15, hell1_center[1] - 15,
hell1_center[0] + 15, hell1_center[1] + 15,
fill='black')
hell2_center = origin + np.array([UNIT, UNIT * 2])
hell2 =canvas.create_rectangle(
hell2_center[0] - 15, hell2_center[1] - 15,
hell2_center[0] + 15, hell2_center[1] + 15,
fill='black')
rect = canvas.create_rectangle(
origin[0] - 15, origin[1] - 15,
origin[0] + 15, origin[1] + 15,
fill='red')
其中,create_rectangle 中的参数是左上角和右下角两个点的坐标
【或者这么理解,和画椭圆是一样的参数分别是椭圆 左极值点、上极值点、右极值点、下极值点(顺时针转一圈)】
oval_center = origin + UNIT * 2
oval = canvas.create_oval(
oval_center[0] - 15, oval_center[1] - 15,
oval_center[0] + 15, oval_center[1] + 15,
fill='yellow')
参数分别是椭圆 左极值点、上极值点、右极值点、下极值点(顺时针转一圈)
和之前的一样
canvas.pack()
window.mainloop()
注:这个教程是辅助Q-learning 算法更新 - 强化学习 (Reinforcement Learning) | 莫烦Python (mofanpy.com)
中没有涉及的Maze 类
参考内容:Reinforcement-learning-with-tensorflow/maze_env.py at master · MorvanZhou/Reinforcement-learning-with-tensorflow (github.com)