学习了tkinter之后,想用它来练练手,于是制作了一个迷宫游戏,如下。
游戏截图:
代码:
import tkinter
root = tkinter.Tk()
root.title('走出迷宫')
root.geometry('500x500+500+100')
canvas = tkinter.Canvas(root, width=500, height=400, bg='white')
canvas.pack()
# 横线
class Horizontal_line:
def __init__(self, start_point=(30, 20)):
self.start_point = start_point
self.distance = 20
self.end_point = (self.start_point[0] + self.distance, self.start_point[1])
def draw_h_line(self, point: list):
canvas.create_line(point, width=1)
# 竖线
class Vertical_line:
def __init__(self, start_point=(30, 20)):
self.start_point = start_point
self.distance = 20
self.end_point = (self.start_point[0], self.start_point[1] + self.distance)
def draw_v_line(self, point: list):
canvas.create_line(point, width=1)
lt_h = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[0, 0, 1, 0, 1, 1, 0, 1, 0, 0],
[0, 1, 1, 1, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 1, 1, 1, 0, 1, 1, 1],
[1, 0, 1, 0, 0, 1, 0, 0, 1, 1],
[0, 1, 1, 0, 1, 0, 0, 0, 1, 0],
[0, 1, 1, 0, 1, 0, 0, 1, 1, 0],
[1, 0, 0, 1, 0, 0, 0, 0, 1, 0],
[0, 0, 1, 1, 1, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 1, 0, 0, 1, 0],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
]
lt_v = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[1, 0, 0, 0, 1, 0, 0, 0, 1, 0],
[0, 0, 1, 1, 0, 1, 0, 1, 0, 1],
[1, 1, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 1, 1, 0, 1, 1, 1, 1],
[0, 1, 1, 0, 1, 0, 0, 1, 1, 0],
[0, 0, 1, 0, 1, 1, 0, 1, 1, 0],
[0, 1, 1, 0, 1, 1, 1, 1, 1, 0],
[0, 1, 0, 0, 0, 1, 1, 1, 0, 0],
[1, 0, 0, 0, 1, 0, 0, 0, 0, 1],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1]
]
# 绘制迷宫
start_point = (140, 140)
start_point_bak = start_point
for i in range(len(lt_h)):
h_line = Horizontal_line(start_point)
for j in range(len(lt_h[0])):
if lt_h[i][j] == 1:
point = [h_line.start_point, h_line.end_point]
h_line.draw_h_line(point)
h_line.start_point = h_line.end_point
h_line.end_point = (h_line.start_point[0] + h_line.distance, h_line.start_point[1])
start_point = (start_point[0], start_point[1] + h_line.distance)
start_point = start_point_bak
for i in range(len(lt_v)):
v_line = Vertical_line(start_point)
for j in range(len(lt_v[0])):
if lt_v[i][j] == 1:
point = [v_line.start_point, v_line.end_point]
v_line.draw_v_line(point)
v_line.start_point = v_line.end_point
v_line.end_point = (v_line.start_point[0], v_line.start_point[1] + v_line.distance)
start_point = (start_point[0] + v_line.distance, start_point[1])
# 绘制方块
x1, y1 = 320, 140
x2, y2 = 340, 160
rect = canvas.create_rectangle(x1, y1, x2, y2, fill='green')
# 设置标签
# 步数标签
count=0 # 步数
str1='步数:'
lb=tkinter.Label(root,width=40,height=1,text=str1+str(count))
lb.place(x=100,y=30)
# 结果标签
lb2=tkinter.Label(root,width=40,height=2,text='---')
lb2.place(x=100,y=60)
# 操作
# 移动一步距离
distance = 20
def move_up():
global y1, y2,count,str1
#判断是否可以上移
# 判断顶部位置
i = (y1 - start_point_bak[1]) // distance
j = (x1 - start_point_bak[0]) // distance
if lt_h[i][j]==0:
y1 -= distance
y2 -= distance
canvas.move(rect, 0, -distance)
count+=1
lb['text']=str1+str(count)
# 检查是否成功
if x1==140 and y1==320:
btn_up.destroy()
btn_down.destroy()
btn_left.destroy()
btn_right.destroy()
lb2['text']='Success!'
def move_down():
global y1, y2,str1,count
i = (y1 - start_point_bak[1]) // distance
j = (x1 - start_point_bak[0]) // distance
if lt_h[i+1][j]==0:
y1 += distance
y2 += distance
canvas.move(rect, 0, distance)
count+=1
lb['text']=str1+str(count)
# 检查是否成功
if x1==140 and y1==320:
btn_up.destroy()
btn_down.destroy()
btn_left.destroy()
btn_right.destroy()
lb2['text']='Success!'
def move_left():
global x1, x2,str1,count
if not (x1==140 and y1==320):
i = (y1 - start_point_bak[1]) // distance
j = (x1 - start_point_bak[0]) // distance
if lt_v[j][i]==0:
x1 -= distance
x2 -= distance
canvas.move(rect, -distance, 0)
count+=1
lb['text']=str1+str(count)
# 检查是否成功
if x1 == 140 and y1 == 320:
btn_up.destroy()
btn_down.destroy()
btn_left.destroy()
btn_right.destroy()
lb2['text'] = 'Success!'
def move_right():
global x1, x2,count,str1
if not (x1==320 and y1==140):
i = (y1 - start_point_bak[1]) // distance
j = (x1 - start_point_bak[0]) // distance
if lt_v[j+1][i]==0:
x1 += distance
x2 += distance
canvas.move(rect, distance, 0)
count+=1
lb['text']=str1+str(count)
# 检查是否成功
if x1 == 140 and y1 == 320:
btn_up.destroy()
btn_down.destroy()
btn_left.destroy()
btn_right.destroy()
lb2['text'] = 'Success!'
btn_up = tkinter.Button(root, text='向上', width=5, height=1, bg='pink', command=move_up)
btn_up.place(x=190, y=410)
btn_down = tkinter.Button(root, text='向下', width=5, height=1, bg='pink', command=move_down)
btn_down.place(x=190, y=440)
btn_left = tkinter.Button(root, text='向左', width=5, height=1, bg='pink', command=move_left)
btn_left.place(x=240, y=410)
btn_right = tkinter.Button(root, text='向右', width=5, height=1, bg='pink', command=move_right)
btn_right.place(x=240, y=440)
root.mainloop()