python练手经典100例项目-Python练手项目实例汇总(附源码下载)

1 #_*_ coding:utf-8 _*_

2 from tkinter import *

3 importrandom4 importtime5 importtkinter.messagebox6

7

8 #俄罗斯方块界面的高度

9 HEIGHT = 20

10

11 #俄罗斯方块界面的宽度

12 WIDTH = 10

13

14 ACTIVE = 1

15 PASSIVE =016 TRUE = 1

17 FALSE =018

19 style =[20 [[(0,0),(0,1),(1,1),(2,1)],[(1,0),(1,1),(1,2),(0,2)],[(0,1),(1,1),(2,1),(2,2)],[(1,0),(2,0),(1,1),(1,2)]],#j

21 [[(1,0),(1,1),(1,2),(2,1)],[(1,0),(0,1),(1,1),(2,1)],[(1,0),(1,1),(1,2),(0,1)],[(0,1),(1,1),(2,1),(1,2)]],#T

22 [[(0,1),(1,1),(2,1),(2,0)],[(0,0),(1,0),(1,1),(1,2)],[(0,1),(1,1),(2,1),(0,2)],[(1,0),(1,1),(1,2),(2,2)]],#反L

23 [[(0,0),(0,1),(1,1),(1,2)],[(2,1),(1,1),(1,2),(0,2)],[(0,0),(0,1),(1,1),(1,2)],[(2,1),(1,1),(1,2),(0,2)]],#Z

24 [[(1,0),(1,1),(0,1),(0,2)],[(0,1),(1,1),(1,2),(2,2)],[(1,0),(1,1),(0,1),(0,2)],[(0,1),(1,1),(1,2),(2,2)]],#反Z

25 [[(0,0),(0,1),(1,1),(1,0)],[(0,0),(0,1),(1,1),(1,0)],[(0,0),(0,1),(1,1),(1,0)],[(0,0),(0,1),(1,1),(1,0)]],#田

26 [[(1,0),(1,1),(1,2),(1,3)],[(0,1),(1,1),(2,1),(3,1)],[(1,0),(1,1),(1,2),(1,3)],[(0,1),(1,1),(2,1),(3,1)]]#长条

27 ]28

29 root=Tk();30 root.title('俄罗斯方块')31

32 classApp(Frame):33 def __init__(self,master):34 Frame.__init__(self)35 master.bind('',self.Up)36 master.bind('',self.Left)37 master.bind('',self.Right)38 master.bind('',self.Down)39

40 master.bind('',self.Space)41 master.bind('',self.Play)42 master.bind('',self.Pause)43 master.bind('',self.StartByS)44

45 #rgb颜色值

46 self.backg="#%02x%02x%02x" % (120,150,30) #大背景

47 self.frontg="#%02x%02x%02x" % (40,120,150) #下一个形状颜色

48 self.nextg="#%02x%02x%02x" % (150,100,100) #小背景

49 self.flashg="#%02x%02x%02x" % (210,130,100) #炸的颜色

50

51 self.LineDisplay=Label(master,text='Lines:',bg='black',fg='red')52 self.Line=Label(master,text='0',bg='black',fg='red')53 self.ScoreDisplay=Label(master,text='Score:',bg='black',fg='red')54 self.Score=Label(master,text='0',bg='black',fg='red')55 self.SpendTimeDisplay=Label(master,text='Time:',bg='black',fg='red')56 self.SpendTime=Label(master,text='0.0',bg='black',fg='red')57

58 self.LineDisplay.grid(row=HEIGHT-2,column=WIDTH,columnspan=2)59 self.Line.grid(row=HEIGHT-2,column=WIDTH+2,columnspan=3)60 self.ScoreDisplay.grid(row=HEIGHT-1,column=WIDTH,columnspan=2)61 self.Score.grid(row=HEIGHT-1,column=WIDTH+2,columnspan=3)62 self.SpendTimeDisplay.grid(row=HEIGHT-4,column=WIDTH,columnspan=2)63 self.SpendTime.grid(row=HEIGHT-4,column=WIDTH+2,columnspan=3)64

65 self.TotalTime=0.0

66 self.TotalLine=067 self.TotalScore=068

69 #游戏结束

70 self.isgameover=FALSE71 #暂停

72 self.isPause=FALSE73 #开始

74 self.isStart=FALSE75 self.NextList=[] #整个小背景

76 self.NextRowList=[] #一行小背景

77

78 self.px=079 self.py=0 #记录方块参考点

80

81 #渲染小背景

82 r=0;c=083 for k in range(4*4):84 LN=Label(master,text=' ',bg=str(self.nextg),fg='white',relief=FLAT,bd=3)85 LN.grid(row=r,column=WIDTH+c,sticky=N+E+S+W)86 self.NextRowList.append(LN)87 c=c+1

88 if c>=4:89 r=r+1;c=090 self.NextList.append(self.NextRowList)91 self.NextRowList=[]92

93 #渲染大背景

94 self.BlockList=[]95 self.BlockRowList=[]96 self.LabelList=[]97 self.LabelRowList=[]98 row=0;col=099 for i in range(HEIGHT*WIDTH):100 L=Label(master,text=' ',bg=str(self.backg),fg='white',relief=FLAT,bd=4)101 L.grid(row=row,column=col,sticky=N+E+S+W)102 L.row=row;L.col=col;L.isactive=PASSIVE103 self.BlockRowList.append(0); #大背景每个格子初始化为0值

104 self.LabelRowList.append(L)105 col=col+1

106 if col>=WIDTH:107 row=row+1;col=0108 self.BlockList.append(self.BlockRowList)109 self.LabelList.append(self.LabelRowList)110 self.BlockRowList=[]111 self.LabelRowList=[]112

113 #file

114 fw=open('text.txt','a')115 fw.close()116 hasHead=FALSE117 f=open('text.txt','r')118 if f.read(5)=='score':119 hasHead=TRUE120 f.close()121 self.file=open('text.txt','a')122 if hasHead==FALSE:123 self.file.write('score line time scorePtime linePtime scorePline date/n')124 self.file.flush()125

126 self.time=1000

127 self.OnTimer()128

129 def __del__(self):130 #self.file.close()

131 pass

132

133 defPause(self,event):134 self.isPause=1-self.isPause135

136 defUp(self,event):137 BL=self.BlockList #格子的值

138 LL=self.LabelList #格子Label

139

140 Moveable=TRUE #是否可旋转

141

142 #代码编写开始

143 nowStyle =style[self.xnow][(self.ynow)]144 newStyle = style[self.xnow][(self.ynow+1)%4] #算出下一俄罗斯方块

145 self.ynow = (self.ynow+1)%4 #此行代码非常重要,否则响应UP时,只能变第一次

146

147 print("nowStyle:"+str(nowStyle)+"=====>>newStyle:"+str(newStyle))148

149 #根据现有形状中每个label的坐标计算出旋转后目标坐标(x,y)

150 SourceList=[];DestList=[]151

152 for i in range(4):153 SourceList.append([ nowStyle[i][0]+self.px, nowStyle[i][1]+self.py])154 x = newStyle[i][0]+self.px155 y = newStyle[i][1]+self.py156 DestList.append([x, y])157

158 if x<0 or x>=HEIGHT or y<0 or y>=WIDTH : #or BL[x][y]==1 or LL[x][y].isactive==PASSIVE

159 Moveable=FALSE160

161 if Moveable==TRUE:162 for i inrange(len(SourceList)):163 self.Empty(SourceList[i][0],SourceList[i][1])164 for i inrange(len(DestList)):165 self.Fill(DestList[i][0],DestList[i][1])166

167 defLeft(self,event):168 BL=self.BlockList;LL=self.LabelList169 Moveable=TRUE170 for i inrange(HEIGHT):171 for j inrange(WIDTH):172 if LL[i][j].isactive==ACTIVE and j-1<0:Moveable=FALSE173 if LL[i][j].isactive==ACTIVE and j-1>=0 and BL[i][j-1]==1 and LL[i][j-1].isactive==PASSIVE:Moveable=FALSE174 if Moveable==TRUE:175 self.py-=1

