扫雷源代码
本帖最后由 张育玮 于 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 == -99:
# 游戏失败,游戏结束
gameover()
# 按钮上显示周围雷的数量
else:
self.strvar.set(str(mines))
# 安全按钮的个数减一
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.grid(row=i, column=j)
# 设置雷
def set_mine():
for rand in range(9):
# 每个位置的初始值设为0,-99代表雷
mines.append()
# 随机生成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 = -99
else:
tmp = int(each / 9)
index = each - tmp * 9
if index == 0:
mines = -99
else:
mines[(each - tmp * 9) - 1] = -99
# 计算mines周围地雷的数量
if mines != -99:
if mines == -99:
mines += 1
if mines == -99:
mines += 1
if mines == -99:
mines += 1
# 计算mines周围地雷的数量
if mines != -99:
if mines == -99:
mines += 1
if mines == -99:
mines += 1
if mines == -99:
mines += 1
# 计算mines周围地雷的数量
if mines != -99:
if mines == -99:
mines += 1
if mines == -99:
mines += 1
if mines == -99:
mines += 1
# 计算mines周围地雷的数量
if mines != -99:
if mines == -99:
mines += 1
if mines == -99:
mines += 1
if mines == -99:
mines += 1
# 计算上侧按钮附近雷的个数
for i in range(1, 8):
if mines != -99:
if mines == -99:
mines += 1
if mines == -99:
mines += 1
if mines == -99:
mines += 1
if mines == -99:
mines += 1
if mines == -99:
mines += 1
# 计算下侧按钮附近雷的个数
for i in range(1, 8):
if mines != -99:
if mines == -99:
mines += 1
if mines == -99:
mines += 1
if mines == -99:
mines += 1
if mines == -99:
mines += 1
if mines == -99:
mines += 1
# 计算左侧按钮附近雷的个数
for i in range(1, 8):
if mines != -99:
if mines == -99:
mines += 1
if mines == -99:
mines += 1
if mines == -99:
mines += 1
if mines == -99:
mines += 1
if mines == -99:
mines += 1
# 计算右侧按钮附近雷的个数
for i in range(1, 8):
if mines != -99:
if mines == -99:
mines += 1
if mines == -99:
mines += 1
if mines == -99:
mines += 1
if mines == -99:
mines += 1
if mines == -99:
mines += 1
# 计算中间剩余7x7每个按钮周围的雷数
for i in range(1, 8):
for j in range(1, 8):
if mines != -99:
if mines == -99:
mines += 1
if mines == -99:
mines += 1
if mines == -99:
mines += 1
if mines == -99:
mines += 1
if mines == -99:
mines += 1
if mines == -99:
mines += 1
if mines == -99:
mines += 1
if mines == -99:
mines += 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["bg"] = "red"
btns["fg"] = "yellow"
btns.settext("√")
# 此时其余按钮不可按下
for i in range(0, 81):
if i not in record:
btns.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["fg"] = "red"
btns["bg"] = "black"
btns.settext("×")
for i in range(0, 81):
if i not in record:
btns.config(state="disabled")
# 初始化游戏界面
init()
# 设置地雷
set_mine()
# 窗口时间循环
root.mainloop()
先来支持一波 底纹很影响阅读体验
建议楼主换掉 昨非 发表于 2020-11-28 15:51
底纹很影响阅读体验
建议楼主换掉
好的 1
页:
[1]