Python實(shí)現(xiàn)簡(jiǎn)單掃雷游戲_第1頁(yè)
Python實(shí)現(xiàn)簡(jiǎn)單掃雷游戲_第2頁(yè)
Python實(shí)現(xiàn)簡(jiǎn)單掃雷游戲_第3頁(yè)
Python實(shí)現(xiàn)簡(jiǎn)單掃雷游戲_第4頁(yè)
Python實(shí)現(xiàn)簡(jiǎn)單掃雷游戲_第5頁(yè)
已閱讀5頁(yè),還剩7頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

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

#coding:utf-8

__note__="""

*掃雷小游戲

*需要python3.x以上

*需要安裝PyQt5

*pipinstallPyQt5

importsys

importPyQt5

exceptImportError:

importtkinter

fromtkinterimportmessagebox

err_str="請(qǐng)安裝PyQt5后再打開(kāi):pipinstallPyQt5"

messagebox.showerror("模塊錯(cuò)誤!",err_str)

raiseImportError(err_str)

sys.exit()

fromrandomimportrandint

fromPyQt5.QtWidgetsimport\

QApplication,

\

QWidget,

\

QPushButton,

\

QLCDNumber,

\

QDesktopWidget,

\

QMessageBox

fromPyQt5.QtCoreimportQt

classMine(object):

mine=9

no_mine=0

n_mine=10

width=10

height=10

def__init__(self,width=10,height=10,nMines=10):

self.map=[]

for_inrange(height):

t_line=[]

for_inrange(width):

t_line.append(self.no_mine)

self.map.append(t_line)

self.width=width

self.height=height

self.n_mine=nMines

self.remix()

#打亂布局重新隨機(jī)編排

defremix(self):

foryinrange(self.height):

forxinrange(self.width):

self.map[y][x]=self.no_mine

defadd_mark(x,y):

#如果不是雷的標(biāo)記就+1

ifself.map[y][x]+1self.mine:

self.map[y][x]+=1

mine_count=0

whilemine_countself.n_mine:

x=randint(0,self.width-1)

y=randint(0,self.height-1)

ifself.map[y][x]!=self.mine:

self.map[y][x]=self.mine

mine_count+=1

#雷所在的位置的8個(gè)方位的數(shù)值+1

##上下左右

ify-1=0:add_mark(x,y-1)

ify+1self.height:add_mark(x,y+1)

ifx-1=0:add_mark(x-1,y)

ifx+1self.width:add_mark(x+1,y)

##四個(gè)角:左上角、左下角、右上角、右下角

ifx-1=0andy-1=1:add_mark(x-1,y-1)

ifx-1=0andy+1self.height:add_mark(x-1,y+1)

ifx+1self.widthandy-1=1:add_mark(x+1,y-1)

ifx+1self.widthandy+1self.height:add_mark(x+1,y+1)

def__getitem__(self,key):

returnself.map[key]

def__str__(self):

format_str=""

foryinrange(self.height):

format_str+=str(self[y])+"\n"

returnformat_str

__repr__=__str__

classLCDCounter(QLCDNumber):

__counter=0

def__init__(self,start=0,parent=None):

super().__init__(4,parent)

self.setSegmentStyle(QLCDNumber.Flat)

self.setStyleSheet("background:black;color:red")

self.counter=start

@property

defcounter(self):

returnself.__counter

@counter.setter

defcounter(self,value):

self.__counter=value

self.display(str(self.__counter))

definc(self):

self.counter+=1

defdec(self):

self.counter-=1

classMineButton(QPushButton):

#按鈕類型

MINE=Mine.mine

#雷

NOTMINE=Mine.no_mine

#不是雷

m_type=None

#按鈕狀態(tài)

mark=False

#是否是標(biāo)記狀態(tài)(默認(rèn):未被標(biāo)記)

s_flag='#9873;'

#標(biāo)記

s_mine='#9760;'

#雷

s_success='#128076;'

#按鈕是否按下(默認(rèn)False:未按下)

__pushed=False

#按鈕對(duì)應(yīng)map的位置

m_x=0

m_y=0

def__init__(self,map_pos,m_type,parent):

super().__init__(parent)

self.m_type=m_type

self.pushed=False

self.m_x=map_pos[0]

self.m_y=map_pos[1]

@property

defpushed(self):

returnnotself.__pushed

@pushed.setter

defpushed(self,value):

self.__pushed=notvalue

self.setEnabled(self.__pushed)

##按鈕上的鼠標(biāo)按下事件

defmousePressEvent(self,e):

#print("m_x:%d"%self.m_x,"m_y:%d"%self.m_y,"m_type:%d"%self.m_type)

p=self.parent()

#記錄鼠標(biāo)單擊次數(shù)

p.nwap_lcd_clicked.counter+=1

#左鍵掃雷

ife.buttons()==Qt.LeftButton:

#踩中雷,全部雷都翻起來(lái)

ifself.m_type==self.MINE:

fort_line_btninp.btn_map:

forbtnint_line_btn:

ifbtn.m_type==btn.MINE:

btn.setText(btn.s_mine)

else:

ifbtn.mark!=True:

ifbtn.m_type!=btn.NOTMINE:

btn.setText(str(btn.m_type))

btn.pushed=True

#苦逼臉

p.RestartBtn.setText('#128547;')

QMessageBox.critical(self,"失敗!","您不小心踩到了雷!"+self.s_mine)

returnNone

elifself.m_type==self.NOTMINE:

self.AutoSwap(self.m_x,self.m_y)

else:

self.setText(str(self.m_type))

p.mine_counter-=1

self.pushed=True

#右鍵添加標(biāo)記

elife.buttons()==Qt.RightButton:

ifself.mark==False:

self.setText(self.s_flag)

self.mark=True

else:

self.setText("")

self.mark=False

self.setFocus(False)

##當(dāng)按下的位置是NOTMINE時(shí)自動(dòng)掃雷

defAutoSwap(self,x,y):

p=self.parent()

map_btn=p.btn_map

deflookup(t_line,index):

#向左掃描

i=index

whilei=0andnott_line[i].pushedandt_line[i].m_type!=MineButton.MINE:

ift_line[i].m_type!=MineButton.NOTMINE:

t_line[i].setText(str(t_line[i].m_type))

t_line[i].pushed=True

p.mine_counter-=1

p.nwap_lcd_counter.counter=p.mine_counter

i-=1

ift_line[i].m_type!=MineButton.NOTMINE:

break

#向右掃描

i=index+1

whileip.mine_map.widthandnott_line[i].pushedandt_line[i].m_type!=MineButton.MINE:

ift_line[i].m_type!=MineButton.NOTMINE:

t_line[i].setText(str(t_line[i].m_type))

t_line[i].pushed=True

p.mine_counter-=1

p.nwap_lcd_counter.counter=p.mine_counter

i+=1

ift_line[i].m_type!=MineButton.NOTMINE:

break

#向上掃描

j=y

whilej=0:

lookup(map_btn[j],x)

j-=1

#向下掃描

j=y+1

whilejp.mine_map.height:

lookup(map_btn[j],x)

j+=1

classMineWindow(QWidget):

def__init__(self):

super().__init__()

self.mine_map=Mine(nMines=16)

self.InitGUI()

#print(self.mine_map)

defInitGUI(self):

w_width=304

w_height=344

self.resize(w_width,w_height)

self.setFixedSize(self.width(),self.height())

self.setWindowTitle("掃雷")

##窗口居中于屏幕

qr=self.frameGeometry()

cp=QDesktopWidget().availableGeometry().center()

qr.moveCenter(cp)

self.move(qr.x(),qr.y())

l_start_x=2

l_start_y=40

l_x=l_start_x

l_y=l_start_y

l_width=30

l_height=30

#雷區(qū)按鈕

self.btn_map=[]

forhinrange(self.mine_map.height):

l_x=l_start_x

self.btn_map.append(list())

forwinrange(self.mine_map.width):

self.btn_map[h].append(MineButton([w,h],self.mine_map[h][w],self))

self.btn_map[h][w].resize(l_width,l_height)

self.btn_map[h][w].move(l_x,l_y)

self.btn_map[h][w].show()

l_x+=l_width

l_y+=l_height

r_width=30

r_height=30

#恢復(fù)按鈕

self.RestartBtn=QPushButton('#128522;',self)

self.RestartBtn.clicked.connect(self.restart_btn_event)

self.RestartBtn.resize(r_width,r_height)

self.RestartBtn.move((w_width-r_width)//2,6)

##計(jì)數(shù)器

self.__mine_counter=self.mine_map.width*self.mine_map.height-self.mine_map.n_mine

##兩個(gè)LCD顯示控件

#操作次數(shù)

self.nwap_lcd_clicked=LCDCounter(0,self)

self.nwap_lcd_clicked.move(44,8)

#無(wú)雷塊個(gè)數(shù)

self.nwap_lcd_counter=LCDCounter(self.mine_counter,self)

self.nwap_lcd_counter.move(204,8)

defrestart_btn_event(self):

self.mine_map.remix()

#QMessageBrmation(self,"lookup",str(self.mine_map))

foryinrange(len(self.btn_map)):

forxinrange(len(self.btn_map[y])):

self.btn_map[y][x].pushed=False

self.btn_map[y][x].setText("")

self.btn_map[y][x].m_type=se

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論