用Python编写一个简单的俄罗斯方块游戏的教程
admin
2023-07-31 02:16:14
0

俄罗斯方块游戏,使用Python实现,总共有350+行代码,实现了俄罗斯方块游戏的基本功能,同时会记录所花费时间,消去的总行数,所得的总分,还包括一个排行榜,可以查看最高记录。

排行榜中包含一系列的统计功能,如单位时间消去的行数,单位时间得分等。

 

附源码:

   

from Tkinter import * 
  from tkMessageBox import * 
  import random 
  import time 
  #俄罗斯方块界面的高度 
  HEIGHT = 18 
  #俄罗斯方块界面的宽度 
  WIDTH  = 10 
  ACTIVE = 1 
  PASSIVE = 0 
  TRUE  = 1 
  FALSE  = 0 
  root=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=\"#%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\') 
      #Display time 
      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) 
      #Display time 
      self.SpendTimeDisplay.grid(row=HEIGHT-4,column=WIDTH,columnspan=2) 
      self.SpendTime.grid(row=HEIGHT-4,column=WIDTH+2,columnspan=3) 
      self.TotalTime=0.0 
      self.TotalLine=0;self.TotalScore=0 
      #Game over 
      self.isgameover=FALSE 
      #Pause 
      self.isPause=FALSE 
      #Start 
      self.isStart=FALSE 
      self.NextList=[];self.NextRowList=[] 
      r=0;c=0 
      for 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=c+1 
        if c>=4: 
          r=r+1;c=0 
          self.NextList.append(self.NextRowList) 
          self.NextRowList=[] 
      self.BlockList=[];self.LabelList=[] 
      self.BlockRowList=[];self.LabelRowList=[] 
      row=0;col=0 
      for 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);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\',\'r+a\') 
      if hasHead==FALSE: 
        self.file.write(\'score  line  time  scorePtime  linePtime  scorePline  date/n\') 
        self.file.flush() 
         
      self.time=1000 
      self.OnTimer() 
    def __del__(self): 
      #self.file.close() 
      pass 
       
    def Pause(self,event): 
      self.isPause=1-self.isPause 
    def Up(self,event): 
      BL=self.BlockList;LL=self.LabelList 
      Moveable=TRUE 
      xtotal=0;ytotal=0;count=0 
      for i in range(HEIGHT): 
        for j in range(WIDTH): 
          if LL[i][j].isactive==ACTIVE: 
            xtotal=xtotal+i;ytotal=ytotal+j;count=count+1 
      SourceList=[];DestList=[] 
      for i in range(HEIGHT): 
        for j in range(WIDTH): 
          if LL[i][j].isactive==ACTIVE: 
            x0=(xtotal+ytotal)/count;y0=(ytotal-xtotal )/count 
            xr=(xtotal+ytotal)%count;yr=(ytotal-xtotal)%count 
            x=x0-j;y=y0+i 
            if xr>=count/2:x=x+1 
            if yr>=count/2:y=y+1 
            SourceList.append([i,j]);DestList.append([x,y]) 
            if x<0 or x>=HEIGHT or y<0 or y>=WIDTH:Moveable=FALSE 
            if x>=0 and x=0 and y=0 and BL[i][j-1]==1 and LL[i][j-1].isactive==PASSIVE:Moveable=FALSE 
      if Moveable==TRUE: 
        for i in range(HEIGHT): 
          for j in range(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) 
    def Right(self,event): 
      BL=self.BlockList;LL=self.LabelList 
      Moveable=TRUE 
      for i in range(HEIGHT): 
        for j in range(WIDTH): 
          if LL[i][j].isactive==ACTIVE and j+1>=WIDTH:Moveable=FALSE 
          if LL[i][j].isactive==ACTIVE and j+1=1000:self.time=900 
      if self.TotalScore>=2000:self.time=750 
      if self.TotalScore>=3000:self.time=600 
      if self.TotalScore>=4000:self.time=400 
      self.after(self.time,self.OnTimer) 
    def Down(self,event): 
      BL=self.BlockList;LL=self.LabelList 
      Moveable=TRUE 
      for i in range(HEIGHT): 
        for j in range(WIDTH): 
          if LL[i][j].isactive==ACTIVE and i+1>=HEIGHT:Moveable=FALSE 
          if LL[i][j].isactive==ACTIVE and i+1



) def NextFill(self,i,j): self.NextList[i][j].config(relief=RAISED,bg=str(self.frontg)) def NextEmpty(self,i,j): self.NextList[i][j].config(relief=FLAT,bg=str(self.nextg)) def Distroy(self): #save if self.TotalScore!=0: 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 in range(HEIGHT): for j in range(WIDTH): self.Empty(i,j) self.TotalLine=0;self.TotalScore=0;self.TotalTime=0.0 self.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) 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);self.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);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=TRUE def 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.NextFill(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);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.seek(0) strHeadLine=self.file.readline() dictLine={} strTotalLine=\'\' for OneLine in self.file.readlines(): temp=int(OneLine[:5]) dictLine[temp]=OneLine list=sorted(dictLine.items(),key=lambda d:d[0]) ii=0 for onerecord in reversed(list): ii=ii+1 if ii<11: strTotalLine+=onerecord[1] showinfo(\'Ranking\', strHeadLine+strTotalLine) def Start(): app.RndFirst();app.Start();app.Rnd() def End(): app.Distroy() def Set(): pass def Show(): app.Show() mainmenu=Menu(root) root[\'menu\']=mainmenu gamemenu=Menu(mainmenu) mainmenu.add_cascade(label=\'game\',menu=gamemenu) gamemenu.add_command(label=\'start\',command=Start) gamemenu.add_command(label=\'end\',command=End) 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()

相关内容

热门资讯

Mobi、epub格式电子书如... 在wps里全局设置里有一个文件关联,打开,勾选电子书文件选项就可以了。
定时清理删除C:\Progra... C:\Program Files (x86)下面很多scoped_dir开头的文件夹 写个批处理 定...
500 行 Python 代码... 语法分析器描述了一个句子的语法结构,用来帮助其他的应用进行推理。自然语言引入了很多意外的歧义,以我们...
scoped_dir32_70... 一台虚拟机C盘总是莫名奇妙的空间用完,导致很多软件没法再运行。经过仔细检查发现是C:\Program...
65536是2的几次方 计算2... 65536是2的16次方:65536=2⁶ 65536是256的2次方:65536=256 6553...
小程序支付时提示:appid和... [Q]小程序支付时提示:appid和mch_id不匹配 [A]小程序和微信支付没有进行关联,访问“小...
pycparser 是一个用... `pycparser` 是一个用 Python 编写的 C 语言解析器。它可以用来解析 C 代码并构...
微信小程序使用slider实现... 众所周知哈,微信小程序里面的音频播放是没有进度条的,但最近有个项目呢,客户要求音频要有进度条控制,所...
Apache Doris 2.... 亲爱的社区小伙伴们,我们很高兴地向大家宣布,Apache Doris 2.0.0 版本已于...
python清除字符串里非数字... 本文实例讲述了python清除字符串里非数字字符的方法。分享给大家供大家参考。具体如下: impor...