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

python實(shí)現(xiàn)掃雷游戲

 更新時間:2020年03月03日 13:24:34   作者:嗨學(xué)編程  
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)掃雷游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文為大家分享了python實(shí)現(xiàn)掃雷游戲的具體代碼,供大家參考,具體內(nèi)容如下

本文實(shí)例借鑒mvc模式,核心數(shù)據(jù)為model,維護(hù)1個矩陣,0表無雷,1表雷,-1表已經(jīng)檢測過。
本例使用python的tkinter做gui,由于沒考慮可用性問題,因此UI比較難看,pygame更有趣更強(qiáng)大更好看,做這些小游戲更合適,感興趣的讀者可以嘗試一下!

具體的功能代碼如下:

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

'''
class Model:
 """
 核心數(shù)據(jù)類,維護(hù)一個矩陣
 """
 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è)置某個位置的值為value
 """
 self.items[r][c]=value;
 
 def checkValue(self,r,c,value):
 """
 檢測某個位置的值是否為value
 """
 if self.items[r][c]!=-1 and self.items[r][c]==value:
 self.items[r][c]=-1 #已經(jīng)檢測過
 return True
 else:
 return False
 
 def countValue(self,r,c,value):
 """
 統(tǒng)計某個位置周圍8個位置中,值為value的個數(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:是雷,所有都顯示出來,游戲結(jié)束
 case 2:是周圍雷數(shù)為0的,遞歸觸發(fā)周圍8個button的點(diǎn)擊事件
 case 3:周圍雷數(shù)不為0的,顯示周圍雷數(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個暫定
 """
 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):
 """
 重新開始游戲
 """
 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()

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

相關(guān)文章

  • Python容器使用的5個技巧和2個誤區(qū)總結(jié)

    Python容器使用的5個技巧和2個誤區(qū)總結(jié)

    在本篇文章里小編給大家整理的是關(guān)于Python容器使用的5個技巧和2個誤區(qū)的相關(guān)知識點(diǎn)內(nèi)容,需要的朋友們學(xué)習(xí)下。
    2019-09-09
  • python矩陣的基本運(yùn)算及各種操作

    python矩陣的基本運(yùn)算及各種操作

    python的numpy庫提供矩陣運(yùn)算的功能,因此我們在需要矩陣運(yùn)算的時候,需要導(dǎo)入numpy的包,下面這篇文章主要給大家介紹了關(guān)于python矩陣的基本運(yùn)算及各種操作的相關(guān)資料,需要的朋友可以參考下
    2022-11-11
  • python 30行代碼實(shí)現(xiàn)螞蟻森林自動偷能量

    python 30行代碼實(shí)現(xiàn)螞蟻森林自動偷能量

    這篇文章主要介紹了python 30行代碼實(shí)現(xiàn)螞蟻森林自動偷能量的方法,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下
    2021-02-02
  • 利用Python實(shí)現(xiàn)自動生成圖文并茂的數(shù)據(jù)分析

    利用Python實(shí)現(xiàn)自動生成圖文并茂的數(shù)據(jù)分析

    這篇文章主要介紹了利用Python實(shí)現(xiàn)自動生成圖文并茂的數(shù)據(jù)分析,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的朋友可以參考一下
    2022-08-08
  • Python中Django與Echarts的結(jié)合用法圖文詳解

    Python中Django與Echarts的結(jié)合用法圖文詳解

    ECharts是一個第三方控件,下面這篇文章主要給大家介紹了關(guān)于Python中Django與Echarts的結(jié)合用法,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2022-10-10
  • python多線程編程方式分析示例詳解

    python多線程編程方式分析示例詳解

    本文介紹一下有關(guān)Python多線程的相關(guān)應(yīng)用技巧,線程相對進(jìn)程來說是"輕量級"的,操作系統(tǒng)用較少的資源創(chuàng)建和管理線程。程序中的線程在相同的內(nèi)存空間中執(zhí)行,并共享許多相同的資源,下面看使用方法
    2013-12-12
  • python imread函數(shù)詳解

    python imread函數(shù)詳解

    這篇文章主要介紹了python imread函數(shù)詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • python flask幾分鐘實(shí)現(xiàn)web服務(wù)的例子

    python flask幾分鐘實(shí)現(xiàn)web服務(wù)的例子

    今天小編就為大家分享一篇python flask幾分鐘實(shí)現(xiàn)web服務(wù)的例子,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • python django 訪問靜態(tài)文件出現(xiàn)404或500錯誤

    python django 訪問靜態(tài)文件出現(xiàn)404或500錯誤

    這篇文章主要介紹了python django 訪問靜態(tài)文件出現(xiàn)404或500錯誤的相關(guān)資料,需要的朋友可以參考下
    2017-01-01
  • Python利用matplotlib.pyplot.boxplot()繪制箱型圖實(shí)例代碼

    Python利用matplotlib.pyplot.boxplot()繪制箱型圖實(shí)例代碼

    相信大家應(yīng)該都知道Python繪制箱線圖主要用matplotlib庫里pyplot模塊里的boxplot()函數(shù),下面這篇文章主要給大家介紹了關(guān)于Python利用matplotlib.pyplot.boxplot()繪制箱型圖的相關(guān)資料,需要的朋友可以參考下
    2022-08-08

最新評論