<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
本文範例為大家分享了Python實現簡單掃雷遊戲的具體程式碼,供大家參考,具體內容如下
#coding: utf-8 __note__ = """ * 掃雷小遊戲 * 需要python3.x以上 * 需要安裝PyQt5 * pip install PyQt5 """ import sys try: import PyQt5 except ImportError: import tkinter from tkinter import messagebox err_str = "請安裝PyQt5後再開啟: pip install PyQt5" messagebox.showerror("模組錯誤!", err_str) raise ImportError(err_str) sys.exit() from random import randint from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QLCDNumber, QDesktopWidget, QMessageBox from PyQt5.QtCore import Qt class Mine(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 _ in range(height): t_line = [] for _ in range(width): t_line.append(self.no_mine) self.map.append(t_line) self.width = width self.height = height self.n_mine = nMines self.remix() # 打亂佈局重新隨機編排 def remix(self): for y in range(self.height): for x in range(self.width): self.map[y][x] = self.no_mine def add_mark(x, y): # 如果不是雷的標記就+1 if self.map[y][x]+1 < self.mine: self.map[y][x] += 1 mine_count = 0 while mine_count < self.n_mine: x = randint(0, self.width-1) y = randint(0, self.height-1) if self.map[y][x] != self.mine: self.map[y][x] = self.mine mine_count += 1 # 雷所在的位置的8個方位的數值+1 ## 上下左右 if y-1 >= 0: add_mark(x, y-1) if y+1 < self.height: add_mark(x, y+1) if x-1 >= 0: add_mark(x-1, y) if x+1 < self.width: add_mark(x+1, y) ## 四個角: 左上角、左下角、右上角、右下角 if x-1 >= 0 and y-1 >=1: add_mark(x-1, y-1) if x-1 >= 0 and y+1 < self.height: add_mark(x-1, y+1) if x+1 < self.width and y-1 >= 1: add_mark(x+1, y-1) if x+1 < self.width and y+1 < self.height: add_mark(x+1, y+1) def __getitem__(self, key): return self.map[key] def __str__(self): format_str = "" for y in range(self.height): format_str += str(self[y]) + "n" return format_str __repr__ = __str__ class LCDCounter(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 def counter(self): return self.__counter @counter.setter def counter(self, value): self.__counter = value self.display(str(self.__counter)) def inc(self): self.counter += 1 def dec(self): self.counter -= 1 class MineButton(QPushButton): # 按鈕型別 MINE = Mine.mine # 雷 NOTMINE = Mine.no_mine # 不是雷 m_type = None # 按鈕狀態 mark = False # 是否是標記狀態(預設: 未被標記) s_flag = '⚑' # 標記 s_mine = '☠' # 雷 s_success = '👌' # 按鈕是否按下(預設False: 未按下) __pushed = False # 按鈕對應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 def pushed(self): return not self.__pushed @pushed.setter def pushed(self, value): self.__pushed = not value self.setEnabled(self.__pushed) ## 按鈕上的滑鼠按下事件 def mousePressEvent(self, e): #print("m_x:%d"%self.m_x, "m_y:%d"%self.m_y, "m_type:%d"%self.m_type) p = self.parent() # 記錄滑鼠單擊次數 p.nwap_lcd_clicked.counter += 1 # 左鍵掃雷 if e.buttons() == Qt.LeftButton: # 踩中雷, 全部雷都翻起來 if self.m_type == self.MINE: for t_line_btn in p.btn_map: for btn in t_line_btn: if btn.m_type == btn.MINE: btn.setText(btn.s_mine) else: if btn.mark != True: if btn.m_type != btn.NOTMINE: btn.setText(str(btn.m_type)) btn.pushed = True # 苦逼臉 p.RestartBtn.setText('😣') QMessageBox.critical(self, "失敗!", "您不小心踩到了雷! " + self.s_mine) return None elif self.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 # 右鍵新增標記 elif e.buttons() == Qt.RightButton: if self.mark == False: self.setText(self.s_flag) self.mark = True else: self.setText("") self.mark = False self.setFocus(False) ## 當按下的位置是NOTMINE時自動掃雷 def AutoSwap(self, x, y): p = self.parent() map_btn = p.btn_map def lookup(t_line, index): # 向左掃描 i = index while i >= 0 and not t_line[i].pushed and t_line[i].m_type != MineButton.MINE: if t_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 if t_line[i].m_type != MineButton.NOTMINE: break # 向右掃描 i = index + 1 while i < p.mine_map.width and not t_line[i].pushed and t_line[i].m_type != MineButton.MINE: if t_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 if t_line[i].m_type != MineButton.NOTMINE: break # 向上掃描 j = y while j >= 0: lookup(map_btn[j], x) j -= 1 # 向下掃描 j = y + 1 while j < p.mine_map.height: lookup(map_btn[j], x) j += 1 class MineWindow(QWidget): def __init__(self): super().__init__() self.mine_map = Mine(nMines=16) self.InitGUI() #print(self.mine_map) def InitGUI(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 # 雷區按鈕 self.btn_map = [] for h in range(self.mine_map.height): l_x = l_start_x self.btn_map.append(list()) for w in range(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 # 恢復按鈕 self.RestartBtn = QPushButton('😊', 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) ## 計數器 self.__mine_counter = self.mine_map.width * self.mine_map.height - self.mine_map.n_mine ## 兩個LCD顯示控制元件 # 操作次數 self.nwap_lcd_clicked = LCDCounter(0, self) self.nwap_lcd_clicked.move(44, 8) # 無雷塊個數 self.nwap_lcd_counter = LCDCounter(self.mine_counter, self) self.nwap_lcd_counter.move(204, 8) def restart_btn_event(self): self.mine_map.remix() #QMessageBox.information(self, "look up", str(self.mine_map)) for y in range(len(self.btn_map)): for x in range(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 = self.mine_map[y][x] self.mine_counter = self.mine_map.width * self.mine_map.height - self.mine_map.n_mine self.RestartBtn.setText('😊') self.nwap_lcd_clicked.counter = 0 self.nwap_lcd_counter.counter = self.mine_counter ### 計數器 @property def mine_counter(self): return self.__mine_counter @mine_counter.setter def mine_counter(self, value): self.__mine_counter = value self.nwap_lcd_counter.dec() if self.mine_counter == 0: for t_line_btn in self.btn_map: for btn in t_line_btn: if btn.m_type == btn.MINE: btn.setText(btn.s_success) btn.pushed = True QMessageBox.information(self, "恭喜!", "您成功掃雷! " + MineButton.s_success) if __name__ == '__main__': app = QApplication(sys.argv) w = MineWindow() w.show() sys.exit(app.exec_())
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援it145.com。
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45