
作者:野客
來(lái)源:Python 技術(shù)
掃雷是一款益智類(lèi)小游戲,最早于 1992 年由微軟在 Windows 上發(fā)行,游戲適合于全年齡段,規(guī)則簡(jiǎn)單,即在最短的時(shí)間內(nèi)找出所有非雷格子且在中間過(guò)程中不能踩到雷, 踩到雷則失敗,需重新開(kāi)始。
本文我們使用 Python 來(lái)實(shí)現(xiàn)掃雷游戲,主要用的 Python 庫(kù)是 pygame。
游戲組成比較簡(jiǎn)單,主要包括:小方格、計(jì)時(shí)器、地雷等。
首先,我們初始化一些常量,比如:橫豎方塊數(shù)、地雷數(shù)、鼠標(biāo)點(diǎn)擊情況等,如下所示:
BLOCK_WIDTH = 30 BLOCK_HEIGHT = 16 # 塊大小 SIZE = 20 # 地雷數(shù) MINE_COUNT = 66 # 未點(diǎn)擊 normal = 1 # 已點(diǎn)擊 opened = 2 # 地雷 mine = 3 # 標(biāo)記為地雷 flag = 4 # 標(biāo)記為問(wèn)號(hào) ask = 5 # 踩中地雷 bomb = 6 # 被雙擊的周?chē)?/span> hint = 7 # 正被鼠標(biāo)左右鍵雙擊 double = 8 readied = 1, started = 2, over = 3, win = 4
接著定義一個(gè)地雷類(lèi),類(lèi)中定義一些基本屬性(如:坐標(biāo)、狀態(tài)等)及 get、set 方法,代碼實(shí)現(xiàn)如下:
class Mine: def __init__(self, x, y, value=0): self._x = x self._y = y self._value = 0 self._around_mine_count = -1 self._status = normal self.set_value(value) def __repr__(self): return str(self._value) def get_x(self): return self._x def set_x(self, x): self._x = x
x = property(fget=get_x, fset=set_x) def get_y(self): return self._y def set_y(self, y): self._y = y
y = property(fget=get_y, fset=set_y) def get_value(self): return self._value def set_value(self, value): if value: self._value = 1 else: self._value = 0 value = property(fget=get_value, fset=set_value, doc='0:非地雷 1:雷') def get_around_mine_count(self): return self._around_mine_count def set_around_mine_count(self, around_mine_count): self._around_mine_count = around_mine_count
around_mine_count = property(fget=get_around_mine_count, fset=set_around_mine_count, doc='四周地雷數(shù)量') def get_status(self): return self._status def set_status(self, value): self._status = value
status = property(fget=get_status, fset=set_status, doc='BlockStatus')
再接著定義一個(gè) MineBlock 類(lèi),用來(lái)處理掃雷的基本邏輯,代碼實(shí)現(xiàn)如下:
class MineBlock: def __init__(self): self._block = [[Mine(i, j) for i in range(BLOCK_WIDTH)] for j in range(BLOCK_HEIGHT)] # 埋雷 for i in random.sample(range(BLOCK_WIDTH * BLOCK_HEIGHT), MINE_COUNT): self._block[i // BLOCK_WIDTH][i % BLOCK_WIDTH].value = 1 def get_block(self): return self._block block = property(fget=get_block) def getmine(self, x, y): return self._block[y][x] def open_mine(self, x, y): # 踩到雷了 if self._block[y][x].value: self._block[y][x].status = bomb return False # 先把狀態(tài)改為 opened self._block[y][x].status = opened around = _get_around(x, y) _sum = 0 for i, j in around: if self._block[j][i].value: _sum += 1 self._block[y][x].around_mine_count = _sum # 如果周?chē)鷽](méi)有雷,那么將周?chē)?8 個(gè)未中未點(diǎn)開(kāi)的遞歸算一遍 if _sum == 0: for i, j in around: if self._block[j][i].around_mine_count == -1: self.open_mine(i, j) return True def double_mouse_button_down(self, x, y): if self._block[y][x].around_mine_count == 0: return True self._block[y][x].status = double around = _get_around(x, y) # 周?chē)粯?biāo)記的雷數(shù)量 sumflag = 0 for i, j in _get_around(x, y): if self._block[j][i].status == flag: sumflag += 1 # 周邊的雷已經(jīng)全部被標(biāo)記 result = True if sumflag == self._block[y][x].around_mine_count: for i, j in around: if self._block[j][i].status == normal: if not self.open_mine(i, j): result = False else: for i, j in around: if self._block[j][i].status == normal: self._block[j][i].status = hint return result def double_mouse_button_up(self, x, y): self._block[y][x].status = opened for i, j in _get_around(x, y): if self._block[j][i].status == hint: self._block[j][i].status = normal
我們接下來(lái)初始化界面,首先生成由小方格組成的面板,主要代碼實(shí)現(xiàn)如下:
for row in block.block: for mine in row: pos = (mine.x * SIZE, (mine.y + 2) * SIZE) if mine.status == opened: screen.blit(img_dict[mine.around_mine_count], pos) opened_count += 1 elif mine.status == double: screen.blit(img_dict[mine.around_mine_count], pos) elif mine.status == bomb: screen.blit(img_blood, pos) elif mine.status == flag: screen.blit(img_flag, pos) flag_count += 1 elif mine.status == ask: screen.blit(img_ask, pos) elif mine.status == hint: screen.blit(img0, pos) elif game_status == over and mine.value: screen.blit(img_mine, pos) elif mine.value == 0 and mine.status == flag: screen.blit(img_error, pos) elif mine.status == normal: screen.blit(img_blank, pos)
看一下效果:
再接著添加面板的 head 部分,包括:顯示雷數(shù)、重新開(kāi)始按鈕(笑臉)、顯示耗時(shí),主要代碼實(shí)現(xiàn)如下:
print_text(screen, font1, 30, (SIZE * 2 - fheight) // 2 - 2, '%02d' % (MINE_COUNT - flag_count), red) if game_status == started: elapsed_time = int(time.time() - start_time) print_text(screen, font1, SCREEN_WIDTH - fwidth - 30, (SIZE * 2 - fheight) // 2 - 2, '%03d' % elapsed_time, red) if flag_count + opened_count == BLOCK_WIDTH * BLOCK_HEIGHT: game_status = win if game_status == over: screen.blit(img_face_fail, (face_pos_x, face_pos_y)) elif game_status == win: screen.blit(img_face_success, (face_pos_x, face_pos_y)) else: screen.blit(img_face_normal, (face_pos_x, face_pos_y))
看一下效果:
再接著添加各種點(diǎn)擊事件,代碼實(shí)現(xiàn)如下:
for event in pygame.event.get(): if event.type == QUIT: sys.exit() elif event.type == MOUSEBUTTONDOWN: mouse_x, mouse_y = event.pos x = mouse_x // SIZE y = mouse_y // SIZE - 2 b1, b2, b3 = pygame.mouse.get_pressed() if game_status == started: # 鼠標(biāo)左右鍵同時(shí)按下,如果已經(jīng)標(biāo)記了所有雷,則打開(kāi)周?chē)蝗Γ蝗绻€未標(biāo)記完所有雷,則有一個(gè)周?chē)蝗Ρ煌瑫r(shí)按下的效果 if b1 and b3: mine = block.getmine(x, y) if mine.status == opened: if not block.double_mouse_button_down(x, y): game_status = over elif event.type == MOUSEBUTTONUP: if y < 0: if face_pos_x <= mouse_x <= face_pos_x + face_size
and face_pos_y <= mouse_y <= face_pos_y + face_size: game_status = readied block = MineBlock() start_time = time.time() elapsed_time = 0 continue if game_status == readied: game_status = started start_time = time.time() elapsed_time = 0 if game_status == started: mine = block.getmine(x, y) # 按鼠標(biāo)左鍵 if b1 and not b3: if mine.status == normal: if not block.open_mine(x, y): game_status = over # 按鼠標(biāo)右鍵 elif not b1 and b3: if mine.status == normal: mine.status = flag elif mine.status == flag: mine.status = ask elif mine.status == ask: mine.status = normal elif b1 and b3: if mine.status == double: block.double_mouse_button_up(x, y)
我們來(lái)看一下最終實(shí)現(xiàn)效果:
本文我們通過(guò) Python 簡(jiǎn)單的實(shí)現(xiàn)了掃雷游戲,大家有興趣的話,可以實(shí)際操作一下,看看自己能否排除全部的雷。
數(shù)據(jù)分析咨詢請(qǐng)掃描二維碼
若不方便掃碼,搜微信號(hào):CDAshujufenxi
LSTM 模型輸入長(zhǎng)度選擇技巧:提升序列建模效能的關(guān)鍵? 在循環(huán)神經(jīng)網(wǎng)絡(luò)(RNN)家族中,長(zhǎng)短期記憶網(wǎng)絡(luò)(LSTM)憑借其解決長(zhǎng)序列 ...
2025-07-11CDA 數(shù)據(jù)分析師報(bào)考條件詳解與準(zhǔn)備指南? ? 在數(shù)據(jù)驅(qū)動(dòng)決策的時(shí)代浪潮下,CDA 數(shù)據(jù)分析師認(rèn)證愈發(fā)受到矚目,成為眾多有志投身數(shù) ...
2025-07-11數(shù)據(jù)透視表中兩列相乘合計(jì)的實(shí)用指南? 在數(shù)據(jù)分析的日常工作中,數(shù)據(jù)透視表憑借其強(qiáng)大的數(shù)據(jù)匯總和分析功能,成為了 Excel 用戶 ...
2025-07-11尊敬的考生: 您好! 我們誠(chéng)摯通知您,CDA Level I和 Level II考試大綱將于 2025年7月25日 實(shí)施重大更新。 此次更新旨在確保認(rèn) ...
2025-07-10BI 大數(shù)據(jù)分析師:連接數(shù)據(jù)與業(yè)務(wù)的價(jià)值轉(zhuǎn)化者? ? 在大數(shù)據(jù)與商業(yè)智能(Business Intelligence,簡(jiǎn)稱(chēng) BI)深度融合的時(shí)代,BI ...
2025-07-10SQL 在預(yù)測(cè)分析中的應(yīng)用:從數(shù)據(jù)查詢到趨勢(shì)預(yù)判? ? 在數(shù)據(jù)驅(qū)動(dòng)決策的時(shí)代,預(yù)測(cè)分析作為挖掘數(shù)據(jù)潛在價(jià)值的核心手段,正被廣泛 ...
2025-07-10數(shù)據(jù)查詢結(jié)束后:分析師的收尾工作與價(jià)值深化? ? 在數(shù)據(jù)分析的全流程中,“query end”(查詢結(jié)束)并非工作的終點(diǎn),而是將數(shù) ...
2025-07-10CDA 數(shù)據(jù)分析師考試:從報(bào)考到取證的全攻略? 在數(shù)字經(jīng)濟(jì)蓬勃發(fā)展的今天,數(shù)據(jù)分析師已成為各行業(yè)爭(zhēng)搶的核心人才,而 CDA(Certi ...
2025-07-09【CDA干貨】單樣本趨勢(shì)性檢驗(yàn):捕捉數(shù)據(jù)背后的時(shí)間軌跡? 在數(shù)據(jù)分析的版圖中,單樣本趨勢(shì)性檢驗(yàn)如同一位耐心的偵探,專(zhuān)注于從單 ...
2025-07-09year_month數(shù)據(jù)類(lèi)型:時(shí)間維度的精準(zhǔn)切片? ? 在數(shù)據(jù)的世界里,時(shí)間是最不可或缺的維度之一,而year_month數(shù)據(jù)類(lèi)型就像一把精準(zhǔn) ...
2025-07-09CDA 備考干貨:Python 在數(shù)據(jù)分析中的核心應(yīng)用與實(shí)戰(zhàn)技巧? ? 在 CDA 數(shù)據(jù)分析師認(rèn)證考試中,Python 作為數(shù)據(jù)處理與分析的核心 ...
2025-07-08SPSS 中的 Mann-Kendall 檢驗(yàn):數(shù)據(jù)趨勢(shì)與突變分析的有力工具? ? ? 在數(shù)據(jù)分析的廣袤領(lǐng)域中,準(zhǔn)確捕捉數(shù)據(jù)的趨勢(shì)變化以及識(shí)別 ...
2025-07-08備戰(zhàn) CDA 數(shù)據(jù)分析師考試:需要多久?如何規(guī)劃? CDA(Certified Data Analyst)數(shù)據(jù)分析師認(rèn)證作為國(guó)內(nèi)權(quán)威的數(shù)據(jù)分析能力認(rèn)證 ...
2025-07-08LSTM 輸出不確定的成因、影響與應(yīng)對(duì)策略? 長(zhǎng)短期記憶網(wǎng)絡(luò)(LSTM)作為循環(huán)神經(jīng)網(wǎng)絡(luò)(RNN)的一種變體,憑借獨(dú)特的門(mén)控機(jī)制,在 ...
2025-07-07統(tǒng)計(jì)學(xué)方法在市場(chǎng)調(diào)研數(shù)據(jù)中的深度應(yīng)用? 市場(chǎng)調(diào)研是企業(yè)洞察市場(chǎng)動(dòng)態(tài)、了解消費(fèi)者需求的重要途徑,而統(tǒng)計(jì)學(xué)方法則是市場(chǎng)調(diào)研數(shù) ...
2025-07-07CDA數(shù)據(jù)分析師證書(shū)考試全攻略? 在數(shù)字化浪潮席卷全球的當(dāng)下,數(shù)據(jù)已成為企業(yè)決策、行業(yè)發(fā)展的核心驅(qū)動(dòng)力,數(shù)據(jù)分析師也因此成為 ...
2025-07-07剖析 CDA 數(shù)據(jù)分析師考試題型:解鎖高效備考與答題策略? CDA(Certified Data Analyst)數(shù)據(jù)分析師考試作為衡量數(shù)據(jù)專(zhuān)業(yè)能力的 ...
2025-07-04SQL Server 字符串截取轉(zhuǎn)日期:解鎖數(shù)據(jù)處理的關(guān)鍵技能? 在數(shù)據(jù)處理與分析工作中,數(shù)據(jù)格式的規(guī)范性是保證后續(xù)分析準(zhǔn)確性的基礎(chǔ) ...
2025-07-04CDA 數(shù)據(jù)分析師視角:從數(shù)據(jù)迷霧中探尋商業(yè)真相? 在數(shù)字化浪潮席卷全球的今天,數(shù)據(jù)已成為企業(yè)決策的核心驅(qū)動(dòng)力,CDA(Certifie ...
2025-07-04CDA 數(shù)據(jù)分析師:開(kāi)啟數(shù)據(jù)職業(yè)發(fā)展新征程? ? 在數(shù)據(jù)成為核心生產(chǎn)要素的今天,數(shù)據(jù)分析師的職業(yè)價(jià)值愈發(fā)凸顯。CDA(Certified D ...
2025-07-03