Python扫雷游戏

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 = '&#9873;'   # 标记
   s_mine = '&#9760;'  # 雷
   s_success = '&#128076;'

   # 按钮是否按下(默认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('&#128547;')
               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('&#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)

       ## 计数器
       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('&#128522;')
       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_())
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,640评论 6 507
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,254评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,011评论 0 355
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,755评论 1 294
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,774评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,610评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,352评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,257评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,717评论 1 315
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,894评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,021评论 1 350
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,735评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,354评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,936评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,054评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,224评论 3 371
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,974评论 2 355

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,332评论 0 10
  • 背景 Python 是 Google主要的脚本语言。这本风格指南主要包含的是针对python的编程准则。 为帮助读...
    派派森森阅读 829评论 0 0
  • #========================================================...
    无涯2016阅读 1,077评论 0 0
  • SwiftDay011.MySwiftimport UIKitprintln("Hello Swift!")var...
    smile丽语阅读 3,837评论 0 6
  • 久违的晴天,家长会。 家长大会开好到教室时,离放学已经没多少时间了。班主任说已经安排了三个家长分享经验。 放学铃声...
    飘雪儿5阅读 7,523评论 16 22