好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識庫 > python實現數字華容道

python實現數字華容道

熱門標簽:淮安呼叫中心外呼系統如何 佛山通用400電話申請 看懂地圖標注方法 電話機器人貸款詐騙 蘇州人工外呼系統軟件 打印谷歌地圖標注 電話外呼系統招商代理 京華圖書館地圖標注 廣東旅游地圖標注

制作Python數字華容道(可選擇關卡),供大家參考,具體內容如下

由于比賽需要,我這邊制作了一份數字華容道,內含有3,4,5階的數字華容道,開頭在殼窗口內選擇,運用了隨機數模塊(random)和圖形化用戶界面(tkinter)

下面是程序完整代碼

# coding:utf-8 #

"""
#============================================================
作者:@Qss
2021年3月20日起草
2021年3月21日完工
2021年3月23日一次優化完成
2021年3月31日完成二次優化(關卡設計)
二次優化待解決漏洞:設計關卡后窗口不能自動顯示,需手動切換
2021年4月1日三次優化完成,成功解決二次優化bug
#============================================================
"""
from random import *    #導入隨機數模塊
from tkinter import *   #導入圖形化用戶界面模塊
step_number = 0     #設置步數的變量,初始值為0
difficulty = int(input('請輸入數字華容道列數(3/4/5):'))
def Button_Click_1(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(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
        """判斷判斷點擊按鈕旁是否為空白按鈕,是則交換位置"""
        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
        """判斷判斷點擊按鈕旁是否為空白按鈕,是則交換位置"""
        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 = Tk()                      #創建圖形化用戶界面實例
root.title('數字華容道')         #設置窗口標題
root.geometry("400x400")    #將窗口大小設為高400寬400
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)

if difficulty == 3:
    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() #顯示窗口
elif difficulty == 4:
    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() #顯示窗口
elif difficulty == 5:
    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() #顯示窗口
else:
    print('未完成此類關卡')

下面是程序運行結果

三階華容道加勝利

四階華容道加勝利

五階就不傳了,都一樣

接下來說一下代碼原理

首先看下這一段

def Button_Click_1(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(3):
                for col in range(3):
                    """對比所有按鈕序列是否正確,不正確則跳出函數"""
                    if buttons[row,col]['text'] != numbers[n]:  
                        return
                    n += 1
            """所有按鈕判斷完畢贏得游戲勝利"""
            label_welcomes['text'] = '你贏了'

這段是自定義了三個按鈕的點擊事件函數,不過三個都差不多,就是range后面的數字換了(應對不同的階級)。這個函數在注釋上已經寫明了,是判斷點擊按鈕旁是否有空白按鈕的,有則交換位置。后面運用循環嵌套對比序列中的數字和矩陣上的是否一一對應,不對應就跳出循環,對應則判定為贏得游戲。

"""創建華容道游戲窗口"""
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)

這一段創建了圖形化用戶界面,具體每行代碼做什么用的注釋上我都標清楚了。

接著說一下,由于時間有限,只做了在殼窗口內選擇幾階級數字華容道的版本。是用輸入數字來判定的。其他的都是些簡單玩意,比如說按鈕創建,調用函數和循環嵌套,代碼的注釋上都寫了一些大概意思,有點tkinter基礎的應該都能看懂,不會的可以問我。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • 如何在CocosCreator中利用常駐節點做圖層管理
  • 游戲開發中如何使用CocosCreator進行音效處理
  • CocosCreator ScrollView優化系列之分幀加載
  • 詳解CocosCreator項目結構機制
  • 如何使用CocosCreator對象池
  • CocosCreator如何實現劃過的位置顯示紋理
  • 整理CocosCreator常用知識點
  • 39條Python語句實現數字華容道
  • Android數字華容道小游戲開發
  • C/C++仿華容道小游戲
  • 詳解CocosCreator華容道數字拼盤

標簽:股票 駐馬店 江蘇 衡水 中山 呼和浩特 湖州 畢節

巨人網絡通訊聲明:本文標題《python實現數字華容道》,本文關鍵詞  python,實現,數字,華容道,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《python實現數字華容道》相關的同類信息!
  • 本頁收集關于python實現數字華容道的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 婷婷99视频精品全部在线观看| 国产a久久秘?麻豆入口红豆| 被老头玩同性经历| 天堂影院在线观看mv| 入禽太深免费视频完整版视频| 让娇妻尝试三p按摩师高清系列| 久久久久久精品毛片A级按摩 | 国产成人无码精品色欲天香 | 班主任丝袜脚夹茎故事| 泷泽萝拉A片在线观看| 91凹凸精品一区二区在线观看| 国产欧美日韩另类va在线| 中国一级特黄特色**毛片| 韩国福利电影在线观看| 日本做爰无遮A片床戏| 成年人黄国产| 黄色国产| 淫品色| xxxxchinesfreehdvideo| 1000部啪啪未满十八勿入使用测评 | xxxxxhd亚洲日本hd| 白丝少萝疯狂?喷水自慰| 好爽?好紧?再深一点网站| 大胸的年轻岳坶2| 依依成人综合网| 国产色视频一区二区三区QQ号| 男女性高爱潮在线观看| 国产骚妻| 好色电影网| 动漫美女被爆羞羞软件免费| 日本在线免费| 91在线码无精品秘?入口| 久中文字幕中文字幕亚洲无线 | 一区二区三区网站| 2018av在线| ?国产嫩草影院久久久久| 国产精品亚洲专一区二区三区| 久久国产碰在人人看怎么用| 三级全黄做爰在线观看| 婷婷在线综合| 日日爽日日操|