176 for i inrange(HEIGHT):177 for j inrange(WIDTH):178 if j-1>=0 and LL[i][j].isactive==ACTIVE and BL[i][j-1]==0:179 self.Fill(i,j-1);self.Empty(i,j)180

181 defRight(self,event):182 BL=self.BlockList;LL=self.LabelList183 Moveable=TRUE184 for i inrange(HEIGHT):185 for j inrange(WIDTH):186 if LL[i][j].isactive==ACTIVE and j+1>=WIDTH:Moveable=FALSE187 if LL[i][j].isactive==ACTIVE and j+1

190 for i in range(HEIGHT-1,-1,-1):191 for j in range(WIDTH-1,-1,-1):192 if j+1

195 defDown(self,event):196 BL=self.BlockList;LL=self.LabelList197 Moveable=TRUE198 for i inrange(HEIGHT):199 for j inrange(WIDTH):200 if LL[i][j].isactive==ACTIVE and i+1>=HEIGHT:Moveable=FALSE201 if LL[i][j].isactive==ACTIVE and i+1

204 for i in range(HEIGHT-1,-1,-1):205 for j in range(WIDTH-1,-1,-1):206 if i+1

221 defSpace(self,event):222 while 1:223 if self.Down(0)==FALSE:break

224

225 defOnTimer(self):226 if self.isStart==TRUE and self.isPause==FALSE:227 self.TotalTime = self.TotalTime + float(self.time)/1000

228 self.SpendTime.config(text=str(self.TotalTime))229

230 if self.isPause==FALSE:231 self.Down(0)232 if self.TotalScore>=1000:self.time=900

233 if self.TotalScore>=2000:self.time=750

234 if self.TotalScore>=3000:self.time=600

235 if self.TotalScore>=4000:self.time=400

236 self.after(self.time,self.OnTimer) #随着分数增大,俄罗斯方块下降速度加快

237

238 defJudgeLineFill(self):239 BL=self.BlockList;LL=self.LabelList240 count=0;LineList=[]241 for i in range(WIDTH):LineList.append(1)242 #display flash

243 for i inrange(HEIGHT):244 if BL[i]==LineList:245 count=count+1

246 for k inrange(WIDTH):247 LL[i][k].config(bg=str(self.flashg))248 LL[i][k].update()249 if count!=0:self.after(100)250 #delete block

251 for i inrange(HEIGHT):252 if BL[i]==LineList:253 #count=count+1

254 for j in range(i,0,-1):255 for k inrange(WIDTH):256 BL[j][k]=BL[j-1][k]257 LL[j][k]['relief']=LL[j-1][k].cget('relief')258 LL[j][k]['bg']=LL[j-1][k].cget('bg')259 for l inrange(WIDTH):260 BL[0][l]=0261 LL[0][l].config(relief=FLAT,bg=str(self.backg))262 self.TotalLine=self.TotalLine+count263 if count==1:self.TotalScore=self.TotalScore+1*WIDTH264 if count==2:self.TotalScore=self.TotalScore+3*WIDTH265 if count==3:self.TotalScore=self.TotalScore+6*WIDTH266 if count==4:self.TotalScore=self.TotalScore+10*WIDTH267 self.Line.config(text=str(self.TotalLine))268 self.Score.config(text=str(self.TotalScore))269

270 defFill(self,i,j):271 if j<0:return

272 if self.BlockList[i][j]==1:self.isgameover=TRUE273 self.BlockList[i][j]=1

274 self.LabelList[i][j].isactive=ACTIVE275 self.LabelList[i][j].config(relief=RAISED,bg=str(self.frontg))276

277 defEmpty(self,i,j):278 self.BlockList[i][j]=0279 self.LabelList[i][j].isactive=PASSIVE280 self.LabelList[i][j].config(relief=FLAT,bg=str(self.backg))281

282 defPlay(self,event):283 showinfo('Made in China','^_^')284

