Python-俄罗斯方块游戏

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

【Python】用Python实现一个俄罗斯方块游戏俄罗斯方块游戏,使用Python实现,总共有350+行代码,实现了俄罗斯方块游戏的基本功能,同时会记录所花费时间,消去的总行数,所得的总分,还包括一个排行榜,可以查看最高记录。排行榜中包含一系列的统计功能,如单位时间消去的行数,单位时间得分等。附源码:fromTkinterimport*fromtkMessageBoximport*importrandomimporttime#俄罗斯方块界面的高度HEIGHT=18#俄罗斯方块界面的宽度WIDTH=10ACTIVE=1PASSIVE=0TRUE=1FALSE=0root=Tk();root.title('Russia')classApp(Frame):def__init__(self,master):Frame.__init__(self)master.bind('Up',self.Up)master.bind('Left',self.Left)master.bind('Right',self.Right)master.bind('Down',self.Down)#master.bind('Down',self.Space)master.bind('space',self.Space)master.bind('Control-Shift-Key-F12',self.Play)master.bind('Key-F6',self.Pause)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')#Displaytimeself.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)#Displaytimeself.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#Gameoverself.isgameover=FALSE#Pauseself.isPause=FALSE#Startself.isStart=FALSEself.NextList=[];self.NextRowList=[]r=0;c=0forkinrange(4*4):LN=Label(master,text='',bg=str(self.nextg),fg='white',relief=FLAT,bd=4)LN.grid(row=r,column=WIDTH+c,sticky=N+E+S+W)self.NextRowList.append(LN)c=c+1ifc=4:r=r+1;c=0self.NextList.append(self.NextRowList)self.NextRowList=[]self.BlockList=[];self.LabelList=[]self.BlockRowList=[];self.LabelRowList=[]row=0;col=0foriinrange(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=PASSIVEself.BlockRowList.append(0);self.LabelRowList.append(L)col=col+1ifcol=WIDTH:row=row+1;col=0self.BlockList.append(self.BlockRowList)self.LabelList.append(self.LabelRowList)self.BlockRowList=[];self.LabelRowList=[]#filefw=open('text.txt','a')fw.close()hasHead=FALSEf=open('text.txt','r')iff.read(5)=='score':hasHead=TRUEf.close()self.file=open('text.txt','r+a')ifhasHead==FALSE:self.file.write('scorelinetimescorePtimelinePtimescorePlinedate\n')self.file.flush()self.time=1000self.OnTimer()def__del__(self):#self.file.close()passdefPause(self,event):self.isPause=1-self.isPausedefUp(self,event):BL=self.BlockList;LL=self.LabelListMoveable=TRUExtotal=0;ytotal=0;count=0foriinrange(HEIGHT):forjinrange(WIDTH):ifLL[i][j].isactive==ACTIVE:xtotal=xtotal+i;ytotal=ytotal+j;count=count+1SourceList=[];DestList=[]foriinrange(HEIGHT):forjinrange(WIDTH):ifLL[i][j].isactive==ACTIVE:x0=(xtotal+ytotal)/count;y0=(ytotal-xtotal)/countxr=(xtotal+ytotal)%count;yr=(ytotal-xtotal)%countx=x0-j;y=y0+iifxr=count/2:x=x+1ifyr=count/2:y=y+1SourceList.append([i,j]);DestList.append([x,y])ifx0orx=HEIGHTory0ory=WIDTH:Moveable=FALSEifx=0andxHEIGHTandy=0andyWIDTHandBL[x][y]==1andLL[x][y].isactive==PASSIVE:Moveable=FALSEifMoveable==TRUE:foriinrange(len(SourceList)):self.Empty(SourceList[i][0],SourceList[i][1])foriinrange(len(DestList)):self.Fill(DestList[i][0],DestList[i][1])defLeft(self,event):BL=self.BlockList;LL=self.LabelListMoveable=TRUEforiinrange(HEIGHT):forjinrange(WIDTH):ifLL[i][j].isactive==ACTIVEandj-10:Moveable=FALSEifLL[i][j].isactive==ACTIVEandj-1=0andBL[i][j-1]==1andLL[i][j-1].isactive==PASSIVE:Moveable=FALSEifMoveable==TRUE:foriinrange(HEIGHT):forjinrange(WIDTH):ifj-1=0andLL[i][j].isactive==ACTIVEandBL[i][j-1]==0:self.Fill(i,j-1);self.Empty(i,j)defRight(self,event):BL=self.BlockList;LL=self.LabelListMoveable=TRUEforiinrange(HEIGHT):forjinrange(WIDTH):ifLL[i][j].isactive==ACTIVEandj+1=WIDTH:Moveable=FALSEifLL[i][j].isactive==ACTIVEandj+1WIDTHandBL[i][j+1]==1andLL[i][j+1].isactive==PASSIVE:Moveable=FALSEifMoveable==TRUE:foriinrange(HEIGHT-1,-1,-1):forjinrange(WIDTH-1,-1,-1):ifj+1WIDTHandLL[i][j].isactive==ACTIVEandBL[i][j+1]==0:self.Fill(i,j+1);self.Empty(i,j)defSpace(self,event):while1:ifself.Down(0)==FALSE:breakdefOnTimer(self):ifself.isStart==TRUEandself.isPause==FALSE:self.TotalTime=self.TotalTime+float(self.time)/1000self.SpendTime.config(text=str(self.TotalTime))ifself.isPause==FALSE:self.Down(0)ifself.TotalScore=1000:self.time=900ifself.TotalScore=2000:self.time=750ifself.TotalScore=3000:self.time=600ifself.TotalScore=4000:self.time=400self.after(self.time,self.OnTimer)defDown(self,event):BL=self.BlockList;LL=self.LabelListMoveable=TRUEforiinrange(HEIGHT):forjinrange(WIDTH):ifLL[i][j].isactive==ACTIVEandi+1=HEIGHT:Moveable=FALSEifLL[i][

1 / 8
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功