python例子練手,python練手經典100例-Python練手項目實例匯總(附源碼下載)

 2023-11-18 阅读 27 评论 0

摘要:1 #_*_ coding:utf-8 _*_ 2 from tkinter import * python例子練手。3 importrandom4 importtime5 importtkinter.messagebox6 7 8 #俄羅斯方塊界面的高度 python編程入門與案例詳解?9 HEIGHT = 20 10 11 #俄羅斯方塊界面的寬度 python入門例子,12 WIDTH = 10 13 14

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

2 from tkinter import *

python例子練手。3 importrandom4 importtime5 importtkinter.messagebox6

7

8 #俄羅斯方塊界面的高度

python編程入門與案例詳解?9 HEIGHT = 20

10

11 #俄羅斯方塊界面的寬度

python入門例子,12 WIDTH = 10

13

14 ACTIVE = 1

python練手?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

100個python練手項目、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()

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/177106.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息