python實(shí)現(xiàn)掃雷游戲
本文為大家分享了python實(shí)現(xiàn)掃雷游戲的具體代碼,供大家參考,具體內(nèi)容如下
本文實(shí)例借鑒mvc模式,核心數(shù)據(jù)為model,維護(hù)1個(gè)矩陣,0表無(wú)雷,1表雷,-1表已經(jīng)檢測(cè)過(guò)。
本例使用python的tkinter做gui,由于沒(méi)考慮可用性問(wèn)題,因此UI比較難看,pygame更有趣更強(qiáng)大更好看,做這些小游戲更合適,感興趣的讀者可以嘗試一下!
具體的功能代碼如下:
# -*- 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()
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
在Pycharm terminal中字體大小設(shè)置的方法
今天小編就為大家分享一篇在Pycharm terminal中字體大小設(shè)置的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01
對(duì)Python進(jìn)行數(shù)據(jù)分析_關(guān)于Package的安裝問(wèn)題
下面小編就為大家?guī)?lái)一篇對(duì)Python進(jìn)行數(shù)據(jù)分析_關(guān)于Package的安裝問(wèn)題。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05
PyCM多類(lèi)別混淆矩陣分析python庫(kù)功能使用探究
這篇文章主要為大家介紹了python編寫(xiě)的PyCM多類(lèi)混淆矩陣庫(kù),用于多類(lèi)別混淆矩陣分析,幫助用戶(hù)從不同角度評(píng)價(jià)分類(lèi)模型的表現(xiàn),這些指標(biāo)包括但不限于準(zhǔn)確率、召回率、F1分?jǐn)?shù)、Kappa?統(tǒng)計(jì)量等,支持二分類(lèi)、多分類(lèi)及多標(biāo)簽分類(lèi)問(wèn)題2024-01-01
Python實(shí)現(xiàn)手機(jī)號(hào)自動(dòng)判斷男女性別(實(shí)例解析)
這篇文章主要介紹了Python實(shí)現(xiàn)手機(jī)號(hào)自動(dòng)判斷男女性別,本文性別判斷主要依靠airtest中的自動(dòng)化測(cè)試實(shí)現(xiàn),通過(guò)實(shí)例代碼給大家講解的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12
解決Django Static內(nèi)容不能加載顯示的問(wèn)題
今天小編就為大家分享一篇解決Django Static內(nèi)容不能加載顯示的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-07-07
ndarray的轉(zhuǎn)置(numpy.transpose()與A.T命令對(duì)比分析)
這篇文章主要介紹了ndarray的轉(zhuǎn)置(numpy.transpose()與A.T命令對(duì)比分析),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02
python3 requests中使用ip代理池隨機(jī)生成ip的實(shí)例
今天小編就為大家分享一篇python3 requests中使用ip代理池隨機(jī)生成ip的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-05-05

