完整的python專案例項-Python例項練手專案彙總(附原始碼)
#_*_ coding:utf-8 _*_
from tkinter import *
importrandomimporttimeimporttkinter.messagebox#俄羅斯方塊介面的高度
HEIGHT = 20
#俄羅斯方塊介面的寬度
WIDTH = 10ACTIVE= 1PASSIVE=0
TRUE= 1FALSE=0
style=[
[[(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
[[(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
[[(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
[[(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
[[(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
[[(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)]],#田
[[(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)]]#長條
]
root=Tk();
root.title("俄羅斯方塊")classApp(Frame):def __init__(self,master):
Frame.__init__(self)
master.bind("",self.Up)
master.bind("",self.Left)
master.bind("",self.Right)
master.bind("",self.Down)
master.bind("",self.Space)
master.bind("",self.Play)
master.bind("",self.Pause)
master.bind("",self.StartByS)#rgb顏色值
self.backg="#%02x%02x%02x" % (120,150,30) #大背景
self.frontg="#%02x%02x%02x" % (40,120,150) #下一個形狀顏色
self.nextg="#%02x%02x%02x" % (150,100,100) #小背景
self.flashg="#%02x%02x%02x" % (210,130,100) #炸的顏色
self.LineDisplay=Label(master,text="Lines:",bg="black",fg="red")
self.Line=Label(master,text="0",bg="black",fg="red")
self.ScoreDisplay=Label(master,text="Score:",bg="black",fg="red")
self.Score=Label(master,text="0",bg="black",fg="red")
self.SpendTimeDisplay=Label(master,text="Time:",bg="black",fg="red")
self.SpendTime=Label(master,text="0.0",bg="black",fg="red")
self.LineDisplay.grid(row=HEIGHT-2,column=WIDTH,columnspan=2)
self.Line.grid(row=HEIGHT-2,column=WIDTH+2,columnspan=3)
self.ScoreDisplay.grid(row=HEIGHT-1,column=WIDTH,columnspan=2)
self.Score.grid(row=HEIGHT-1,column=WIDTH+2,columnspan=3)
self.SpendTimeDisplay.grid(row=HEIGHT-4,column=WIDTH,columnspan=2)
self.SpendTime.grid(row=HEIGHT-4,column=WIDTH+2,columnspan=3)
self.TotalTime=0.0self.TotalLine=0
self.TotalScore=0#遊戲結束
self.isgameover=FALSE#暫停
self.isPause=FALSE#開始
self.isStart=FALSE
self.NextList=[] #整個小背景
self.NextRowList=[] #一行小背景
self.px=0
self.py=0 #記錄方塊參考點
#渲染小背景
r=0;c=0for k in range(4*4):
LN=Label(master,text=" ",bg=str(self.nextg),fg="white",relief=FLAT,bd=3)
LN.grid(row=r,column=WIDTH+c,sticky=N+E+S+W)
self.NextRowList.append(LN)
c=c+1
if c>=4:
r=r+1;c=0
self.NextList.append(self.NextRowList)
self.NextRowList=[]#渲染大背景
self.BlockList=[]
self.BlockRowList=[]
self.LabelList=[]
self.LabelRowList=[]
row=0;col=0for i in range(HEIGHT*WIDTH):
L=Label(master,text=" ",bg=str(self.backg),fg="white",relief=FLAT,bd=4)
L.grid(row=row,column=col,sticky=N+E+S+W)
L.row=row;L.col=col;L.isactive=PASSIVE
self.BlockRowList.append(0);#大背景每個格子初始化為0值
self.LabelRowList.append(L)
col=col+1
if col>=WIDTH:
row=row+1;col=0
self.BlockList.append(self.BlockRowList)
self.LabelList.append(self.LabelRowList)
self.BlockRowList=[]
self.LabelRowList=[]#file
fw=open("text.txt","a")
fw.close()
hasHead=FALSE
f=open("text.txt","r")if f.read(5)=="score":
hasHead=TRUE
f.close()
self.file=open("text.txt","a")if hasHead==FALSE:
self.file.write("score line time scorePtime linePtime scorePline date/n")
self.file.flush()
self.time=1000self.OnTimer()def __del__(self):#self.file.close()
pass
defPause(self,event):
self.isPause=1-self.isPausedefUp(self,event):
BL=self.BlockList #格子的值
LL=self.LabelList #格子Label
Moveable=TRUE #是否可旋轉
#程式碼編寫開始
nowStyle =style[self.xnow][(self.ynow)]
newStyle= style[self.xnow][(self.ynow+1)%4] #算出下一俄羅斯方塊
self.ynow = (self.ynow+1)%4 #此行程式碼非常重要,否則響應UP時,只能變第一次
print("nowStyle:"+str(nowStyle)+"=====>>newStyle:"+str(newStyle))#根據現有形狀中每個label的座標計算出旋轉後目標座標(x,y)
SourceList=[];DestList=[]for i in range(4):
SourceList.append([ nowStyle[i][0]+self.px, nowStyle[i][1]+self.py])
x= newStyle[i][0]+self.px
y= newStyle[i][1]+self.py
DestList.append([x, y])if x<0 or x>=HEIGHT or y<0 or y>=WIDTH : #or BL[x][y]==1 or LL[x][y].isactive==PASSIVE
Moveable=FALSEif Moveable==TRUE:for i inrange(len(SourceList)):
self.Empty(SourceList[i][0],SourceList[i][1])for i inrange(len(DestList)):
self.Fill(DestList[i][0],DestList[i][1])defLeft(self,event):
BL=self.BlockList;LL=self.LabelList
Moveable=TRUEfor i inrange(HEIGHT):for j inrange(WIDTH):if LL[i][j].isactive==ACTIVE and j-1<0:Moveable=FALSEif LL[i][j].isactive==ACTIVE and j-1>=0 and BL[i][j-1]==1 and LL[i][j-1].isactive==PASSIVE:Moveable=FALSEif Moveable==TRUE:
self.py-=1
for i inrange(HEIGHT):for j inrange(WIDTH):if j-1>=0 and LL[i][j].isactive==ACTIVE and BL[i][j-1]==0:
self.Fill(i,j-1);self.Empty(i,j)defRight(self,event):
BL=self.BlockList;LL=self.LabelList
Moveable=TRUEfor i inrange(HEIGHT):for j inrange(WIDTH):if LL[i][j].isactive==ACTIVE and j+1>=WIDTH:Moveable=FALSEif LL[i][j].isactive==ACTIVE and j+1
self.py+=1
for i in range(HEIGHT-1,-1,-1):for j in range(WIDTH-1,-1,-1):if j+1
self.Fill(i,j+1);self.Empty(i,j)defDown(self,event):
BL=self.BlockList;LL=self.LabelList
Moveable=TRUEfor i inrange(HEIGHT):for j inrange(WIDTH):if LL[i][j].isactive==ACTIVE and i+1>=HEIGHT:Moveable=FALSEif LL[i][j].isactive==ACTIVE and i+1
self.px+=1
for i in range(HEIGHT-1,-1,-1):for j in range(WIDTH-1,-1,-1):if i+1
self.Fill(i+1,j);self.Empty(i,j);if Moveable==FALSE:for i inrange(HEIGHT):for j inrange(WIDTH):
LL[i][j].isactive=PASSIVE
self.JudgeLineFill()
self.Start()if self.isgameover==TRUE:showinfo("T_T","The game is over!");self.Distroy();returnFALSEfor i in range(4):for j in range(4):
self.NextEmpty(i,j)
self.Rnd()returnMoveabledefSpace(self,event):while 1:if self.Down(0)==FALSE:break
defOnTimer(self):if self.isStart==TRUE and self.isPause==FALSE:
self.TotalTime= self.TotalTime + float(self.time)/1000self.SpendTime.config(text=str(self.TotalTime))if self.isPause==FALSE:
self.Down(0)if self.TotalScore>=1000:self.time=900
if self.TotalScore>=2000:self.time=750
if self.TotalScore>=3000:self.time=600
if self.TotalScore>=4000:self.time=400self.after(self.time,self.OnTimer)#隨著分數增大,俄羅斯方塊下降速度加快
defJudgeLineFill(self):
BL=self.BlockList;LL=self.LabelList
count=0;LineList=[]for i in range(WIDTH):LineList.append(1)#display flash
for i inrange(HEIGHT):if BL[i]==LineList:
count=count+1
for k inrange(WIDTH):
LL[i][k].config(bg=str(self.flashg))
LL[i][k].update()if count!=0:self.after(100)#delete block
for i inrange(HEIGHT):if BL[i]==LineList:#count=count+1
for j in range(i,0,-1):for k inrange(WIDTH):
BL[j][k]=BL[j-1][k]
LL[j][k]["relief"]=LL[j-1][k].cget("relief")
LL[j][k]["bg"]=LL[j-1][k].cget("bg")for l inrange(WIDTH):
BL[0][l]=0
LL[0][l].config(relief=FLAT,bg=str(self.backg))
self.TotalLine=self.TotalLine+countif count==1:self.TotalScore=self.TotalScore+1*WIDTHif count==2:self.TotalScore=self.TotalScore+3*WIDTHif count==3:self.TotalScore=self.TotalScore+6*WIDTHif count==4:self.TotalScore=self.TotalScore+10*WIDTH
self.Line.config(text=str(self.TotalLine))
self.Score.config(text=str(self.TotalScore))defFill(self,i,j):if j<0:return
if self.BlockList[i][j]==1:self.isgameover=TRUE
self.BlockList[i][j]=1self.LabelList[i][j].isactive=ACTIVE
self.LabelList[i][j].config(relief=RAISED,bg=str(self.frontg))defEmpty(self,i,j):
self.BlockList[i][j]=0
self.LabelList[i][j].isactive=PASSIVE
self.LabelList[i][j].config(relief=FLAT,bg=str(self.backg))defPlay(self,event):
showinfo("Made in China","^_^")defNextFill(self,i,j):
self.NextList[i][j].config(relief=RAISED,bg=str(self.frontg))defNextEmpty(self,i,j):
self.NextList[i][j].config(relief=FLAT,bg=str(self.nextg))defDistroy(self):#save
if self.TotalScore!=0:#cehkongfu
savestr="%-9u%-8u%-8.2f%-14.2f%-13.2f%-14.2f%s/n" %(
self.TotalScore,self.TotalLine,self.TotalTime
,self.TotalScore/self.TotalTime
,self.TotalLine/self.TotalTime
,float(self.TotalScore)/self.TotalLine
,time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()))
self.file.seek(0,2)
self.file.write(savestr)
self.file.flush()for i inrange(HEIGHT):for j inrange(WIDTH):
self.Empty(i,j)
self.TotalLine=0;self.TotalScore=0;self.TotalTime=0.0self.Line.config(text=str(self.TotalLine))
self.Score.config(text=str(self.TotalScore))
self.SpendTime.config(text=str(self.TotalTime))
self.isgameover=FALSE
self.isStart=FALSE
self.time=1000
for i in range(4):for j in range(4):
self.NextEmpty(i,j)#遊戲開始方塊
defStart(self):
nextStyle= style[self.x][self.y] #下一形狀
self.xnow =self.x
self.ynow= self.y #記錄大背景中的方塊
self.py = random.randint(0,6)print("給py賦任意值:"+str(self.py))
self.px=0for ii in range(4):
self.Fill(int(nextStyle[ii][0]),int(nextStyle[ii][1])+self.py)
self.isStart=TRUE #遊戲開始
#預處理方塊
defRnd(self):
self.x=random.randint(0,6)
self.y=random.randint(0,3)
nextStyle= style[self.x][self.y] #下一形狀
for ii in range(4):
self.NextFill(int(nextStyle[ii][0]),int(nextStyle[ii][1]))#遊戲開始給出一次任意形狀的方塊
defRndFirst(self):
self.x=random.randint(0,6) #選擇第一個方塊style
self.y=random.randint(0,3)defShow(self):
self.file.seek(0)
strHeadLine=self.file.readline()
dictLine={}
strTotalLine=""
for OneLine inself.file.readlines():
temp=int(OneLine[:5])
dictLine[temp]=OneLine
list=sorted(dictLine.items(),key=lambdad:d[0])
ii=0for onerecord inreversed(list):
ii=ii+1
if ii<11:
strTotalLine+=onerecord[1]
showinfo("Ranking", strHeadLine+strTotalLine)defStartByS(self,event):
self.RndFirst()
self.Start()
self.Rnd()defStart():
app.RndFirst()
app.Start()
app.Rnd()defEnd():
app.Distroy()defSet():print("設定功能待完善...")defShow():
app.Show()#主選單
mainmenu=Menu(root)
root["menu"]=mainmenu#二級選單:game
gamemenu=Menu(mainmenu)
mainmenu.add_cascade(label="遊戲",menu=gamemenu)
gamemenu.add_command(label="開始",command=Start)
gamemenu.add_command(label="結束",command=End)
gamemenu.add_separator()
gamemenu.add_command(label="退出",command=root.quit)#二級選單:set
setmenu=Menu(mainmenu)
mainmenu.add_cascade(label="設定",menu=setmenu)
setmenu.add_command(label="設定",command=Set)#二級選單:show
showmenu=Menu(mainmenu)
mainmenu.add_cascade(label="展示",menu=showmenu)
showmenu.add_command(label="展示",command=Show)#繫結功能
app=App(root)#程式入口
root.mainloop()
相關文章
- 完整的python專案例項-python完整專案Python
- Python爬蟲專案100例,附原始碼!100個Python爬蟲練手例項Python爬蟲原始碼
- python專案例項原始碼-32個Python爬蟲實戰專案,滿足你的專案慌(帶原始碼)Python原始碼爬蟲
- 完整的python專案例項-《Python爬蟲開發與專案實戰》pdf完整版Python爬蟲
- Python專案實戰例項Python
- python專案開發例項書-關於開發Python專案的心得總結Python
- 全網最全Python專案練習500例(附原始碼),練完可就業Python原始碼就業
- 70個Python經典實用練手專案(附原始碼)Python原始碼
- MVVM+Reactive Cocoa專案完整例項MVVMReact
- python專案開發例項-Python專案案例開發從入門到實戰——爬蟲、遊戲Python爬蟲遊戲
- python爬蟲例項專案大全-GitHub 上有哪些優秀的 Python 爬蟲專案?Python爬蟲Github
- python大型專案應用例項,最全python專案開發案例,吃透這些知識,小白也能變大牛...Python
- python開發例項-python開發案例Python
- 爬蟲的例項專案爬蟲
- 吐血總結!10個Python實戰專案(附原始碼)Python原始碼
- python練手經典100例-推薦幾個適合新手練手的Python專案《python入門例子》Python
- Vue專案入門例項Vue
- PyQt5案例彙總(完整版)QT
- python互動式專案例項 外星人入侵Python
- Python 描述符(Descriptor) 附例項Python
- 學python找不到專案練手?別擔心,70個python練手專案給你充實感Python
- 【提測平臺】測試平臺開發練手專案原始碼和教程彙總原始碼
- python socket例項Python
- python例項1Python
- 專案資源管理流程例項
- SpringBoot + ES基本專案搭建例項Spring Boot
- 給Python初學者的最好練手專案Python
- 20240302 專項訓練
- 20240309 專項訓練
- CF專項訓練
- 專項訓練們
- 7個Python實戰專案(附原始碼),拿走就用Python原始碼
- RyuBook1.0案例一:SwitchingHub專案原始碼分析原始碼
- Python實現簡單網頁圖片抓取完整程式碼例項Python網頁
- Python例項集錦Python
- python鬧鐘例項Python
- python100例項Python
- python 類和例項Python