A tutorial on writing a simple Tetris game in Python _python

Source: Internet
Author: User
Tags flush in python

Tetris game, the use of Python implementation, a total of 350+ line of code, the realization of the basic functions of Tetris game, while will record the time spent, eliminate the total number of points, and also includes a list, you can view the highest record.

The list contains a series of statistical functions, such as the number of lines to eliminate the unit time, the unit time score.

Attached source code:


From tkinter Import * to tkmessagebox Import * Import random import time #俄罗斯方块界面的高度 HEIGHT = #俄罗斯 Square Interface width = Ten 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 (' <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.next g= "#%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 s Elf.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.back g), 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.F Ile.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.is Pause=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): to J in range (WID TH): If Ll[i][j].isactive==active:xtotal=xtotal+i;ytotal=ytotal+j;count=count+1 SourceList 
            =[];D estlist=[] for I in range (HEIGHT): to 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) I 
            F xr>=count/2:x=x+1 if Yr>=count/2:y=y+1 sourcelist.append ([i,j]);D estlist.append ([x,y]) If x<0 or X>=height or y<0 or Y>=width:moveable=false if x>=0 and X 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.