马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 张育玮 于 2020-11-28 18:36 编辑
由于python样式没学好,所以不是很好看,请大家原谅 ^ - ^
上代码!!!
# author:zwj
# date:2018-1-30
# 扫雷 v1.0
import random
import tkinter
# 创建程序窗口
root = tkinter.Tk()
# 窗口标题
root.title("扫雷v1.0")
# 窗口大小和显示位置
root.geometry('470x425+500+150')
btns = [] # 存储生成的按钮
mines = [] # 存储地雷
# 按钮类
class MyButton(tkinter.Button):
# 剩余安全按钮的个数
left = 71
def __init__(self, obj, string, wid, hei):
self.flag = True
# 按钮上显示的字符
self.strvar = tkinter.StringVar()
# 调用父类的__init__方法
super(MyButton, self).__init__(obj, textvariable=self.strvar, width=wid, height=hei, command=self.check)
self.index = int(string)
# 设置按钮上要显示的字符
def settext(self, text):
self.strvar.set(text)
# 判断是否为雷
def check(self):
# 判断所有安全的按钮是否全部别点击了
if MyButton.left == 0:
# 游戏胜利
win()
# 判断点击的按钮是否为雷
else:
index_x = 0
if self.index < 10:
index_y = self.index - 1
else:
tmp = int(self.index / 9)
index_y = self.index - tmp * 9
if index_y == 0:
index_x = tmp - 1
index_y = 8
else:
index_x = tmp
index_y = index_y - 1
if mines[index_x][index_y] == -99:
# 游戏失败,游戏结束
gameover()
# 按钮上显示周围雷的数量
else:
self.strvar.set(str(mines[index_x][index_y]))
# 安全按钮的个数减一
if self.flag:
MyButton.left -= 1
self.flag = False
def init(x=9, y=9):
# 按钮的序号
count = 1
for index in range(x):
for index2 in range(y):
# 生成新的按钮
btn = MyButton(root, str(count), 6, 2)
count += 1
# 将生成的按钮加入按钮列表中
btns.append(btn)
for i in range(x):
for j in range(y):
# 按钮布置到窗口中
btns[i * 9 + j].grid(row=i, column=j)
# 设置雷
def set_mine():
for rand in range(9):
# 每个位置的初始值设为0,-99代表雷
mines.append([0, 0, 0, 0, 0, 0, 0, 0, 0])
# 随机生成1~81中的十个数,用来设置雷
random_list = []
for i in range(10):
tmp = random.randint(1, 81)
if tmp not in random_list:
random_list.append(tmp)
# 在相应的位置设置-99表示雷
for each in random_list:
if each < 10:
mines[0][each - 1] = -99
else:
tmp = int(each / 9)
index = each - tmp * 9
if index == 0:
mines[tmp - 1][8] = -99
else:
mines[tmp][(each - tmp * 9) - 1] = -99
# 计算mines[0][0]周围地雷的数量
if mines[0][0] != -99:
if mines[0][1] == -99:
mines[0][0] += 1
if mines[1][0] == -99:
mines[0][0] += 1
if mines[1][1] == -99:
mines[0][0] += 1
# 计算mines[0][8]周围地雷的数量
if mines[0][8] != -99:
if mines[0][7] == -99:
mines[0][8] += 1
if mines[1][7] == -99:
mines[0][8] += 1
if mines[1][8] == -99:
mines[0][8] += 1
# 计算mines[8][0]周围地雷的数量
if mines[8][0] != -99:
if mines[7][0] == -99:
mines[8][0] += 1
if mines[7][1] == -99:
mines[8][0] += 1
if mines[8][1] == -99:
mines[8][0] += 1
# 计算mines[8][8]周围地雷的数量
if mines[8][8] != -99:
if mines[7][7] == -99:
mines[8][8] += 1
if mines[8][7] == -99:
mines[8][8] += 1
if mines[7][8] == -99:
mines[8][8] += 1
# 计算上侧按钮附近雷的个数
for i in range(1, 8):
if mines[0][i] != -99:
if mines[0][i - 1] == -99:
mines[0][i] += 1
if mines[0][i + 1] == -99:
mines[0][i] += 1
if mines[1][i - 1] == -99:
mines[0][i] += 1
if mines[1][i] == -99:
mines[0][i] += 1
if mines[1][i + 1] == -99:
mines[0][i] += 1
# 计算下侧按钮附近雷的个数
for i in range(1, 8):
if mines[8][i] != -99:
if mines[8][i - 1] == -99:
mines[8][i] += 1
if mines[8][i + 1] == -99:
mines[8][i] += 1
if mines[7][i - 1] == -99:
mines[8][i] += 1
if mines[7][i] == -99:
mines[8][i] += 1
if mines[7][i + 1] == -99:
mines[8][i] += 1
# 计算左侧按钮附近雷的个数
for i in range(1, 8):
if mines[i][0] != -99:
if mines[i - 1][0] == -99:
mines[i][0] += 1
if mines[i + 1][0] == -99:
mines[i][0] += 1
if mines[i - 1][1] == -99:
mines[i][0] += 1
if mines[i][1] == -99:
mines[i][0] += 1
if mines[i + 1][1] == -99:
mines[i][0] += 1
# 计算右侧按钮附近雷的个数
for i in range(1, 8):
if mines[i][8] != -99:
if mines[i - 1][8] == -99:
mines[i][8] += 1
if mines[i + 1][8] == -99:
mines[i][8] += 1
if mines[i - 1][7] == -99:
mines[i][8] += 1
if mines[i][7] == -99:
mines[i][8] += 1
if mines[i + 1][7] == -99:
mines[i][8] += 1
# 计算中间剩余7x7每个按钮周围的雷数
for i in range(1, 8):
for j in range(1, 8):
if mines[i][j] != -99:
if mines[i - 1][j - 1] == -99:
mines[i][j] += 1
if mines[i + 1][j + 1] == -99:
mines[i][j] += 1
if mines[i - 1][j] == -99:
mines[i][j] += 1
if mines[i - 1][j + 1] == -99:
mines[i][j] += 1
if mines[i][j - 1] == -99:
mines[i][j] += 1
if mines[i][j + 1] == -99:
mines[i][j] += 1
if mines[i + 1][j - 1] == -99:
mines[i][j] += 1
if mines[i + 1][j] == -99:
mines[i][j] += 1
# 玩家胜利
def win():
record = []
count = 0
# 找出所有地雷的位置
for item in mines:
for each in item:
if each == -99:
record.append(count)
count += 1
else:
count += 1
# 显示所有地雷
for index in record:
btns[index]["bg"] = "red"
btns[index]["fg"] = "yellow"
btns[index].settext("√")
# 此时其余按钮不可按下
for i in range(0, 81):
if i not in record:
btns[i].config(state="disabled")
# 游戏失败
def gameover():
record = []
count = 0
for item in mines:
for each in item:
if each == -99:
record.append(count)
count += 1
else:
count += 1
for index in record:
btns[index]["fg"] = "red"
btns[index]["bg"] = "black"
btns[index].settext("×")
for i in range(0, 81):
if i not in record:
btns[i].config(state="disabled")
# 初始化游戏界面
init()
# 设置地雷
set_mine()
# 窗口时间循环
root.mainloop()
|