python?GUI編程實現(xiàn)掃雷游戲
前言
1992年掃雷被加入到windows3.1,成為早期windows的經(jīng)典游戲。近來接觸python的GUI(圖形化)編程,于是通過編寫掃雷來實踐學(xué)習(xí)。有關(guān)程序的問題和想法歡迎大家指出。
一、基本思路
(1)程序的核心數(shù)據(jù)是二維列表control_list[16][16],值-1代表雷,0和其他數(shù)字代表四周雷的數(shù)目。函數(shù)randomization()隨機(jī)40個雷的 位置
(2)生成16x16個按鈕控件,根據(jù)control_list列表確定點(diǎn)擊相應(yīng)的按鈕時執(zhí)行的代碼。如按照游戲規(guī)則:點(diǎn)擊對應(yīng)control_list列表值為0的按鈕時,遞歸執(zhí)行函數(shù)re()來掃雷,當(dāng)點(diǎn)擊對應(yīng)的control_list列表值為-1時直接結(jié)束游戲,對應(yīng)大于0的值則只需隱藏當(dāng)前的按鈕控件。
其他:本程序用到的GUI編程庫為tkinter, 控件的布局統(tǒng)一采用place()方法
二、源代碼
1.運(yùn)行效果
運(yùn)行效果如下(示例):
2.上源碼
""" Created on Tuesday, April 5,2022 @author:I """ from tkinter import * from tkinter import messagebox import random #定義五個二維數(shù)組,充當(dāng)整個程序的數(shù)據(jù) control_list=[[0 for i in range(16)] for j in range(16)]#二維列表,呈現(xiàn)雷和數(shù)字的分布。 show_list=[[0 for i in range(16)] for j in range(16)]#二維列表,控制遮住或顯示雷和數(shù)字。(0--遮住,1--顯示) button_list=[[0 for i in range(16)] for j in range(16)]#二維的按鈕列表(顯示在上層) label_list=[[0 for i in range(16)] for j in range(16)]#二維的標(biāo)簽列表(顯示在下層) mark_list=[[0 for i in range(16)] for j in range(16)]#二維標(biāo)記列表 num_mine=40#控制游戲結(jié)束 counter=0#計時 T ,t= 1,0#游戲結(jié)束的判斷 def randomization(c_list):#隨機(jī)初始化雷的分布即初始化列表control_list ? ? num=0 ? ? while num<40: ? ? ? ? x=random.randint(0,15) ? ? ? ? y=random.randint(0,15) ? ? ? ? if(c_list[x][y]==0): ? ? ? ? ? ? num+=1 ? ? ? ? ? ? c_list[x][y]=-1 ? ? for i in range(16): ? ? ? ? for j in range(16): ? ? ? ? ? ? if(c_list[i][j]>-1): ? ? ? ? ? ? ? ? if (i>0 and c_list[i-1][j]==-1): ? ? ? ? ? ? ? ? ? ? c_list[i][j]+=1 ? ? ? ? ? ? ? ? if (i<15 and c_list[i+1][j]==-1): ? ? ? ? ? ? ? ? ? ? c_list[i][j]+=1 ? ? ? ? ? ? ? ? if (j>0 and c_list[i][j-1]==-1): ? ? ? ? ? ? ? ? ? ? c_list[i][j]+=1 ? ? ? ? ? ? ? ? if (j<15 and c_list[i][j+1]==-1): ? ? ? ? ? ? ? ? ? ? c_list[i][j]+=1 ? ? ? ? ? ? ? ? if (i>0 and j>0 and c_list[i-1][j-1]==-1): ? ? ? ? ? ? ? ? ? ? c_list[i][j]+=1 ? ? ? ? ? ? ? ? if (i<15 and j<15 and c_list[i+1][j+1]==-1): ? ? ? ? ? ? ? ? ? ? c_list[i][j]+=1 ? ? ? ? ? ? ? ? if (i>0 and j<15 and c_list[i-1][j+1]==-1): ? ? ? ? ? ? ? ? ? ? c_list[i][j]+=1 ? ? ? ? ? ? ? ? if (i<15 and j>0 and c_list[i+1][j-1]==-1): ? ? ? ? ? ? ? ? ? ? c_list[i][j]+=1 def game_core(): ? ? randomization(control_list) ? ? for row in range(16): ? ? ? ? for col in range(16): ? ? ? ? ? ? if(control_list[row][col]==-1): ? ? ? ? ? ? ? ? label_list[row][col]=Label(root,text="?",font=('arial', 15, 'bold'),fg="black",bg="#AAAAAA",relief=RIDGE) ? ? ? ? ? ? ? ? label_list[row][col].place(x=17+col*20,y=46+row*20,height=20,width=20) ? ? ? ? ? ? elif(control_list[row][col]==0): ? ? ? ? ? ? ? ? label_list[row][col]=Label(root,text="",bg="#AAAAAA",relief=RIDGE) ? ? ? ? ? ? ? ? label_list[row][col].place(x=17+col*20,y=46+row*20,height=20,width=20) ? ? ? ? ? ? elif(control_list[row][col]==1): ? ? ? ? ? ? ? ? label_list[row][col]=Label(root,text="1",font=('arial', 15, 'bold'),fg="red",bg="#AAAAAA",relief=RIDGE) ? ? ? ? ? ? ? ? label_list[row][col].place(x=17+col*20,y=46+row*20,height=20,width=20) ? ? ? ? ? ? elif(control_list[row][col]==2): ? ? ? ? ? ? ? ? label_list[row][col]=Label(root,text="2",font=('arial', 15, 'bold'),fg="blue",bg="#AAAAAA",relief=RIDGE) ? ? ? ? ? ? ? ? label_list[row][col].place(x=17+col*20,y=46+row*20,height=20,width=20) ? ? ? ? ? ? elif(control_list[row][col]==3): ? ? ? ? ? ? ? ? label_list[row][col]=Label(root,text="3",font=('arial', 15, 'bold'),fg="green",bg="#AAAAAA",relief=RIDGE) ? ? ? ? ? ? ? ? label_list[row][col].place(x=17+col*20,y=46+row*20,height=20,width=20) ? ? ? ? ? ? elif(control_list[row][col]==4): ? ? ? ? ? ? ? ? label_list[row][col]=Label(root,text="4",font=('arial', 15, 'bold'),fg="white",bg="#AAAAAA",relief=RIDGE) ? ? ? ? ? ? ? ? label_list[row][col].place(x=17+col*20,y=46+row*20,height=20,width=20) ? ? ? ? ? ? elif(control_list[row][col]==5): ? ? ? ? ? ? ? ? label_list[row][col]=Label(root,text="5",font=('arial', 15, 'bold'),fg="red",bg="#AAAAAA",relief=RIDGE) ? ? ? ? ? ? ? ? label_list[row][col].place(x=17+col*20,y=46+row*20,height=20,width=20) ? ? ? ? ? ? elif(control_list[row][col]==6): ? ? ? ? ? ? ? ? label_list[row][col]=Label(root,text="6",font=('arial', 15, 'bold'),fg="blue",bg="#AAAAAA",relief=RIDGE) ? ? ? ? ? ? ? ? label_list[row][col].place(x=17+col*20,y=46+row*20,height=20,width=20) ? ? ? ? ? ? elif(control_list[row][col]==7): ? ? ? ? ? ? ? ? label_list[row][col]=Label(root,text="7",font=('arial', 15, 'bold'),fg="green",bg="#AAAAAA",relief=RIDGE) ? ? ? ? ? ? ? ? label_list[row][col].place(x=17+col*20,y=46+row*20,height=20,width=20) ? ? ? ? ? ? elif(control_list[row][col]==8): ? ? ? ? ? ? ? ? label_list[row][col]=Label(root,text="8",font=('arial', 15, 'bold'),fg="white",bg="#AAAAAA",relief=RIDGE) ? ? ? ? ? ? ? ? label_list[row][col].place(x=17+col*20,y=46+row*20,height=20,width=20) ? ? for r in range(16): ? ? ? ? for c in range(16): ? ? ? ? ? ? s = str((r)*16+c) ? ? ? ? ? ? button_list[r][c]=Button(root,text=s,activeforeground="#AAAAAA",bg="#AAAAAA",fg="#AAAAAA") ? ? ? ? ? ? button_list[r][c].place(x=17+c*20,y=46+r*20,height=20,width=20) ? ? ? ? ? ? button_list[r][c].bind("<Button-1>",button_control_l)#鼠標(biāo)左擊綁定函數(shù) ? ? ? ? ? ? button_list[r][c].bind("<Button-3>",button_control_r) def button_control_l(event):#掃雷控制函數(shù).(開始函數(shù)直接用參數(shù)r和c,但是會產(chǎn)生問題) ? ? r = int(event.widget["text"])//16 ? ? c = int(event.widget["text"])%16 ? ? global t ? ? global T ? ? if(control_list[r][c]>=1): ? ? ? ? button_list[r][c].place_forget() ? ? ? ? show_list[r][c]=1 ? ? ? ? t+=1 ? ? elif(control_list[r][c]==0): ? ? ? ? rec(r,c) ? ? elif(control_list[r][c]==-1 and T): ? ? ? ? button_list[r][c].place_forget() ? ? ? ? show_list[r][c]=1 ? ? ? ? T=0 ? ? ? ? for i in range(16): ? ? ? ? ? ? for j in range(16): ? ? ? ? ? ? ? ? if(control_list[i][j]==-1): ? ? ? ? ? ? ? ? ? ? button_list[i][j].place_forget() ? ? ? ? ? ? ? ? ? ? show_list[r][c]=1 ? ? ? ? button_restart["text"]="?" ? ? ? ? messagebox.showwarning("失敗","你已經(jīng)被炸死了!") ? ? if t==216: ? ? ? ? ? ? T=0 ? ? ? ? ? ? messagebox.showwarning("成功","恭喜你掃雷完成!") def button_control_r(event): ? ? r = int(event.widget["text"])//16 ? ? c = int(event.widget["text"])%16 ? ? mark_list[r][c]=Button(root,text="?",font=('楷體', 14),activeforeground="#AAAAAA",bg="#AAAAAA",fg="yellow") ? ? mark_list[r][c].place(x=17+c*20,y=46+r*20,height=20,width=20) ? ? mark_list[r][c].bind("<Button-3>",button_control_r_change) def button_control_r_change(event): ? ? global num_mine ? ? if (event.widget["text"]=="?" and num_mine>0): ? ? ? ? num_mine-=1 ? ? ? ? event.widget["text"]="▲" ? ? ? ? cout_label["text"]=str(num_mine) ? ? elif(event.widget["text"]=="▲"): ? ? ? ? num_mine+=1 ? ? ? ? cout_label["text"]=str(num_mine) ? ? ? ? event.widget.place_forget() ? ? elif (event.widget["text"]=="?" and num_mine==0): ? ? ? ? event.widget.place_forget() def rec(r,c):#遞歸探測 ? ? global t ? ? if control_list[r][c]>0 and show_list[r][c]==0: ? ? ? ? button_list[r][c].place_forget() ? ? ? ? show_list[r][c]=1 ? ? ? ? t+=1 ? ? ? ? return 0 ? ? elif control_list[r][c] ==0 and show_list[r][c]==0: ? ? ? ? button_list[r][c].place_forget() ? ? ? ? show_list[r][c]=1 ? ? ? ? t+=1 ? ? ? ? if r>0 and c>0: ? ? ? ? ? ? rec(r-1,c-1) ? ? ? ? if r>0: ? ? ? ? ? ? rec(r-1,c) ? ? ? ? if r>0 and c<15: ? ? ? ? ? ? rec(r-1,c+1) ? ? ? ? if c<15: ? ? ? ? ? ? rec(r,c+1) ? ? ? ? if r<15 and c<15: ? ? ? ? ? ? rec(r+1,c+1) ? ? ? ? if r<15: ? ? ? ? ? ? rec(r+1,c) ? ? ? ? if r<15 and c>0: ? ? ? ? ? ? rec(r+1,c-1) ? ? ? ? if c>0: ? ? ? ? ? ? rec(r,c-1) def time_counter(la): ?# la是標(biāo)簽,計時函數(shù) ? ? def counting(): ? ? ? ? global counter ? ? ? ? if T: ? ? ? ? ? ? counter += 1 ? ? ? ? la["text"]=str(counter) ? ? ? ? la.after(1000,counting) ?# 在1000毫秒后執(zhí)行counting()函數(shù),即循環(huán)執(zhí)行counting ? ? counting() def restart():#重新開始函數(shù) ? ? button_restart["text"]="?" ? ? cout_label["text"]="40" ? ? #數(shù)據(jù)重置 ? ? for i in range(16): ? ? ? ? for j in range(16): ? ? ? ? ? ? control_list[i][j]=0 ? ? ? ? ? ? show_list[i][j]=0 ? ? ? ? ? ? button_list[i][j].place_forget() ? ? ? ? ? ? button_list[i][j]=0 ? ? ? ? ? ? label_list[i][j].place_forget() ? ? ? ? ? ? label_list[i][j]=0 ? ? ? ? ? ? if (mark_list[i][j]!=0): ? ? ? ? ? ? ? ? mark_list[i][j].place_forget() ? ? ? ? ? ? mark_list[i][j]=0 ? ? global num_mine ? ? global counter ? ? global T ,t ? ? num_mine=40 ? ? counter=0 ? ? T ,t= 1,0 ? ? game_core() if __name__ =="__main__": ? ?? ? ? root = Tk()#根窗體 ? ? root.title("掃雷小游戲") ? ? root.geometry("360x410")#根窗體大小 ? ? cv1 = Canvas(root,bd=15,bg="#FFFFFF",relief=RIDGE,cursor="cross",width=321,height=350) ? ? cv1.create_line(15,45,337,45) ? ? cv1.place(x=0,y=0) ? ? w=Label(root,text="你所作的選擇,決定你的命運(yùn)!",font=("楷體",12)) ? ? w.place(x=60,y=385) ? ? button_restart=Button(root,text="?",font=('楷體', 15),bg="#AAAAAA",fg="yellow",command=restart) ? ? button_restart.place(x=150,y=17,height=27,width=27) ? ? time_label = Label(root,bg="black",fg="red",text=str(counter),font=("LcdD",15))#計時標(biāo)簽 ? ? time_label.place(x=285,y=17,height=27,width=50) ? ? cout_label = Label(root,bg="black",fg="red",text="40",font=("LcdD",20))#計數(shù)標(biāo)簽 ? ? cout_label.place(x=18,y=17,height=27,width=27) ? ? game_core() ? ? time_counter(time_label) ? ? root.mainloop()#監(jiān)控組件,組件發(fā)生變化或觸發(fā)事件時,更新窗口
總結(jié)
掃雷的思路簡單,但編寫這個程序仍然花費(fèi)了我比較長的時間,究其原因:初次接觸python的GUI編程,對各個控件的方法等不熟悉,且沒有能夠充分的查找資料,在面對問題時處理思路局限。編程世界,道路漫長遙遠(yuǎn)。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
淺談keras2 predict和fit_generator的坑
這篇文章主要介紹了淺談keras2 predict和fit_generator的坑,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06Python可視化工具如何實現(xiàn)動態(tài)圖表
這篇文章主要介紹了Python可視化工具如何實現(xiàn)動態(tài)圖表,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-10-10圖解Python中淺拷貝copy()和深拷貝deepcopy()的區(qū)別
這篇文章主要介紹了Python中淺拷貝copy()和深拷貝deepcopy()的區(qū)別,淺拷貝和深拷貝想必大家在學(xué)習(xí)中遇到很多次,這也是面試中常常被問到的問題,本文就帶你詳細(xì)了解一下2023-05-05Python輪播圖與導(dǎo)航欄功能的實現(xiàn)流程全講解
這篇文章主要介紹了Python項目輪播圖功能實現(xiàn)和導(dǎo)航欄的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-09-09MySQLdb ImportError: libmysqlclient.so.18解決方法
這篇文章主要介紹了MySQLdb ImportError: libmysqlclient.so.18解決方法,需要的朋友可以參考下2014-08-08python django框架中使用FastDFS分布式文件系統(tǒng)的安裝方法
這篇文章主要介紹了python-django框架中使用FastDFS分布式文件系統(tǒng)的安裝方法,本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價值 ,需要的朋友可以參考下2019-06-06