“學(xué)了兩年半Python,用7小時(shí)編了個(gè)掃雷”

Exe文件下載:www.123pan.com/s/MAi7Vv-OzjyH.html
代碼如下(需安裝Python及Pygame模塊):
import pygame,random,time
q=[-1,0,1]
tc=[[255,255,255],[0,0,255],[0,128,0],[255,0,0],[0,0,128],[128,0,0],[0,128,128],[0,0,0],[128,128,128]]
lv=0
m2=0
lv0=[-1,0]
lv1=[-1,0]
lv2=[-1,0]
x=9
y=9
z=10
def start(sx,sy):
??global a
??a0=[]
??for i in range(x*y):
????a0.append([0,0])
??for i in range(z):
????a0[i]=[9,0]
??rt=True
??while rt:
????random.shuffle(a0)
????if a0[sx*x+sy][0]!=9:
??????rt=False
??a=[]
??t=0
??for i in range(x):
????a.append([])
????for j in range(y):
??????a[-1].append(a0[t])
??????t+=1
??for i in range(x):
????for j in range(y):
??????if a[i][j][0]!=9:
????????t=0
????????for k in q:
??????????for l in q:
????????????if k!=0 or l!=0:
??????????????if 0<=i+k<x and 0<=j+l<y:
????????????????if a[i+k][j+l][0]==9:
??????????????????t+=1
????????a[i][j][0]=t
pygame.init()
s=pygame.display.set_mode((640,480))
def txt(t,f,sz,c,tx,ty,c2="a"):
??f0=["times","simsun"]
??if c2=="a":
????tt=pygame.font.SysFont(f0[f],sz).render(t,True,c)
??else:
????tt=pygame.font.SysFont(f0[f],sz).render(t,True,c,c2)
??s.blit(tt,(tx,ty))
def cl():
??if i==pos[0]//32 and j==pos[1]//32:
????pygame.draw.rect(s,(164,255,255),(i*32+1,j*32+1,30,30),0)
??else:
????pygame.draw.rect(s,(255,255,164),(i*32+1,j*32+1,30,30),0)
def op(ax,ay):
??pw=[[ax,ay,0]]
??rp=True
??while rp:
????t=-1
????for i in pw:
??????t+=1
??????if i[2]==0:
????????for j in q:
??????????for k in q:
????????????if j!=0 or k!=0:
??????????????if 0<=i[0]+j<x and 0<=i[1]+k<y:
????????????????t1=0
????????????????for l in pw:
??????????????????if i[0]+j==l[0] and i[1]+k==l[1]:
????????????????????t1=1
????????????????if t1==0:
??????????????????if a[i[0]+j][i[1]+k][0]==0:
????????????????????pw.append([i[0]+j,i[1]+k,0])
??????????????????elif a[i[0]+j][i[1]+k][0]!=9:
????????????????????pw.append([i[0]+j,i[1]+k,1])
????????pw[t][2]=1
????rp=False
????for i in pw:
??????if i[2]==0:
????????rp=True
????for i in pw:
??????a[i[0]][i[1]][1]=3
??for i in pw:
????a[i[0]][i[1]][1]=3
pygame.display.set_caption("掃雷")
ms=0
wl=-2
wll=False
pos=-64,-64
ty=0
kl=[pygame.K_0,pygame.K_1,pygame.K_2,pygame.K_3,pygame.K_4,
??pygame.K_5,pygame.K_6,pygame.K_7,pygame.K_8,pygame.K_9,
??pygame.K_KP0,pygame.K_KP1,pygame.K_KP2,pygame.K_KP3,pygame.K_KP4,
??pygame.K_KP5,pygame.K_KP6,pygame.K_KP7,pygame.K_KP8,pygame.K_KP9,
??pygame.K_BACKSPACE,pygame.K_RETURN]
r=True
wt=-1
stm=time.time()
while r:
??wt+=1
??for event in pygame.event.get():
????if event.type==pygame.QUIT:
??????r=False
????if event.type==pygame.MOUSEMOTION:
??????pos=event.pos
????if event.type==pygame.MOUSEBUTTONUP:
??????if event.button==1:
????????ms=1
??????elif event.button==3:
????????ms=2
??????mp=event.pos
????if event.type==pygame.KEYUP:
??????for i in range(len(kl)):
????????if event.key==kl[i] and m2>0:
??????????if 0<=i<20:
????????????if m2==1 and int(str(x)+str(i%10))<=30:
??????????????x=int(str(x)+str(i%10))
????????????if m2==2 and int(str(y)+str(i%10))<=24:
??????????????y=int(str(y)+str(i%10))
????????????if m2==3 and int(str(z)+str(i%10))<=x*y*0.925:
??????????????z=int(str(z)+str(i%10))
??????????if i==20:
????????????if m2==1:
??????????????x=list(str(x))
??????????????if len(x)==1:
????????????????x=0
??????????????else:
????????????????x.pop()
????????????????x0=""
????????????????for i in x:
??????????????????x0+=i
????????????????x=int(x0)
??????????????if z>x*y*0.925:
????????????????z=int(x*y*0.925)
????????????if m2==2:
??????????????y=list(str(y))
??????????????if len(y)==1:
????????????????y=0
??????????????else:
????????????????y.pop()
????????????????y0=""
????????????????for i in y:
??????????????????y0+=i
????????????????y=int(y0)
??????????????if z>x*y*0.925:
????????????????z=int(x*y*0.925)
????????????if m2==3:
??????????????z=list(str(z))
??????????????if len(z)==1:
????????????????z=0
??????????????else:
????????????????z.pop()
????????????????z0=""
????????????????for i in z:
??????????????????z0+=i
????????????????z=int(z0)
??????????if i==21:
????????????m2=0
??????????if x==9 and y==9 and z==10:
????????????lv=0
??????????elif x==16 and y==16 and z==40:
????????????lv=1
??????????elif x==30 and y==16 and z==99:
????????????lv=2
??????????else:
????????????lv=3
??s.fill((0,192,192))
??if ms==1:
????if wl==-2:
??????if 384<=mp[0]<576 and 352<=mp[1]<416:
????????s=pygame.display.set_mode((x*32+64,y*32))
????????wll=True
??????if 160<=mp[0]<448 and 128<=mp[1]<160:
????????lv=0
????????x=9
????????y=9
????????z=10
??????if 160<=mp[0]<480 and 168<=mp[1]<200:
????????lv=1
????????x=16
????????y=16
????????z=40
??????if 160<=mp[0]<480 and 208<=mp[1]<240:
????????lv=2
????????x=30
????????y=16
????????z=99
??????if 160<=mp[0]<192 and 308<=mp[1]<344:
????????m2=1
????????x=0
????????z=0
??????if 225<=mp[0]<257 and 308<=mp[1]<344:
????????m2=2
????????y=0
????????z=0
??????if 160<=mp[0]<208 and 348<=mp[1]<384:
????????m2=3
????????z=0
????if wl>-2 and x*32<=mp[0]<x*32+64 and 0<=mp[1]<64:
??????wl=-1
????if wl==-1:
??????if 0<=mp[0]<x*32:
????????start(mp[0]//32,mp[1]//32)
????????wl=0
????????tm=time.time()
????if wl==0:
??????if 0<=mp[0]<x*32:
????????if a[mp[0]//32][mp[1]//32][1]==0 or a[mp[0]//32][mp[1]//32][1]==2:
??????????if a[mp[0]//32][mp[1]//32][0]==0:
????????????op(mp[0]//32,mp[1]//32)
??????????elif a[mp[0]//32][mp[1]//32][0]==9:
????????????for i in range(x):
??????????????for j in range(y):
????????????????if a[i][j][0]==9 and a[i][j][1]!=1:
??????????????????a[i][j][1]=3
????????????????elif a[i][j][0]!=9 and a[i][j][1]==1:
??????????????????a[i][j][1]=4
????????????a[mp[0]//32][mp[1]//32][1]=5
????????????wl=1
????????????tm2=time.time()-tm
????????????t=0
????????????for i in range(x):
??????????????for j in range(y):
????????????????if a[i][j][1]==1:
??????????????????t+=1
????????????ty=t
??????????else:
????????????a[mp[0]//32][mp[1]//32][1]=3
??????????if a[mp[0]//32][mp[1]//32][0]!=9:
????????????t=0
????????????for i in range(x):
??????????????for j in range(y):
????????????????if a[i][j][1]<3:
??????????????????t+=1
????????????if t==z:
??????????????wl=2
??????????????tm2=time.time()-tm
??????????????if lv==0:
????????????????lv0[1]+=1
????????????????if tm2<=lv0[0] or lv0[0]==-1:
??????????????????lv0[0]=int(tm2)
??????????????if lv==1:
????????????????lv1[1]+=1
????????????????if tm2<=lv1[0] or lv1[0]==-1:
??????????????????lv1[0]=int(tm2)
??????????????if lv==2:
????????????????lv2[1]+=1
????????????????if tm2<=lv2[0] or lv2[0]==-1:
??????????????????lv2[0]=int(tm2)
??????????????for i in range(x):
????????????????for j in range(y):
??????????????????if a[i][j][1]<3:
????????????????????a[i][j][1]=1???????????
????ms=0
??if ms==2:
????if wl!=-2 and x*32<=mp[0]<x*32+64 and 0<=mp[1]<64:
??????wl=-2
??????s=pygame.display.set_mode((640,480))
????if 0<=mp[0]<x*32 and wl==0:
??????if a[mp[0]//32][mp[1]//32][1]<2:
????????a[mp[0]//32][mp[1]//32][1]+=1
??????elif a[mp[0]//32][mp[1]//32][1]==2:
????????a[mp[0]//32][mp[1]//32][1]=0
????if wl>=0:
??????t=0
??????for i in range(x):
????????for j in range(y):
??????????if a[i][j][1]==1:
????????????t+=1
??????ty=t
????ms=0
??if wll:
????wl=-1
????wll=False
??if wl!=-2:
????if x*32<=pos[0]<x*32+64 and 0<=pos[1]<64:
??????pygame.draw.rect(s,(164,255,255),(x*32+1,1,62,62),0)
????else:
??????pygame.draw.rect(s,(255,255,164),(x*32+1,1,62,62),0)
????if -2<wl<=0:
??????if int(time.time()-stm)%4==0:
????????txt("左鍵",1,30,(0,0,255),x*32+2,15)
??????elif int(time.time()-stm)%4==1:
????????txt("重新",1,30,(0,0,255),x*32+2,2)
????????txt("開始",1,30,(0,0,255),x*32+2,30)
??????elif int(time.time()-stm)%4==2:
????????txt("右鍵",1,30,(0,255,0),x*32+2,15)
??????else:
????????txt("前往",1,30,(0,255,0),x*32+2,2)
????????txt("設(shè)置",1,30,(0,255,0),x*32+2,30)
????elif wl==1:
??????txt("失敗",1,30,(255,0,255),x*32+2,15)
????elif wl==2:
??????txt("成功",1,30,(255,0,0),x*32+2,15)
??if wl==-2:
????pygame.draw.rect(s,(255,255,255),(384,352,192,64),0)
????txt("確定",1,48,(0,192,192),430,358)
????txt("掃雷",1,64,(255,255,255),256,32)
????txt("級(jí)別:",1,32,(255,255,255),64,128)
????if lv==0:
??????txt("初級(jí)(9×9,10雷)",1,32,(0,192,192),160,128,(255,255,255))
????else:
??????txt("初級(jí)(9×9,10雷)",1,32,(255,255,255),160,128)
????if lv==1:
??????txt("中級(jí)(16×16,40雷)",1,32,(0,192,192),160,168,(255,255,255))
????else:
??????txt("中級(jí)(16×16,40雷)",1,32,(255,255,255),160,168)
????if lv==2:
??????txt("高級(jí)(30×16,99雷)",1,32,(0,192,192),160,208,(255,255,255))
????else:
??????txt("高級(jí)(30×16,99雷)",1,32,(255,255,255),160,208)
????if lv==3:
??????txt("自定義",1,32,(0,192,192),160,248,(255,255,255))
????else:
??????txt("自定義",1,32,(255,255,255),160,248)
????txt("大小",1,32,(255,255,255),64,308)
????if m2==1:
??????txt(str(x),0,32,(0,192,192),160,308,(255,255,255))
????else:
??????txt(str(x),0,32,(255,255,255),160,308)
????txt("×",0,32,(255,255,255),200,308)
????if m2==2:
??????txt(str(y),0,32,(0,192,192),225,308,(255,255,255))
????else:
??????txt(str(y),0,32,(255,255,255),225,308)
????txt("雷數(shù)",1,32,(255,255,255),64,348)
????if m2==3:
??????txt(str(z),0,32,(0,192,192),160,348,(255,255,255))
????else:
??????txt(str(z),0,32,(255,255,255),160,348)
??elif wl==-1:
????for i in range(x):
??????for j in range(y):
????????cl()
??else:
????for i in range(x):
??????for j in range(y):
????????if a[i][j][1]==0:
??????????cl()
????????elif a[i][j][1]==1:
??????????cl()
??????????txt("f",0,30,(255,0,0),i*32+8,j*32-1)
????????elif a[i][j][1]==2:
??????????cl()
??????????txt("?",0,30,(0,0,0),i*32+8,j*32-1)
????????elif a[i][j][1]==3:
??????????pygame.draw.rect(s,(255,255,255),(i*32+1,j*32+1,30,30),0)
??????????if a[i][j][0]==9:
????????????txt("m",0,30,(255,0,255),i*32+4,j*32-1)
??????????else:
????????????txt(str(a[i][j][0]),0,30,tc[a[i][j][0]],i*32+9,j*32-1)
????????elif a[i][j][1]==4:
??????????pygame.draw.rect(s,(255,255,255),(i*32+1,j*32+1,30,30),0)
??????????txt("m",0,30,(128,128,128),i*32+4,j*32-1)
??????????txt("×",0,45,(128,128,128),i*32+2,j*32-9)
????????elif a[i][j][1]==5:
??????????pygame.draw.rect(s,(255,0,255),(i*32+1,j*32+1,30,30),0)
??????????txt("m",0,30,(255,255,255),i*32+4,j*32-1)
??if wl>=0:
????txt("時(shí)間",1,30,(255,255,255),x*32+2,64)????
????txt("雷",1,24,(255,255,255),x*32+2,128)
????txt(str(z-ty),0,24,(255,255,255),x*32+26,128)
????if wl==0:
??????txt(str(int(time.time()-tm)),0,30,(255,255,255),x*32+8,96)
????else:
??????txt(str(int(tm2)),0,30,(255,255,255),x*32+8,96)
??if wl!=-2:
????if lv==0:
??????txt("初級(jí)",1,30,(255,255,255),x*32+2,256)
??????txt("已勝",1,20,(255,255,255),x*32+2,155)
??????txt(str(lv0[1]),0,30,(255,255,255),x*32+2,175)
??????txt("次",1,20,(255,255,255),x*32+36,180)
??????if lv0[0]!=-1:
????????txt("最快",1,20,(255,255,255),x*32+2,205)
????????txt(str(lv0[0]),0,30,(255,255,255),x*32+2,225)
????????txt("秒",1,20,(255,255,255),x*32+44,230)
????if lv==1:
??????txt("中級(jí)",1,30,(255,255,255),x*32+2,480)
??????txt("已勝",1,30,(255,255,255),x*32+2,160)
??????txt(str(lv1[1]),0,30,(255,255,255),x*32+2,192)
??????txt("次",1,30,(255,255,255),x*32+36,192)
??????if lv1[0]!=-1:
????????txt("最快",1,30,(255,255,255),x*32+2,224)
????????txt(str(lv1[0]),0,25,(255,255,255),x*32+2,256)
????????txt("秒",1,20,(255,255,255),x*32+44,260)
????if lv==2:
??????txt("高級(jí)",1,30,(255,255,255),x*32+2,480)
??????txt("已勝",1,30,(255,255,255),x*32+2,160)
??????txt(str(lv2[1]),0,30,(255,255,255),x*32+2,192)
??????txt("次",1,30,(255,255,255),x*32+36,192)
??????if lv2[0]!=-1:
????????txt("最快",1,30,(255,255,255),x*32+2,224)
????????txt(str(lv2[0]),0,25,(255,255,255),x*32+2,256)
????????txt("秒",1,20,(255,255,255),x*32+44,260)
??pygame.display.update()
“學(xué)了兩年半Python,用7小時(shí)編了個(gè)掃雷”的評(píng)論 (共 條)
