资源简介
用python实现扫雷,非常有意思。
大家下载看看吧!
全部都是干货
代码片段和文件信息
import random
import time
from tkinter import *
mine = 100 #代表地雷
Pro = 20# 放大比例
class Sweep:
def __init__(selfwidthheight):
self.width = width ; self.height = height
self.degree = 100
self.count = 0
self.live = True
self.map =[[0 for col in range(width)]for row in range(height)]
self.dist = [[0 for col in range(self.width)]for row in range(self.height)]
for i in range(self.degree):
x = random.randint(0width-1)
y = random.randint(0height-1)
self.map[x][y] = mine
for row in range(0width):
for col in range(0height):
if self.map[row][col] != mine:
cnt = 0
for i in range(row-1row+2):
for j in range(col-1col+2):
try:
if self.map[i][j]==mine and i>-1 and i-1 and j cnt+=1
except:
continue
self.map[row][col] = cnt
self.SetWindows()
def SetWindows(self):
#设置窗口
self.w=Tk()
self.w.title(“ 扫雷“)
self.w.resizable(width=Falseheight=False)
#布置画布
self.c=Canvas(self.wwidth=self.width*Proheight=self.height*Probg=‘white‘)
self.c.place(x=0y=0)
self.c.grid(row=0column=0sticky=W)
#绑定按钮
self.w.bind(““self.callback1)
self.w.bind(““self.callback2)
self.w.bind(““self.callback3)
self.showmap()
def showmap(self):
for i in range(0self.width):
for j in range(0self.height):
self.c.create_rectangle(j*Proi*Proj*Pro+Proi*Pro+Pro\
fill=‘white‘outline=‘black‘width=2tags=(‘all‘))
def Death(self):
for i in range(0self.height):
for j in range(0self.width):
if self.map[i][j]==mine:
self.c.create_rectangle(i*Proj*Proi*Pro+Proj*Pro+Pro\
fill=‘red‘outline=‘black‘width=2tags=(‘all‘))
else:
self.c.create_rectangle(i*Proj*Proi*Pro+Proj*Pro+Pro\
fill=‘gray‘outline=‘black‘width=2tags=(‘all‘))
self.c.create_text((self.width/2)*Pro(self.height/2)*Protext=‘You Are Death‘font=(‘Times‘35))
def win(self):
self.c.create_text((self.width/2)*Pro(self.height/2)*Protext=‘You Are Winner‘font=(‘Times‘35))
def Blank(selfrowcol):
for x in range(row-1row+2):
for y in range(col-1col+2):
if x>-1 and x-1 and y
评论
共有 条评论