亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

python?GUI編程實(shí)現(xiàn)掃雷游戲

 更新時(shí)間:2022年06月08日 10:57:32   作者:ZJF010101  
這篇文章主要為大家詳細(xì)介紹了python?GUI編程實(shí)現(xiàn)掃雷游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

前言

1992年掃雷被加入到windows3.1,成為早期windows的經(jīng)典游戲。近來(lái)接觸python的GUI(圖形化)編程,于是通過(guò)編寫(xiě)掃雷來(lái)實(shí)踐學(xué)習(xí)。有關(guān)程序的問(wèn)題和想法歡迎大家指出。

一、基本思路

(1)程序的核心數(shù)據(jù)是二維列表control_list[16][16],值-1代表雷,0和其他數(shù)字代表四周雷的數(shù)目。函數(shù)randomization()隨機(jī)40個(gè)雷的 位置
(2)生成16x16個(gè)按鈕控件,根據(jù)control_list列表確定點(diǎn)擊相應(yīng)的按鈕時(shí)執(zhí)行的代碼。如按照游戲規(guī)則:點(diǎn)擊對(duì)應(yīng)control_list列表值為0的按鈕時(shí),遞歸執(zhí)行函數(shù)re()來(lái)掃雷,當(dāng)點(diǎn)擊對(duì)應(yīng)的control_list列表值為-1時(shí)直接結(jié)束游戲,對(duì)應(yīng)大于0的值則只需隱藏當(dāng)前的按鈕控件。
其他:本程序用到的GUI編程庫(kù)為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

#定義五個(gè)二維數(shù)組,充當(dāng)整個(gè)程序的數(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#計(jì)時(shí)
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ù).(開(kāi)始函數(shù)直接用參數(shù)r和c,但是會(huì)產(chǎn)生問(wè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):#遞歸探測(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)簽,計(jì)時(shí)函數(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():#重新開(kāi)始函數(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))#計(jì)時(shí)標(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))#計(jì)數(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ā)事件時(shí),更新窗口

總結(jié)

掃雷的思路簡(jiǎn)單,但編寫(xiě)這個(gè)程序仍然花費(fèi)了我比較長(zhǎng)的時(shí)間,究其原因:初次接觸python的GUI編程,對(duì)各個(gè)控件的方法等不熟悉,且沒(méi)有能夠充分的查找資料,在面對(duì)問(wèn)題時(shí)處理思路局限。編程世界,道路漫長(zhǎng)遙遠(yuǎn)。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 基于Python 優(yōu)化 MUI標(biāo)題欄

    基于Python 優(yōu)化 MUI標(biāo)題欄

    這篇文章主要介紹的是基于Python 優(yōu)化 MUI標(biāo)題欄,一個(gè)特色鮮明MUI界面無(wú)疑是能夠吸引用戶的關(guān)鍵之一,這利用css和JavaScript可以很快進(jìn)行實(shí)現(xiàn),但是同時(shí)對(duì)于初學(xué)者來(lái)說(shuō)也是困難的,下面文章就來(lái)學(xué)習(xí)幾個(gè)簡(jiǎn)單的小技巧實(shí)現(xiàn)頁(yè)面美化吧,需要的朋友可以參考一下
    2021-11-11
  • 淺談keras2 predict和fit_generator的坑

    淺談keras2 predict和fit_generator的坑

    這篇文章主要介紹了淺談keras2 predict和fit_generator的坑,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-06-06
  • 對(duì)python pandas 畫(huà)移動(dòng)平均線的方法詳解

    對(duì)python pandas 畫(huà)移動(dòng)平均線的方法詳解

    今天小編就為大家分享一篇對(duì)python pandas 畫(huà)移動(dòng)平均線的方法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-11-11
  • Python可視化工具如何實(shí)現(xiàn)動(dòng)態(tài)圖表

    Python可視化工具如何實(shí)現(xiàn)動(dòng)態(tài)圖表

    這篇文章主要介紹了Python可視化工具如何實(shí)現(xiàn)動(dòng)態(tài)圖表,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-10-10
  • 圖解Python中淺拷貝copy()和深拷貝deepcopy()的區(qū)別

    圖解Python中淺拷貝copy()和深拷貝deepcopy()的區(qū)別

    這篇文章主要介紹了Python中淺拷貝copy()和深拷貝deepcopy()的區(qū)別,淺拷貝和深拷貝想必大家在學(xué)習(xí)中遇到很多次,這也是面試中常常被問(wèn)到的問(wèn)題,本文就帶你詳細(xì)了解一下
    2023-05-05
  • Python輪播圖與導(dǎo)航欄功能的實(shí)現(xiàn)流程全講解

    Python輪播圖與導(dǎo)航欄功能的實(shí)現(xiàn)流程全講解

    這篇文章主要介紹了Python項(xiàng)目輪播圖功能實(shí)現(xiàn)和導(dǎo)航欄的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧
    2022-09-09
  • python實(shí)現(xiàn)圖片插入文字

    python實(shí)現(xiàn)圖片插入文字

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)圖片插入文字,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • Pandas中的空字符串(非缺失值)處理方式

    Pandas中的空字符串(非缺失值)處理方式

    這篇文章主要介紹了Pandas中的空字符串(非缺失值)處理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • MySQLdb ImportError: libmysqlclient.so.18解決方法

    MySQLdb ImportError: libmysqlclient.so.18解決方法

    這篇文章主要介紹了MySQLdb ImportError: libmysqlclient.so.18解決方法,需要的朋友可以參考下
    2014-08-08
  • python django框架中使用FastDFS分布式文件系統(tǒng)的安裝方法

    python django框架中使用FastDFS分布式文件系統(tǒng)的安裝方法

    這篇文章主要介紹了python-django框架中使用FastDFS分布式文件系統(tǒng)的安裝方法,本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下
    2019-06-06

最新評(píng)論