也没啥可介绍哒,扫雷大家都玩过。
雷的分布算法也很简单,就是在雷地图(map:二维数组)中,随机放雷,然后这个雷的8个方位(上下左右、四个对角)的数字(非雷的标记、加一后不为雷的标记)都加一。
如何判断是否踩到雷有很多种方法,只要判断某个控件对应map中的值是否为雷的标记即可,其余的都是PyQt5的控件操作,没啦~~~
#coding: utf-8
* 扫雷小游戏
* 需要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 = '