溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

使用python實(shí)現(xiàn)掃雷游戲的案例

發(fā)布時(shí)間:2021-03-23 11:17:37 來(lái)源:億速云 閱讀:164 作者:小新 欄目:開(kāi)發(fā)技術(shù)

這篇文章給大家分享的是有關(guān)使用python實(shí)現(xiàn)掃雷游戲的案例的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。

具體的功能代碼如下:

# -*- coding: utf-8 -*-
import random
import sys
from Tkinter import *
'''
想要學(xué)習(xí)Python?

'''
class Model:
 """
 核心數(shù)據(jù)類(lèi),維護(hù)一個(gè)矩陣
 """
 def __init__(self,row,col):
 self.width=col
 self.height=row
 self.items=[[0 for c in range(col)] for r in range(row)]
 
 def setItemValue(self,r,c,value):
 """
 設(shè)置某個(gè)位置的值為value
 """
 self.items[r][c]=value;
 
 def checkValue(self,r,c,value):
 """
 檢測(cè)某個(gè)位置的值是否為value
 """
 if self.items[r][c]!=-1 and self.items[r][c]==value:
 self.items[r][c]=-1 #已經(jīng)檢測(cè)過(guò)
 return True
 else:
 return False
 
 def countValue(self,r,c,value):
 """
 統(tǒng)計(jì)某個(gè)位置周?chē)?個(gè)位置中,值為value的個(gè)數(shù)
 """
 count=0
 if r-1>=0 and c-1>=0:
 if self.items[r-1][c-1]==1:count+=1
 if r-1>=0 and c>=0:
 if self.items[r-1][c]==1:count+=1
 if r-1>=0 and c+1<=self.width-1:
 if self.items[r-1][c+1]==1:count+=1
 if c-1>=0:
 if self.items[r][c-1]==1:count+=1
 if c+1<=self.width-1 :
 if self.items[r][c+1]==1:count+=1
 if r+1<=self.height-1 and c-1>=0:
 if self.items[r+1][c-1]==1:count+=1
 if r+1<=self.height-1 :
 if self.items[r+1][c]==1:count+=1
 if r+1<=self.height-1 and c+1<=self.width-1:
 if self.items[r+1][c+1]==1:count+=1
 return count
 
 
class Mines(Frame):
 def __init__(self,m,master=None):
 Frame.__init__(self,master)
 self.model=m
 self.initmine()
 self.grid()
 self.createWidgets()
 
 
 
 def createWidgets(self):
 #top=self.winfo_toplevel()
 #top.rowconfigure(self.model.height*2,weight=1)
 #top.columnconfigure(self.model.width*2,weight=1)
 self.rowconfigure(self.model.height,weight=1)
 self.columnconfigure(self.model.width,weight=1)
 self.buttongroups=[[Button(self,height=1,width=2) for i in range(self.model.width)]
 for j in range(self.model.height)]
 for r in range(self.model.width):
 for c in range(self.model.height):
 self.buttongroups[r][c].grid(row=r,column=c)
 self.buttongroups[r][c].bind('<Button-1>',self.clickevent)
 self.buttongroups[r][c]['padx']=r
 self.buttongroups[r][c]['pady']=c
 
 def showall(self):
 for r in range(model.height):
 for c in range(model.width):
 self.showone(r,c)
 
 def showone(self,r,c):
 if model.checkValue(r,c,0):
 self.buttongroups[r][c]['text']=model.countValue(r,c,1)
 else:
 self.buttongroups[r][c]['text']='Mines'
 
 def recureshow(self,r,c):
 if 0<=r<=self.model.height-1 and 0<=c<=self.model.width-1:
 if model.checkValue(r,c,0) and model.countValue(r,c,1)==0:
 self.buttongroups[r][c]['text']=''
 self.recureshow(r-1,c-1)
 self.recureshow(r-1,c)
 self.recureshow(r-1,c+1)
 self.recureshow(r,c-1)
 self.recureshow(r,c+1)
 self.recureshow(r+1,c-1)
 self.recureshow(r+1,c)
 self.recureshow(r+1,c+1)
 elif model.countValue(r,c,1)!=0:
 self.buttongroups[r][c]['text']=model.countValue(r,c,1)
 else:
 pass
 
 
 def clickevent(self,event):
 """
 點(diǎn)擊事件
 case 1:是雷,所有都顯示出來(lái),游戲結(jié)束
 case 2:是周?chē)讛?shù)為0的,遞歸觸發(fā)周?chē)?個(gè)button的點(diǎn)擊事件
 case 3:周?chē)讛?shù)不為0的,顯示周?chē)讛?shù)
 """
 r=int(str(event.widget['padx']))
 c=int(str(event.widget['pady']))
 if model.checkValue(r,c,1):#是雷
 self.showall()
 else:#不是雷
 self.recureshow(r,c)
 
 
 def initmine(self):
 """
 埋雷,每行埋height/width+2個(gè)暫定
 """
 r=random.randint(1,model.height/model.width+2)
 for r in range(model.height):
 for i in range(2):
 rancol=random.randint(0,model.width-1)
 model.setItemValue(r,rancol,1)
 
 
 def printf(self):
 """
 打印
 """
 for r in range(model.height):
 for c in range(model.width):
 print model.items[r][c],
 print '/n'
 
 
def new(self):
 """
 重新開(kāi)始游戲
 """
 pass
 
if __name__=='__main__':
 model=Model(10,10)
 root=Tk()
 
 #menu
 menu = Menu(root)
 root.config(menu=menu)
 filemenu = Menu(menu)
 menu.add_cascade(label="File", menu=filemenu)
 filemenu.add_command(label="New",command=new)
 filemenu.add_separator()
 filemenu.add_command(label="Exit", command=root.quit)
 
 #Mines
 m=Mines(model,root)
 #m.printf()
 root.mainloop()

感謝各位的閱讀!關(guān)于“使用python實(shí)現(xiàn)掃雷游戲的案例”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI