Compile a simple Russian square game tutorial in Python.
The Tetris game is implemented in Python with a total of more than 350 lines of code. It implements the basic functions of the Tetris game and records the time spent, the total number of deleted rows, and the total score, it also includes a ranking table to view the highest record.
A ranking includes a series of statistical functions, such as the number of rows deleted per unit time and the unit time score.
Source code:
From Tkinter import * from tkMessageBox import * import random import time # HEIGHT of the Russian square interface = 18 # WIDTH of the Russian square interface = 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 ('<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, t Ext = '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, c Olumnspan = 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 sel F. 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. labelRowLi St = [] # file fw1_open('text.txt ', 'A') fw. close () hasHead = FALSE f=open('text.txt ', 'R') if f. read (5) = 'score ': hasHead = TRUE f. close () self.file1_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 de F 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 <HEIGHT and y> = 0 and y <WIDTH and BL [x] [y] = 1 and LL [x] [y]. isactive = PASSIVE: Moveable = FALSE if Moveable = TRUE: for I in range (len (SourceList): self. empty (SourceList [I] [0], SourceList [I] [1]) for I in range (len (DestList): self. fill (DestList [I] [0], DestList [I] [1]) def Left (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 <0: Moveable = FALSE if LL [I] [j]. isactive = ACTIVE and J-1> = 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 <WIDTH and BL [I] [j + 1] = 1 and LL [I] [j + 1]. isactive = PASSIVE: Moveable = FALSE if Moveable = TRUE: I in range (HEIGHT-1,-1,-1): for j in range (WIDTH-1,-1,-1 ): if j + 1 <WIDTH and LL [I] [j]. isactive = ACTIVE and BL [I] [j + 1] = 0: self. fill (I, j + 1); self. empty (I, j) def Space (self, event): while 1: if self. down (0) = FALSE: break def OnTimer (self): if self. isStart = TRUE and self. isPause = FALSE: self. totalTime = self. totalTime + float (self. time)/1000 self. spendTime. config (text = str (self. totalTime) if self. isPaus E = 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 = 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 <HEIGHT and BL [I + 1] [j] = 1 and LL [I + 1] [j]. isactive = PASSIVE: Moveable = FALSE if Moveable = TRUE: for I in range (HEIGHT-1,-1,-1): for j in range (WIDTH-1, -1,-1): if I + 1 <HEIGHT and LL [I] [j]. isactive = ACTIVE and BL [I + 1] [j] = 0: self. fill (I + 1, j); self. empty (I, j) if Moveable = FALSE: for I in range (HEIGHT): for j in range (WIDTH): LL [I] [j]. isactive = PASSIVE self. judgeLineFill () self. Start () if self. isgameover = TRUE: showinfo ('t _ t', 'the game is over! '); Self. distroy (); return FALSE for I in range (4): for j in range (4): self. nextEmpty (I, j) self. rnd () return Moveable def JudgeLineFill (self): BL = self. blockList; LL = self. labelList count = 0; LineList = [] for I in range (WIDTH): LineList. append (1) # display flash for I in range (HEIGHT): if BL [I] = LineList: count = count + 1 for k in range (WIDTH ): LL [I] [k]. config (bg = str (self. flashg) LL [I] [k]. update () if count! = 0: self. after (100) # delete block for I in range (HEIGHT): if BL [I] = LineList: # count = count + 1 for j in range (I, 0, -1): for k in range (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 in range (WIDTH): BL [0] [l] = 0 LL [0] [l]. config (relief = FLAT, bg = str (self. backg) self. totalLine = self. totalLine + count if count = 1: self. totalScore = self. totalScor E + 1 * WIDTH if count = 2: self. totalScore = self. totalScore + 3 * WIDTH if count = 3: self. totalScore = self. totalScore + 6 * WIDTH if count = 4: self. totalScore = self. totalScore + 10 * WIDTH self. line. config (text = str (self. totalLine) self. score. config (text = str (self. totalScore) def Fill (self, I, j): if j <0: return if self. blockList [I] [j] = 1: self. isgameover = TRUE self. blockList [I] [j] = 1 self. labelList [I] [j]. isactive = ACTIVE sel F. labelList [I] [j]. config (relief = RAISED, bg = str (self. frontg) def Empty (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) def Play (self, event): showinfo ('Made in China ', '^ _ </font> </p> <span mce_name = "em" style = "font-style: italic; "class =" Apple-style-span "mce_style =" font-style: italic; "> <span style =" font-size: smal L; "id =" "mce_style =" font-size: small; "> <br> </span> </p> <span mce_name =" em "style =" font-style: italic; "class =" Apple-style-span "mce_style =" font-style: italic; "> <span style =" font-size: small; "id =" "mce_style =" font-size: small; "> </span> </p> <br> </p>) 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 ()