285 defNextFill(self,i,j):286 self.NextList[i][j].config(relief=RAISED,bg=str(self.frontg))287

288 defNextEmpty(self,i,j):289 self.NextList[i][j].config(relief=FLAT,bg=str(self.nextg))290

291 defDistroy(self):292 #save

293 if self.TotalScore!=0:294 #cehkongfu

295 savestr='%-9u%-8u%-8.2f%-14.2f%-13.2f%-14.2f%s/n' %(296 self.TotalScore,self.TotalLine,self.TotalTime297 ,self.TotalScore/self.TotalTime298 ,self.TotalLine/self.TotalTime299 ,float(self.TotalScore)/self.TotalLine300 ,time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()))301 self.file.seek(0,2)302 self.file.write(savestr)303 self.file.flush()304

305 for i inrange(HEIGHT):306 for j inrange(WIDTH):307 self.Empty(i,j)308 self.TotalLine=0;self.TotalScore=0;self.TotalTime=0.0

309 self.Line.config(text=str(self.TotalLine))310 self.Score.config(text=str(self.TotalScore))311 self.SpendTime.config(text=str(self.TotalTime))312 self.isgameover=FALSE313 self.isStart=FALSE314 self.time=1000

315 for i in range(4):316 for j in range(4):317 self.NextEmpty(i,j)318

319 #游戏开始方块

320 defStart(self):321 nextStyle = style[self.x][self.y] #下一形状

322 self.xnow =self.x323 self.ynow = self.y #记录大背景中的方块

324 self.py = random.randint(0,6)325 print("给py赋任意值:"+str(self.py))326 self.px =0327 for ii in range(4):328 self.Fill(int(nextStyle[ii][0]),int(nextStyle[ii][1])+self.py)329 self.isStart=TRUE #游戏开始

330

331 #预处理方块

332 defRnd(self):333 self.x=random.randint(0,6)334 self.y=random.randint(0,3)335 nextStyle = style[self.x][self.y] #下一形状

336 for ii in range(4):337 self.NextFill(int(nextStyle[ii][0]),int(nextStyle[ii][1]))338

339 #游戏开始给出一次任意形状的方块

340 defRndFirst(self):341 self.x=random.randint(0,6) #选择第一个方块style

342 self.y=random.randint(0,3)343

344 defShow(self):345 self.file.seek(0)346 strHeadLine=self.file.readline()347 dictLine={}348 strTotalLine=''

349 for OneLine inself.file.readlines():350 temp=int(OneLine[:5])351 dictLine[temp]=OneLine352

353 list=sorted(dictLine.items(),key=lambdad:d[0])354 ii=0355 for onerecord inreversed(list):356 ii=ii+1

357 if ii<11:358 strTotalLine+=onerecord[1]359 showinfo('Ranking', strHeadLine+strTotalLine)360

361 defStartByS(self,event):362 self.RndFirst()363 self.Start()364 self.Rnd()365

366 defStart():367 app.RndFirst()368 app.Start()369 app.Rnd()370

371 defEnd():372 app.Distroy()373

374 defSet():375 print("设置功能待完善...")376

377 defShow():378 app.Show()379

380 #主菜单

381 mainmenu=Menu(root)382 root['menu']=mainmenu383

384 #二级菜单:game

385 gamemenu=Menu(mainmenu)386 mainmenu.add_cascade(label='游戏',menu=gamemenu)387 gamemenu.add_command(label='开始',command=Start)388 gamemenu.add_command(label='结束',command=End)389 gamemenu.add_separator()390 gamemenu.add_command(label='退出',command=root.quit)391

392 #二级菜单:set

393 setmenu=Menu(mainmenu)394 mainmenu.add_cascade(label='设置',menu=setmenu)395 setmenu.add_command(label='设置',command=Set)396

397 #二级菜单:show

398 showmenu=Menu(mainmenu)399 mainmenu.add_cascade(label='展示',menu=showmenu)400 showmenu.add_command(label='展示',command=Show)401

402 #绑定功能

403

404 app=App(root)405 #程序入口

406 root.mainloop()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值