几个月没见到我更新了,是不是以为我不会更新了?最近由于转学以及一些琐事影响,停更了挺久的。当然编程还是在继续坚持(现在在了解C++,以后把它当作辅修)不多说了,直接进入程序。
又双叒叕是数字华容道的编程
关于数字华容道,我好像已经发过两篇文章了
制作Python数字华容道(可选择关卡)
Python数字华容道编辑日记(细讲原理)
不过我在想一个问题,第二篇明明是讲的比第一篇详细很多,第一篇我自己感觉还有些许的水,为什么阅读量相差这么大:
这是一个令人深思的问题
先放上我自己做的编辑记录
""" ======================================================================= 作者:@Qss 2021年3月20日起草 2021年3月21日完工 2021年3月23日一次优化完成 2021年3月31日完成二次优化(关卡设计) 二次优化待解决漏洞:设计关卡后窗口不能自动显示,需手动切换 2021年4月1日三次优化完成,成功解决二次优化bug 2021年10月7日完成四次优化,使关卡选择界面化(二次优化时在壳窗口内选关) ======================================================================= """
这下知道这程序经历了我的多少次摧残 严格修改了吧。
这次最大的修改就是把它完全的界面化
当然,看上去改动了很少,实际上改动了也很少 特别多!
首先,我将之前的if判定全部删除了,这个程序的主题,只有这么短:
"""主程序框架"""
#创建图形化用户界面实例
root_1 = Tk()
root_1.title('数字华容道')
root_1.geometry("380x300")
root_1.configure(bg = 'darkslategray')
root_1.resizable(width = False,height = False)
#设置label控件
label_welcome = Label(root_1,text = '数字华容道',bg = 'darkslategray',fg = 'red',font = ("font",20))
label_welcome.place(x = 70,y = 10,width = 250,height = 40)
#设置按钮
b = Button(root_1, font=('Arial', 12), width=10, height=1, text="3x3", command=game_1, bg='white', fg='black')
b.place(x=140,y=60)
b_1 = Button(root_1, font=('Arial',12), width=10, height=1, text="4x4", command=game_2, bg='white', fg='black')
b_1.place(x=140,y=100)
b_2 = Button(root_1, font=('Arial',12), width=10, height=1, text="5x5", command=game_3, bg='white', fg='black')
b_2.place(x=140,y=140)
其余的,全是自定义函数。
为什么全部要写成函数,问题就出在这两行程序上:
#设置按钮
b = Button(root_1, font=('Arial', 12), width=10, height=1, text="3x3", command=game_1, bg='white', fg='black')
b.place(x=140,y=60)
b_1 = Button(root_1, font=('Arial',12), width=10, height=1, text="4x4", command=game_2, bg='white', fg='black')
b_1.place(x=140,y=100)
b_2 = Button(root_1, font=('Arial',12), width=10, height=1, text="5x5", command=game_3, bg='white', fg='black')
b_2.place(x=140,y=140)
这里的command后面接的是按钮点击触发的函数。没错,只有函数。所以导致我把原来的程序主体变为了函数。
def game_1():
global row_of_space
global col_of_space
global step_number
global difficulty
global label_step_number
global buttons
global numbers
global label_welcomes
step_number = 0
"""创建华容道游戏窗口"""
root = Tk() #创建图形化用户界面实例
root.title('数字华容道') #设置窗口标题
root.geometry("400x400") #将窗口大小设为高300宽300
root.configure(bg = 'black') #将窗口背景设为黑色
root.resizable(width = False,height = False) #设置窗口为不可拉伸
"""设置欢迎语的label控件"""
label_welcomes = Label(root,text = '欢迎来到数字华容道',bg = 'black',fg = 'red',font = ("Arial",13)) #设置label控件的属性
label_welcomes.place(x = 20,y = 10,width = 250,height = 40) #设置label控件位置
"""设置显示操作方式的label控件"""
label_operation = Label(root,text = '单击数字',bg = 'black',fg = 'white',font = ("Arial",10))
label_operation.place(x = 3,y = 40,width = 50,height = 30)
label_operation_2 = Label(root,text = '移动方块',bg = 'black',fg = 'white',font = ("Arial",10))
label_operation_2.place(x = 3,y = 60,width = 50,height = 30)
"""设置显示步数的label控件"""
label_step_number = Label(root,text = '步数:' + str(step_number),bg = 'black',fg = 'yellow',font = ("Arial",10))
label_step_number.place(x = 3,y = 20,width = 50,height = 30)
root.attributes("-topmost", True)
row_of_space = 0 #存放空白按钮的行号
col_of_space = 0 #存放空白按钮的列号
buttons = {} #存放数字按钮的数组
numbers = ['1','2','3','4','5','6','7','8',' '] #所有数字文本的列表
shuffle(numbers) #打乱数字列表中的数字顺序
"""制造数字华容道方阵"""
for row in range(3):
for col in range(3):
"""创建数字按钮,并将行列号传入该按钮的点击事件函数"""
button = Button(root,command = lambda x = row,y = col:Button_Click_1(x,y),bg = 'black',fg = 'green',font = ("Arial",35))
buttons[row,col] = button #将按钮导入数组
button['text'] = numbers.pop() #设置按钮上的文本
button.place(x = 60 + col * 60,y = 60 + row * 60,width = 50,height = 50) #设置数字按钮大小
if button['text'] == ' ': #判断是否为空白按钮,如果是,则记录到空白按钮行列号变量
row_of_space = row
col_of_space = col
numbers = ['1','2','3','4','5','6','7','8',' '] #还原数字列表
root.mainloop() #显示窗口
这里呢注意要声明全局变量,不然程序可不知道你这变量只有函数里起作用还是全局起作用。
这创意说简单嘛,挺简单的。说难嘛,也有点难。毕竟你得想到把原来的游戏主体塞到函数里,让另外几行代码代替成为主体。我暂时还没有看到有人在修改程序的时候敢把主体塞到函数里修改的吧(有可能我没刷到过这种文章)。
好了,接下来把代码放全了,准备接收。
# coding:utf-8 #
"""
=======================================================================
作者:@Qss
2021年3月20日起草
2021年3月21日完工
2021年3月23日一次优化完成
2021年3月31日完成二次优化(关卡设计)
二次优化待解决漏洞:设计关卡后窗口不能自动显示,需手动切换
2021年4月1日三次优化完成,成功解决二次优化bug
2021年10月7日完成四次优化,使关卡选择界面化(二次优化时在壳窗口内选关)
=======================================================================
"""
"""使用模块"""
from random import * #导入随机数模块
from tkinter import * #导入图形化用户界面模块
"""自定义函数"""
#数字华容道
def Button_Click_1(x,y): #按钮点击事件函数
"""声明空白按钮行列号和步数的变量为全局变量"""
global row_of_space
global col_of_space
global step_number
global difficulty
"""判断判断点击按钮旁是否为空白按钮,是则交换位置"""
if abs(x-row_of_space) + abs(y-col_of_space) == 1:
step_number += 1 #将步数赋值
label_step_number['text'] = '步数:' + str(step_number) #将步数变量导入label控件
"""交换按钮位置"""
buttons[row_of_space,col_of_space]['text'] = buttons[x,y]['text']
buttons[x,y]['text'] = ' '
row_of_space = x
col_of_space = y
n = 0
for row in range(3):
for col in range(3):
"""对比所有按钮序列是否正确,不正确则跳出函数"""
if buttons[row,col]['text'] != numbers[n]:
return
n += 1
"""所有按钮判断完毕赢得游戏胜利"""
label_welcomes['text'] = '你赢了'
def Button_Click_2(x,y): #按钮点击事件函数
"""声明空白按钮行列号和步数的变量为全局变量"""
global row_of_space
global col_of_space
global step_number
global difficulty
"""判断判断点击按钮旁是否为空白按钮,是则交换位置"""
if abs(x-row_of_space) + abs(y-col_of_space) == 1:
step_number += 1 #将步数赋值
label_step_number['text'] = '步数:' + str(step_number) #将步数变量导入label控件
"""交换按钮位置"""
buttons[row_of_space,col_of_space]['text'] = buttons[x,y]['text']
buttons[x,y]['text'] = ' '
row_of_space = x
col_of_space = y
n = 0
for row in range(4):
for col in range(4):
"""对比所有按钮序列是否正确,不正确则跳出函数"""
if buttons[row,col]['text'] != numbers[n]:
return
n += 1
"""所有按钮判断完毕赢得游戏胜利"""
label_welcomes['text'] = '你赢了'
def Button_Click_3(x,y): #按钮点击事件函数
"""声明空白按钮行列号和步数的变量为全局变量"""
global row_of_space
global col_of_space
global step_number
global difficulty
"""判断判断点击按钮旁是否为空白按钮,是则交换位置"""
if abs(x-row_of_space) + abs(y-col_of_space) == 1:
step_number += 1 #将步数赋值
label_step_number['text'] = '步数:' + str(step_number) #将步数变量导入label控件
"""交换按钮位置"""
buttons[row_of_space,col_of_space]['text'] = buttons[x,y]['text']
buttons[x,y]['text'] = ' '
row_of_space = x
col_of_space = y
n = 0
for row in range(5):
for col in range(5):
"""对比所有按钮序列是否正确,不正确则跳出函数"""
if buttons[row,col]['text'] != numbers[n]:
return
n += 1
"""所有按钮判断完毕赢得游戏胜利"""
label_welcomes['text'] = '你赢了'
def game_1():
global row_of_space
global col_of_space
global step_number
global difficulty
global label_step_number
global buttons
global numbers
global label_welcomes
step_number = 0
"""创建华容道游戏窗口"""
root = Tk() #创建图形化用户界面实例
root.title('数字华容道') #设置窗口标题
root.geometry("400x400") #将窗口大小设为高300宽300
root.configure(bg = 'black') #将窗口背景设为黑色
root.resizable(width = False,height = False) #设置窗口为不可拉伸
"""设置欢迎语的label控件"""
label_welcomes = Label(root,text = '欢迎来到数字华容道',bg = 'black',fg = 'red',font = ("Arial",13)) #设置label控件的属性
label_welcomes.place(x = 20,y = 10,width = 250,height = 40) #设置label控件位置
"""设置显示操作方式的label控件"""
label_operation = Label(root,text = '单击数字',bg = 'black',fg = 'white',font = ("Arial",10))
label_operation.place(x = 3,y = 40,width = 50,height = 30)
label_operation_2 = Label(root,text = '移动方块',bg = 'black',fg = 'white',font = ("Arial",10))
label_operation_2.place(x = 3,y = 60,width = 50,height = 30)
"""设置显示步数的label控件"""
label_step_number = Label(root,text = '步数:' + str(step_number),bg = 'black',fg = 'yellow',font = ("Arial",10))
label_step_number.place(x = 3,y = 20,width = 50,height = 30)
root.attributes("-topmost", True)
row_of_space = 0 #存放空白按钮的行号
col_of_space = 0 #存放空白按钮的列号
buttons = {} #存放数字按钮的数组
numbers = ['1','2','3','4','5','6','7','8',' '] #所有数字文本的列表
shuffle(numbers) #打乱数字列表中的数字顺序
"""制造数字华容道方阵"""
for row in range(3):
for col in range(3):
"""创建数字按钮,并将行列号传入该按钮的点击事件函数"""
button = Button(root,command = lambda x = row,y = col:Button_Click_1(x,y),bg = 'black',fg = 'green',font = ("Arial",35))
buttons[row,col] = button #将按钮导入数组
button['text'] = numbers.pop() #设置按钮上的文本
button.place(x = 60 + col * 60,y = 60 + row * 60,width = 50,height = 50) #设置数字按钮大小
if button['text'] == ' ': #判断是否为空白按钮,如果是,则记录到空白按钮行列号变量
row_of_space = row
col_of_space = col
numbers = ['1','2','3','4','5','6','7','8',' '] #还原数字列表
root.mainloop() #显示窗口
def game_2():
global row_of_space
global col_of_space
global step_number
global difficulty
global label_step_number
global buttons
global numbers
global label_welcomes
step_number = 0
"""创建华容道游戏窗口"""
root = Tk() #创建图形化用户界面实例
root.title('数字华容道') #设置窗口标题
root.geometry("400x400") #将窗口大小设为高300宽300
root.configure(bg = 'black') #将窗口背景设为黑色
root.resizable(width = False,height = False) #设置窗口为不可拉伸
"""设置欢迎语的label控件"""
label_welcomes = Label(root,text = '欢迎来到数字华容道',bg = 'black',fg = 'red',font = ("Arial",13)) #设置label控件的属性
label_welcomes.place(x = 20,y = 10,width = 250,height = 40) #设置label控件位置
"""设置显示操作方式的label控件"""
label_operation = Label(root,text = '单击数字',bg = 'black',fg = 'white',font = ("Arial",10))
label_operation.place(x = 3,y = 40,width = 50,height = 30)
label_operation_2 = Label(root,text = '移动方块',bg = 'black',fg = 'white',font = ("Arial",10))
label_operation_2.place(x = 3,y = 60,width = 50,height = 30)
"""设置显示步数的label控件"""
label_step_number = Label(root,text = '步数:' + str(step_number),bg = 'black',fg = 'yellow',font = ("Arial",10))
label_step_number.place(x = 3,y = 20,width = 50,height = 30)
root.attributes("-topmost", True)
row_of_space = 0 #存放空白按钮的行号
col_of_space = 0 #存放空白按钮的列号
buttons = {} #存放数字按钮的数组
numbers = ['1','2','3','4','5','6','7','8','9','10','11','12','13','14','15',' '] #所有数字文本的列表
shuffle(numbers) #打乱数字列表中的数字顺序
"""制造数字华容道方阵"""
for row in range(4):
for col in range(4):
"""创建数字按钮,并将行列号传入该按钮的点击事件函数"""
button = Button(root,command = lambda x = row,y = col:Button_Click_2(x,y),bg = 'black',fg = 'green',font = ("Arial",35))
buttons[row,col] = button #将按钮导入数组
button['text'] = numbers.pop() #设置按钮上的文本
button.place(x = 60 + col * 60,y = 60 + row * 60,width = 50,height = 50) #设置数字按钮大小
if button['text'] == ' ': #判断是否为空白按钮,如果是,则记录到空白按钮行列号变量
row_of_space = row
col_of_space = col
numbers = ['1','2','3','4','5','6','7','8','9','10','11','12','13','14','15',' '] #还原数字列表
root.mainloop() #显示窗口
def game_3():
global row_of_space
global col_of_space
global step_number
global difficulty
global label_step_number
global buttons
global numbers
global label_welcomes
step_number = 0
"""创建华容道游戏窗口"""
root = Tk() #创建图形化用户界面实例
root.title('数字华容道') #设置窗口标题
root.geometry("400x400") #将窗口大小设为高300宽300
root.configure(bg = 'black') #将窗口背景设为黑色
root.resizable(width = False,height = False) #设置窗口为不可拉伸
"""设置欢迎语的label控件"""
label_welcomes = Label(root,text = '欢迎来到数字华容道',bg = 'black',fg = 'red',font = ("Arial",13)) #设置label控件的属性
label_welcomes.place(x = 20,y = 10,width = 250,height = 40) #设置label控件位置
"""设置显示操作方式的label控件"""
label_operation = Label(root,text = '单击数字',bg = 'black',fg = 'white',font = ("Arial",10))
label_operation.place(x = 3,y = 40,width = 50,height = 30)
label_operation_2 = Label(root,text = '移动方块',bg = 'black',fg = 'white',font = ("Arial",10))
label_operation_2.place(x = 3,y = 60,width = 50,height = 30)
"""设置显示步数的label控件"""
label_step_number = Label(root,text = '步数:' + str(step_number),bg = 'black',fg = 'yellow',font = ("Arial",10))
label_step_number.place(x = 3,y = 20,width = 50,height = 30)
root.attributes("-topmost", True)
row_of_space = 0 #存放空白按钮的行号
col_of_space = 0 #存放空白按钮的列号
buttons = {} #存放数字按钮的数组
numbers = ['1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24',' '] #所有数字文本的列表
shuffle(numbers) #打乱数字列表中的数字顺序
for row in range(5):
for col in range(5):
"""创建数字按钮,并将行列号传入该按钮的点击事件函数"""
button = Button(root,command = lambda x = row,y = col:Button_Click_3(x,y),bg = 'black',fg = 'green',font = ("Arial",35))
buttons[row,col] = button #将按钮导入数组
button['text'] = numbers.pop() #设置按钮上的文本
button.place(x = 60 + col * 60,y = 60 + row * 60,width = 50,height = 50) #设置数字按钮大小
if button['text'] == ' ': #判断是否为空白按钮,如果是,则记录到空白按钮行列号变量
row_of_space = row
col_of_space = col
numbers = ['1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24',' '] #还原数字列表
root.mainloop() #显示窗口
"""主程序框架"""
#创建图形化用户界面实例
root_1 = Tk()
root_1.title('数字华容道')
root_1.geometry("380x300")
root_1.configure(bg = 'darkslategray')
root_1.resizable(width = False,height = False)
#设置label控件
label_welcome = Label(root_1,text = '数字华容道',bg = 'darkslategray',fg = 'red',font = ("font",20))
label_welcome.place(x = 70,y = 10,width = 250,height = 40)
#设置按钮
b = Button(root_1, font=('Arial', 12), width=10, height=1, text="3x3", command=game_1, bg='white', fg='black')
b.place(x=140,y=60)
b_1 = Button(root_1, font=('Arial',12), width=10, height=1, text="4x4", command=game_2, bg='white', fg='black')
b_1.place(x=140,y=100)
b_2 = Button(root_1, font=('Arial',12), width=10, height=1, text="5x5", command=game_3, bg='white', fg='black')
b_2.place(x=140,y=140)
长还是挺长的
好了,今天文章就到这里,不懂得朋友评论区留言,我会定期巡查评论区
今天没有往期回顾,自己去主页翻吧



