收藏 分享(赏)

Python-俄罗斯方块游戏.doc

上传人:scg750829 文档编号:7135108 上传时间:2019-05-06 格式:DOC 页数:8 大小:53.50KB
下载 相关 举报
Python-俄罗斯方块游戏.doc_第1页
第1页 / 共8页
Python-俄罗斯方块游戏.doc_第2页
第2页 / 共8页
Python-俄罗斯方块游戏.doc_第3页
第3页 / 共8页
Python-俄罗斯方块游戏.doc_第4页
第4页 / 共8页
Python-俄罗斯方块游戏.doc_第5页
第5页 / 共8页
点击查看更多>>
资源描述

1、【Python 】用 Python 实现一个俄罗斯方块游戏俄罗斯方块游戏,使用 Python 实现,总共有 350+行代码,实现了俄罗斯方块游戏的基本功能,同时会记录所花费时间,消去的总行数,所得的总分,还包括一个排行榜,可以查看最高记录。排行榜中包含一系列的统计功能,如单位时间消去的行数,单位时间得分等。附源码:from Tkinter import *from tkMessageBox import *import randomimport time#俄罗斯方块界面的高度HEIGHT = 18#俄罗斯方块界面的宽度WIDTH = 10ACTIVE = 1PASSIVE = 0TRUE =

2、1FALSE = 0root=Tk();root.title(Russia)class App(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.Space)master.bind(,self.Play)master.bind(,self.Pause)self.backg=“#%02

3、x%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:

4、,bg=black,fg=red)self.Score=Label(master,text=0,bg=black,fg=red)#Display timeself.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,col

5、umnspan=3)self.ScoreDisplay.grid(row=HEIGHT-1,column=WIDTH,columnspan=2)self.Score.grid(row=HEIGHT-1,column=WIDTH+2,columnspan=3)#Display timeself.SpendTimeDisplay.grid(row=HEIGHT-4,column=WIDTH,columnspan=2)self.SpendTime.grid(row=HEIGHT-4,column=WIDTH+2,columnspan=3)self.TotalTime=0.0self.TotalLin

6、e=0;self.TotalScore=0#Game overself.isgameover=FALSE#Pauseself.isPause=FALSE#Startself.isStart=FALSEself.NextList=;self.NextRowList=r=0;c=0for k in range(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=

7、c+1if c=4:r=r+1;c=0self.NextList.append(self.NextRowList)self.NextRowList=self.BlockList=;self.LabelList=self.BlockRowList=;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;

8、L.col=col;L.isactive=PASSIVEself.BlockRowList.append(0);self.LabelRowList.append(L)col=col+1if col=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,

9、r)if f.read(5)=score:hasHead=TRUEf.close()self.file=open(text.txt,r+a)if hasHead=FALSE:self.file.write(score line time scorePtime linePtime scorePline daten)self.file.flush()self.time=1000self.OnTimer()def _del_(self):#self.file.close()passdef Pause(self,event):self.isPause=1-self.isPausedef Up(self

10、,event):BL=self.BlockList;LL=self.LabelListMoveable=TRUExtotal=0;ytotal=0;count=0for i in range(HEIGHT):for j in range(WIDTH):if LLij.isactive=ACTIVE:xtotal=xtotal+i;ytotal=ytotal+j;count=count+1SourceList=;DestList=for i in range(HEIGHT):for j in range(WIDTH):if LLij.isactive=ACTIVE:x0=(xtotal+ytot

11、al)/count;y0=(ytotal-xtotal )/countxr=(xtotal+ytotal)%count;yr=(ytotal-xtotal)%countx=x0-j;y=y0+iif xr=count/2:x=x+1if yr=count/2:y=y+1SourceList.append(i,j);DestList.append(x,y)if x=HEIGHT or y=WIDTH:Moveable=FALSEif x=0 and x=0 and y=0 and BLij-1=1 and LLij-1.isactive=PASSIVE:Moveable=FALSEif Move

12、able=TRUE:for i in range(HEIGHT):for j in range(WIDTH):if j-1=0 and LLij.isactive=ACTIVE and BLij-1=0:self.Fill(i,j-1);self.Empty(i,j)def Right(self,event):BL=self.BlockList;LL=self.LabelListMoveable=TRUEfor i in range(HEIGHT):for j in range(WIDTH):if LLij.isactive=ACTIVE and j+1=WIDTH:Moveable=FALS

13、Eif LLij.isactive=ACTIVE and j+1=1000:self.time=900if self.TotalScore=2000:self.time=750if self.TotalScore=3000:self.time=600if self.TotalScore=4000:self.time=400self.after(self.time,self.OnTimer)def Down(self,event):BL=self.BlockList;LL=self.LabelListMoveable=TRUEfor i in range(HEIGHT):for j in ran

14、ge(WIDTH):if LLij.isactive=ACTIVE and i+1=HEIGHT:Moveable=FALSEif LLij.isactive=ACTIVE and i+1)def NextFill(self,i,j):self.NextListij.config(relief=RAISED,bg=str(self.frontg)def NextEmpty(self,i,j):self.NextListij.config(relief=FLAT,bg=str(self.nextg)def Distroy(self):#saveif self.TotalScore!=0:save

15、str=%-9u%-8u%-8.2f%-14.2f%-13.2f%-14.2f%sn % (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()f

16、or i in range(HEIGHT):for j in range(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=FALSEself.isStart=FALSEself.time=1000for i

17、 in range(4):for j in range(4):self.NextEmpty(i,j)def Start(self):if self.x=1:self.Fill(0,WIDTH/2-2);self.Fill(0,WIDTH/2-1);self.Fill(0,WIDTH/2);self.Fill(0,WIDTH/2+1)if self.x=2:self.Fill(0,WIDTH/2-1);self.Fill(0,WIDTH/2);self.Fill(1,WIDTH/2-1);self.Fill(1,WIDTH/2)if self.x=3:self.Fill(0,WIDTH/2);s

18、elf.Fill(1,WIDTH/2-1);self.Fill(1,WIDTH/2);self.Fill(1,WIDTH/2+1)if self.x=4:self.Fill(0,WIDTH/2-1);self.Fill(1,WIDTH/2-1);self.Fill(1,WIDTH/2);self.Fill(1,WIDTH/2+1)if self.x=5:self.Fill(0,WIDTH/2+1);self.Fill(1,WIDTH/2-1);self.Fill(1,WIDTH/2);self.Fill(1,WIDTH/2+1)if self.x=6:self.Fill(0,WIDTH/2-1

19、);self.Fill(0,WIDTH/2);self.Fill(1,WIDTH/2);self.Fill(1,WIDTH/2+1)if self.x=7:self.Fill(0,WIDTH/2);self.Fill(0,WIDTH/2+1);self.Fill(1,WIDTH/2-1);self.Fill(1,WIDTH/2)self.isStart=TRUEdef Rnd(self):self.x=random.randint(1,7)if self.x=1:self.NextFill(0,0);self.NextFill(0,1);self.NextFill(0,2);self.Next

20、Fill(0,3)if self.x=2:self.NextFill(0,1);self.NextFill(0,2);self.NextFill(1,1);self.NextFill(1,2)if self.x=3:self.NextFill(0,2);self.NextFill(1,1);self.NextFill(1,2);self.NextFill(1,3)if self.x=4:self.NextFill(0,1);self.NextFill(1,1);self.NextFill(1,2);self.NextFill(1,3)if self.x=5:self.NextFill(0,3)

21、;self.NextFill(1,1);self.NextFill(1,2);self.NextFill(1,3)if self.x=6:self.NextFill(0,1);self.NextFill(0,2);self.NextFill(1,2);self.NextFill(1,3)if self.x=7:self.NextFill(0,2);self.NextFill(0,3);self.NextFill(1,1);self.NextFill(1,2)def RndFirst(self):self.x=random.randint(1,7)def Show(self):self.file

22、.seek(0)strHeadLine=self.file.readline()dictLine=strTotalLine=for OneLine in self.file.readlines():temp=int(OneLine:5)dictLinetemp=OneLinelist=sorted(dictLine.items(),key=lambda d:d0)ii=0 for onerecord in reversed(list):ii=ii+1if ii11:strTotalLine+=onerecord1showinfo(Ranking, strHeadLine+strTotalLin

23、e)def Start():app.RndFirst();app.Start();app.Rnd()def End():app.Distroy()def Set():passdef Show():app.Show()mainmenu=Menu(root)rootmenu=mainmenugamemenu=Menu(mainmenu)mainmenu.add_cascade(label=game,menu=gamemenu)gamemenu.add_command(label=start,command=Start)gamemenu.add_command(label=end,command=E

24、nd)gamemenu.add_separator()gamemenu.add_command(label=exit,command=root.quit)setmenu=Menu(mainmenu)mainmenu.add_cascade(label=set,menu=setmenu)setmenu.add_command(label=set,command=Set)showmenu=Menu(mainmenu)mainmenu.add_cascade(label=show,menu=showmenu)showmenu.add_command(label=show,command=Show)app=App(root)root.mainloop()

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 企业管理 > 管理学资料

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报