欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于Python實(shí)現(xiàn)的掃雷游戲?qū)嵗a

 更新時(shí)間:2014年08月01日 10:49:37   投稿:shichen2014  
這篇文章主要介紹了基于Python實(shí)現(xiàn)的掃雷游戲?qū)嵗a,對(duì)于Python的學(xué)習(xí)以及Python游戲開發(fā)都有一定的借鑒價(jià)值,需要的朋友可以參考下

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

具體的功能代碼如下:

# -*- coding: utf-8 -*-
import random
import sys
from Tkinter import *

class Model:
  """
  核心數(shù)據(jù)類,維護(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è)過
      return True
    else:
      return False
    
  def countValue(self,r,c,value):
    """
    統(tǒng)計(jì)某個(gè)位置周圍8個(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:是雷,所有都顯示出來,游戲結(jié)束
    case 2:是周圍雷數(shù)為0的,遞歸觸發(fā)周圍8個(gè)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個(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):
  """
  重新開始游戲
  """
  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)文章

  • matplotlib中l(wèi)egend位置調(diào)整解析

    matplotlib中l(wèi)egend位置調(diào)整解析

    這篇文章主要介紹了matplotlib中l(wèi)egend位置調(diào)整解析,具有一定借鑒價(jià)值,需要的朋友可以參考下。
    2017-12-12
  • Numpy中np.random.rand()和np.random.randn() 用法和區(qū)別詳解

    Numpy中np.random.rand()和np.random.randn() 用法和區(qū)別詳解

    這篇文章主要介紹了Numpy中np.random.rand()和np.random.randn() 用法和區(qū)別詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • Python從文件中讀取數(shù)據(jù)的方法步驟

    Python從文件中讀取數(shù)據(jù)的方法步驟

    這篇文章主要介紹了Python從文件中讀取數(shù)據(jù)的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • Python3爬蟲中Selenium的用法詳解

    Python3爬蟲中Selenium的用法詳解

    在本篇內(nèi)容里小編給大家分享了關(guān)于Python3爬蟲中Selenium的用法詳解內(nèi)容,需要的朋友們可以參考下。
    2020-07-07
  • python調(diào)用HEG工具批量處理MODIS數(shù)據(jù)的方法及注意事項(xiàng)

    python調(diào)用HEG工具批量處理MODIS數(shù)據(jù)的方法及注意事項(xiàng)

    這篇文章主要介紹了python調(diào)用HEG工具批量處理MODIS數(shù)據(jù)的方法,本文給大家提到了注意事項(xiàng),通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-02-02
  • python中的數(shù)據(jù)結(jié)構(gòu)比較

    python中的數(shù)據(jù)結(jié)構(gòu)比較

    這篇文章主要介紹了python中的數(shù)據(jù)結(jié)構(gòu)比較,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-05-05
  • Django零基礎(chǔ)入門之靜態(tài)文件的引用

    Django零基礎(chǔ)入門之靜態(tài)文件的引用

    這篇文章主要介紹了Django零基礎(chǔ)入門之靜態(tài)文件的引用,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-09-09
  • python 實(shí)現(xiàn)方陣的對(duì)角線遍歷示例

    python 實(shí)現(xiàn)方陣的對(duì)角線遍歷示例

    今天小編就為大家分享一篇python 實(shí)現(xiàn)方陣的對(duì)角線遍歷示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • python代碼實(shí)現(xiàn)將列表中重復(fù)元素之間的內(nèi)容全部濾除

    python代碼實(shí)現(xiàn)將列表中重復(fù)元素之間的內(nèi)容全部濾除

    這篇文章主要介紹了python代碼實(shí)現(xiàn)將列表中重復(fù)元素之間的內(nèi)容全部濾除,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-05-05
  • Python?Pandas中缺失值NaN的判斷,刪除及替換

    Python?Pandas中缺失值NaN的判斷,刪除及替換

    缺失值是指數(shù)據(jù)集中的某些觀察存在遺漏的指標(biāo)值,缺失值的存在同樣會(huì)影響到數(shù)據(jù)剖析和挖掘的效果,下面這篇文章主要給大家介紹了關(guān)于Python?Pandas中缺失值NaN的判斷,刪除及替換的相關(guān)資料,需要的朋友可以參考下
    2022-01-01

最新評(píng)論