From 84d1701fc8a1ae0038ee3cade499ec6d527e2956 Mon Sep 17 00:00:00 2001 From: hnu202110040318 Date: Sun, 29 May 2022 19:45:51 +0800 Subject: [PATCH] ADD file via upload --- 华容道(完整版).py | 122 ++++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 华容道(完整版).py diff --git a/华容道(完整版).py b/华容道(完整版).py new file mode 100644 index 0000000..e208af9 --- /dev/null +++ b/华容道(完整版).py @@ -0,0 +1,122 @@ +from random import * #导入随机数模块 +from tkinter import * #导入图形化用户界面模块 + +step_number = 0 #设置步数的变量,初始值为0 +difficulty = int(input('请输入数字华容道列数(4/5):')) + +"""创建华容道游戏窗口""" +root = Tk() #创建图形化用户界面实例 +root.title('数字华容道') #设置窗口标题 +root.geometry("400x400") #将窗口大小设为高400宽400 +root.configure(bg = 'SteelBlue') #窗口背景 +"""设置欢迎语的label控件""" +label_welcomes = Label(root,text = '快来展现你的愚蠢吧',bg = 'SteelBlue',fg = 'LightCyan',font = ("华文行楷",18,'underline')) #设置label控件的属性 +label_welcomes.place(x = 40,y = 15,width = 300,height = 40) #设置label控件位置 +"""设置显示操作方式的label控件""" +label_operation = Label(root,text = '单击数字',bg = 'SteelBlue',fg = 'white',font = ("Arial",10)) +label_operation.place(x = 3,y = 40,width = 50,height = 30) +label_operation_2 = Label(root,text = '移动方块',bg = 'SteelBlue',fg = 'white',font = ("Arial",10)) +label_operation_2.place(x = 3,y = 70,width = 50,height = 20) +"""设置显示步数的label控件""" +label_step_number = Label(root,text = '步数:' + str(step_number),bg = 'SteelBlue',fg = 'yellow',font = ("Arial",10)) +label_step_number.place(x = 3,y = 20,width = 50,height = 30) + +def Button_Click_2(x,y): #按钮点击事件函数 + """声明空白按钮行列号和步数的变量为全局变量""" + global row_of_space + global col_of_space + global step_number + + """判断判断点击按钮旁是否为空白按钮""" + 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'] = 'win' + + +def Button_Click_3(x,y): #按钮点击事件函数 + """声明空白按钮行列号和步数的变量为全局变量""" + global row_of_space + global col_of_space + global step_number + """判断判断点击按钮旁是否为空白按钮,是则交换位置""" + 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'] = '你赢了' + + + +root.attributes("-topmost", True) +row_of_space = 0 # 存放空白按钮的行号 +col_of_space = 0 # 存放空白按钮的列号 +buttons = {} # 存放数字按钮的数组 +def hit_me(): + if b['text'] == '开始吧': + print('点击按钮,赠送屠龙宝刀') +b = Button(root,text='开始吧',command=hit_me,bg = 'SteelBlue',fg = 'white',font = ("Arial",10)) +b.pack(side='bottom') + +if difficulty == 4: + 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 = 'Thistle',fg = 'RosyBrown',font = ("Arial",30)) + buttons[row,col] = button #将按钮导入数组 + button['text'] = numbers.pop() #设置按钮上的文本 + button.place(x = 70 + col * 60,y = 70 + 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() #显示窗口 + +elif difficulty == 5: + 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 = 'Thistle',fg = 'RosyBrown',font = ("Arial",30)) + 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() #显示窗口 + +else: + print('未完成此类关卡')