用户:L0_0查看:0 回复:2 评论:0 创建时间:2021-03-06T19:13:55
【作品展示】

【作品介绍】
我使用了tkinter模块做的,没有导入图片。
【作品源代码】
import random
from tkinter import Tk, Button, Label, messagebox
root = Tk()
root.title('扫雷')
root.resizable(width=False, height=False)
first = False
over = False
delei = 10
df = 0
time = 0
def dc(x, y, txt):
global df
global over
global delei
global first
if not first:
first = True
if not over:
if buttons[x, y]['text'] in [1, 2, 3, 4, 5, 6, 7, 8]:
r = 0
try:
if buttons[x-1, y-1]['text'] == '⚐':
r += 1
except:
pass
try:
if buttons[x, y-1]['text'] == '⚐':
r += 1
except:
pass
try:
if buttons[x+1, y-1]['text'] == '⚐':
r += 1
except:
pass
try:
if buttons[x-1, y]['text'] == '⚐':
r += 1
except:
pass
try:
if buttons[x+1, y]['text'] == '⚐':
r += 1
except:
pass
try:
if buttons[x+1, y+1]['text'] == '⚐':
r += 1
except:
pass
try:
if buttons[x-1, y+1]['text'] == '⚐':
r += 1
except:
pass
try:
if buttons[x, y+1]['text'] == '⚐':
r += 1
except:
pass
if buttons[x, y]['text'] == r:
g(x, y)
else:
messagebox.showerror('扫雷', '数字是几,就需要在数字旁边插几个旗子,需要插'+str(buttons[x, y]['text'])+'个旗子,你插了'+str(r)+'个旗子。')
return
if txt == '翻开':
if not over and buttons[x, y]['text'] == '':
if buttons[x, y]['bg'] != 'white':
df += 1
buttons[x, y]['bg'] = 'white'
buttons[x, y]['text'] = name[x, y]
if buttons[x, y]['text'] == 1:
buttons[x, y]['fg'] = 'blue'
buttons[x, y]['activeforeground'] = 'blue'
if buttons[x, y]['text'] == 2:
buttons[x, y]['fg'] = 'green'
buttons[x, y]['activeforeground'] = 'green'
if buttons[x, y]['text'] == 3:
buttons[x, y]['fg'] = 'red'
buttons[x, y]['activeforeground'] = 'red'
if buttons[x, y]['text'] == 4:
buttons[x, y]['fg'] = 'dark blue'
buttons[x, y]['activeforeground'] = 'dark blue'
if buttons[x, y]['text'] == 5:
buttons[x, y]['fg'] = 'brown'
buttons[x, y]['activeforeground'] = 'brown'
if buttons[x, y]['text'] == 6:
buttons[x, y]['fg'] = 'dark green'
buttons[x, y]['activeforeground'] = 'dark green'
if buttons[x, y]['text'] == 7:
buttons[x, y]['fg'] = 'purple'
buttons[x, y]['activeforeground'] = 'purple'
if buttons[x, y]['text'] == 8:
buttons[x, y]['fg'] = 'orange'
buttons[x, y]['activeforeground'] = 'orange'
if buttons[x, y]['text'] == '':
g(x, y)
if buttons[x, y]['text'] == d:
over = True
ac()
buttons[x, y]['bg'] = 'yellow'
messagebox.showinfo('扫雷', '祝你下次好运。')
else:
if df == 71:
al()
messagebox.showinfo('扫雷','恭喜!')
elif txt == '插旗' and not over:
if buttons[x, y]['bg'] != 'white':
if buttons[x, y]['text'] == '' and not over:
buttons[x, y]['text'] = '⚐'
delei -= 1
elif buttons[x, y]['text'] == '⚐':
buttons[x, y]['text'] = '?'
delei += 1
else:
buttons[x, y]['text'] = ''
w['text']='☢:'+str(delei)
def choice():
global buttons
global name
global df
global over
global delei
global time
global first
first = False
c['text'] = '翻开'
delei = 10
time = 0
time_text['text'] = '⏲: 0'
w['text'] = '☢:10'
over=False
df=0
random.shuffle(List)
buttons={}
name={}
for i in range(81):
b = Button(root, command=lambda x=i % 9, y=i//9: dc(x, y, c['text']),
width=6, height=2, bg='light blue', font=('MS PGothic', 20))
b.grid(column=i % 9, row=i//9+1)
buttons[i % 9, i//9] = b
name[i % 9, i//9] = List[i]
dj()
w = Label(root, font=('Ms PGothic', 20), anchor='w', width=6, height=2, text='☢:10')
w.grid(row=0, column=0)
chongxin = Button(root, command=choice, font=('Ms PGothic', 20), width=13, height=2, text='新游戏')
chongxin.grid(row=0,column=3, columnspan=2)
time_text = Label(root, font=('Ms PGothic', 20), anchor='w', width=12, height=2, text='⏲: 0')
time_text.grid(column=1, row=0, columnspan=2)
def g(x,y):
if x-1 >= 0 and y-1 >= 0:
if buttons[x-1, y-1]['bg'] != 'white':
dc(x-1, y-1, '翻开')
if y-1 >= 0:
if buttons[x, y-1]['bg'] != 'white':
dc(x, y-1, '翻开')
if x+1 <= 8 and y-1 >= 0:
if buttons[x+1, y-1]['bg'] != 'white':
dc(x+1, y-1, '翻开')
if x-1 >= 0:
if buttons[x-1, y]['bg'] != 'white':
dc(x-1, y, '翻开')
if x-1 >= 0 and y+1 <= 8:
if buttons[x-1, y+1]['bg'] != 'white':
dc(x-1, y+1, '翻开')
if x+1 <= 8:
if buttons[x+1, y]['bg'] != 'white':
dc(x+1, y, '翻开')
if x+1 <= 8 and y+1 <= 8:
if buttons[x+1, y+1]['bg'] != 'white':
dc(x+1, y+1, '翻开')
if y+1 <= 8:
if buttons[x, y+1]['bg'] != 'white':
dc(x, y+1, '翻开')
def ac():
for x in range(9):
for y in range(9):
if name[x, y] == d:
buttons[x, y]['bg'] = 'white'
if buttons[x, y]['text'] in ['','?']:
buttons[x, y]['text'] = d
elif buttons[x, y]['text'] == '⚐':
buttons[x,y]['text'] = '✅'
elif name[x, y]!=d and buttons[x, y]['text'] != '':
if buttons[x, y]['text'] == '⚐':
buttons[x, y]['text'] = '❌'
buttons[x, y]['bg'] = 'white'
elif buttons[x, y]['text'] == '?':
buttons[x, y]['text'] = ''
def al():
global over
for x in range(9):
for y in range(9):
if name[x, y] == d:
buttons[x, y]['bg'] = 'white'
if name[x, y]==d:
buttons[x, y]['text'] = '✅'
over = True
buttons = {}
name = {}
d = '☢'
List = [d, d, d, d, d, d, d, d, d,
d, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1]
random.shuffle(List)
def wr():
if c['text'] == '翻开':
c['text'] = '插旗'
else:
c['text'] = '翻开'
def easy():
messagebox.showinfo('玩法', '目标是翻开所有没雷的方块,如果触雷,游戏失败。翻开数字,数字代表它周围有多少颗雷,如果你觉得某个地方有雷,可在那里插旗。数字是几,这个数字旁边插了几个旗,可点击数字。当翻到了雷,没喵旗的雷会翻开,喵旗的雷会标记✅,插错的旗子会标记❌。翻开所有没有雷的方块,雷会被标记✅。')
c = Button(root, command=wr, width=13,
height=2, font=('Ms PGothic', 20), text='翻开')
c.grid(row=0, column=5, columnspan=2)
cd = Button(root, command=easy, width=13,
height=2, font=('Ms PGothic', 20), text='玩法')
cd.grid(row=0, column=7, columnspan=2)
for i in range(81):
b = Button(root, command=lambda x=i % 9, y=i//9: dc(x, y, c['text']),
width=6, height=2, bg='light blue', font=('MS PGothic', 20))
b.grid(column=i % 9, row=i//9+1)
buttons[i % 9, i//9] = b
name[i % 9, i//9] = List[i]
def time_append():
global time
global first
if not over and first:
time += 1
time_text['text'] = '⏲: ' + str(time)
root.after(1000, time_append)
cankao = ['', 1, 2, 3, 4, 5, 6, 7, 8]
def dj():
for x in range(9):
for y in range(9):
a = 0
if name[x, y] != d:
try:
if name[x-1, y-1] == d:
a += 1
except:
pass
try:
if name[x, y-1] == d:
a += 1
except:
pass
try:
if name[x+1, y-1] == d:
a += 1
except:
pass
try:
if name[x-1, y] == d:
a += 1
except:
pass
try:
if name[x+1, y] == d:
a += 1
except:
pass
try:
if name[x-1, y+1] == d:
a += 1
except:
pass
try:
if name[x, y+1] == d:
a += 1
except:
pass
try:
if name[x+1, y+1] == d:
a += 1
except:
pass
name[x, y] = cankao[a]
dj()
root.after(1000, time_append)
root.mainloop()
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn