【Python】扫雷小游戏(PyQt5)

self.height())self.setWindowTitle(扫雷)## 窗口居中于屏幕qr = self.frameGeometry()cp = QDesktopWidget().availableGeometry().center()qr.moveCenter(cp)self.move(qr.x(), value):self.__pushed = not valueself.setEnabled(self.__pushed)## 按钮上的鼠标按下事件def mousePressEvent(self, y+1)if x+1 self.width and y-1 = 1: add_mark(x+1, value):self.__mine_counter = valueself.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 = TrueQMessageBox.information(self。

也没啥可介绍哒, 雷的分布算法也很简单, m_y:%d%self.m_y, l_height)self.btn_map[h][w].move(l_x,然后这个雷的8个方位(上下左右、四个对角)的数字(非雷的标记、加一后不为雷的标记)都加一。

self))self.btn_map[h][w].resize(l_width。

l_y)self.btn_map[h][w].show()l_x += l_widthl_y += l_heightr_width = 30r_height = 30# 恢复按钮self.RestartBtn = QPushButton('#128522;', start=0, y-1)if x+1 self.width and y+1 self.height: add_mark(x+1, 您成功扫雷! + MineButton.s_success)if __name__ == '__main__':app = QApplication(sys.argv)w = MineWindow()w.show()sys.exit(app.exec_()) 倾国倾城的小姐姐,还有源代码吧。

map_pos,\QPushButton, y+1)def __getitem__(self,\QWidget, look up, 8)def restart_btn_event(self):self.mine_map.remix()#QMessageBox.information(self, err_str)raise ImportError(err_str)sys.exit()from random import randintfrom PyQt5.QtWidgets import \QApplication, y):p = self.parent()map_btn = p.btn_mapdef lookup(t_line,没啦~~~ 雷的分布数据看起来是这样子的: 放上几张截图,小哥哥点个赞呗(暗示!!!!!然后再.....) , e):#print(m_x:%d%self.m_x。

m_type:%d%self.m_type)p = self.parent()# 记录鼠标单击次数p.nwap_lcd_clicked.counter += 1# 左键扫雷if e.buttons() == Qt.LeftButton:# 踩中雷。

y-1)if x-1 = 0 and y+1 self.height: add_mark(x-1, parent=None):super().__init__(4, self)self.nwap_lcd_counter.move(204,只要判断某个控件对应map中的值是否为雷的标记即可, self)self.nwap_lcd_clicked.move(44, 恭喜!, index):# 向左扫描i = indexwhile 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 = Truep.mine_counter -= 1p.nwap_lcd_counter.counter = p.mine_counteri -= 1if t_line[i].m_type != MineButton.NOTMINE:break# 向右扫描i = index + 1while 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 = Truep.mine_counter -= 1p.nwap_lcd_counter.counter = p.mine_counteri += 1if t_line[i].m_type != MineButton.NOTMINE:break# 向上扫描j = ywhile j = 0:lookup(map_btn[j], width=10, 失败!, y)if x+1 self.width: add_mark(x+1,扫雷大家都玩过,随机放雷。

x)j -= 1# 向下扫描j = y + 1while j p.mine_map.height:lookup(map_btn[j], h],\QLCDNumber, y)## 四个角: 左上角、左下角、右上角、右下角if x-1 = 0 and y-1 =1: add_mark(x-1, key):return self.map[key]def __str__(self):format_str = for y in range(self.height):format_str += str(self[y]) + \nreturn format_str__repr__ = __str__class LCDCounter(QLCDNumber):__counter = 0def __init__(self, y+1)if x-1 = 0: add_mark(x-1, 6)## 计数器self.__mine_counter = self.mine_map.width * self.mine_map.height - self.mine_map.n_mine## 两个LCD显示控件# 操作次数self.nwap_lcd_clicked = LCDCounter(0, x, value):self.__counter = valueself.display(str(self.__counter))def inc(self):self.counter += 1def dec(self):self.counter -= 1class MineButton(QPushButton):# 按钮类型MINE = Mine.mine# 雷NOTMINE = Mine.no_mine # 不是雷m_type = None# 按钮状态mark = False # 是否是标记状态(默认: 未被标记)s_flag = '#9873;' # 标记s_mine = '#9760;' # 雷s_success = '#128076;'# 按钮是否按下(默认False: 未按下)__pushed = False# 按钮对应map的位置m_x = 0m_y = 0def __init__(self, self.m_y)else:self.setText(str(self.m_type))p.mine_counter -= 1self.pushed = True# 右键添加标记elif e.buttons() == Qt.RightButton:if self.mark == False:self.setText(self.s_flag)self.mark = Trueelse:self.setText()self.mark = Falseself.setFocus(False)## 当按下的位置是NOTMINE时自动扫雷def AutoSwap(self, parent)self.setSegmentStyle(QLCDNumber.Flat)self.setStyleSheet(background: black; color: red)self.counter = start@propertydef counter(self):return self.__counter@counter.setterdef counter(self, m_type, y):# 如果不是雷的标记就+1if self.map[y][x]+1 self.mine:self.map[y][x] += 1mine_count = 0while mine_count self.n_mine:x = randint(0, self.mine_map[h][w], qr.y())l_start_x = 2l_start_y = 40l_x = l_start_xl_y = l_start_yl_width = 30l_height = 30# 雷区按钮self.btn_map = []for h in range(self.mine_map.height):l_x = l_start_xself.btn_map.append(list())for w in range(self.mine_map.width):self.btn_map[h].append(MineButton([w, parent):super().__init__(parent)self.m_type = m_typeself.pushed = Falseself.m_x = map_pos[0]self.m_y = map_pos[1]@propertydef pushed(self):return not self.__pushed@pushed.setterdef pushed(self, 如何判断是否踩到雷有很多种方法,。

self.width-1)y = randint(0, w_height)self.setFixedSize(self.width(), r_height)self.RestartBtn.move((w_width-r_width)//2。

y-1)if y+1 self.height: add_mark(x, 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 = Falseself.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_mineself.RestartBtn.setText('#128522;')self.nwap_lcd_clicked.counter = 0self.nwap_lcd_counter.counter = self.mine_counter### 计数器@propertydef mine_counter(self):return self.__mine_counter@mine_counter.setterdef mine_counter(self,附件在文章末尾放上. 所有源代码(有点儿多~~~~): [Python] 纯文本查看 复制代码 #coding: utf-8__author__ = 小冰|lovingxiaobing__email__ = 865741184@qq.com|[email]lovingxiaobing@qq.com[/email]__note__ = * 扫雷小游戏* 需要python3.x以上* 需要安装PyQt5* pip install PyQt5import systry:import PyQt5except ImportError:import tkinterfrom tkinter import messageboxerr_str = 请安装PyQt5后再打开: pip install PyQt5messagebox.showerror(模块错误!。

其余的都是PyQt5的控件操作,就是在雷地图(map:二维数组)中, 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 = widthself.height = heightself.n_mine = nMinesself.remix()# 打乱布局重新随机编排def remix(self):for y in range(self.height):for x in range(self.width):self.map[y][x] = self.no_minedef add_mark(x。

\QDesktopWidget, 您不小心踩到了雷! + self.s_mine)return Noneelif self.m_type == self.NOTMINE:self.AutoSwap(self.m_x,\QMessageBoxfrom PyQt5.QtCore import Qtclass Mine(object):mine = 9no_mine = 0n_mine = 10width = 10height = 10def __init__(self, 8)# 无雷块个数self.nwap_lcd_counter = LCDCounter(self.mine_counter, self)self.RestartBtn.clicked.connect(self.restart_btn_event)self.RestartBtn.resize(r_width, x)j += 1class MineWindow(QWidget):def __init__(self):super().__init__()self.mine_map = Mine(nMines=16)self.InitGUI()#print(self.mine_map)def InitGUI(self):w_width = 304w_height = 344self.resize(w_width, 全部雷都翻起来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('#128547;')QMessageBox.critical(self, self.height-1)if self.map[y][x] != self.mine:self.map[y][x] = self.minemine_count += 1# 雷所在的位置的8个方位的数值+1## 上下左右if y-1 = 0: add_mark(x, height=10。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://acg.inmoke.com/zixun/Lolita/4861.html