Advertisement
here2share

# Tk_Tetris.py

Jul 30th, 2016
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.77 KB | None | 0 0
  1. # Tk_Tetris.py
  2. from Tkinter import *
  3. import random
  4.  
  5. delay = True
  6.  
  7. def init():
  8.     iPiece = [
  9.     [ True,  True,  True,  True]]
  10.     jPiece = [
  11.     [ True, False, False ],
  12.     [ True, True,  True]]
  13.     lPiece = [
  14.     [ False, False, True],
  15.     [ True,  True,  True]]
  16.     oPiece = [
  17.     [ True, True],
  18.     [ True, True]]
  19.     sPiece = [
  20.     [ False, True, True],
  21.     [ True,  True, False ]]
  22.     tPiece = [
  23.     [ False, True, False ],
  24.     [ True,  True, True]]
  25.     zPiece = [
  26.     [ True,  True, False ],
  27.     [ False, True, True]]
  28.     tetrisPieces = [ iPiece, jPiece, lPiece, oPiece, sPiece, tPiece, zPiece ]
  29.     tetrisPieceColors = [ "red", "yellow", "purple", "violet", "cyan", "green", "orange" ]
  30.     canvas.data.tetrisPieces = tetrisPieces
  31.     canvas.data.tetrisPieceColors = tetrisPieceColors
  32.  
  33. root = Tk()
  34. canvas = Canvas(root, width = 500, height = 700)
  35. class Struct: pass
  36. canvas.data = Struct()
  37. canvas.pack()
  38. root.resizable(width = 0, height = 0)
  39. init()
  40.  
  41. def run(rows=14, cols=10):
  42.     canvas.data.score = 0
  43.     canvas.data.isGameOver = False
  44.     board = []
  45.     for r in range(rows):
  46.         blueAcross = []
  47.         for c in range(cols):
  48.             blueAcross.append("blue")
  49.         board.append(blueAcross)
  50.     canvas.data.board = board
  51.     newFallingPiece()
  52.     if delay == True:
  53.         timerFired()
  54.     root.mainloop()
  55.  
  56. def redrawAll(rows=14, cols=10):
  57.     canvas.delete(ALL)
  58.     drawGame(rows, cols)
  59.     drawScore()
  60.  
  61. def drawGame(rows, cols):
  62.     canvas.create_rectangle(0, 0, 500, 700, fill = "yellow")
  63.     drawBoard(rows, cols)
  64.     removeFullRows()
  65.  
  66. def drawScore():
  67.     canvas.create_text(300, 25, text = canvas.data.score, fill = "black", font = "Times 25 italic" )
  68.  
  69. def drawBoard(rows, cols):
  70.     if canvas.data.isGameOver == False:
  71.         for r in range(rows):
  72.             for c in range(cols):
  73.                 drawCell(r, c, canvas.data.board[r][c])
  74.     else:
  75.         canvas.create_rectangle(0, 0, 1000, 1000, fill = "yellow")
  76.         canvas.create_text(250, 100, fill = "red", text = "GAME OVER! Press 'r' to\nstart another game!", font = "Times 30 italic")
  77.  
  78. def drawCell(row, col, color):
  79.     Ystart = 45
  80.     Xstart = 45
  81.     Xwidth = 45
  82.     Ywidth = 45
  83.     XPOS = row*Xwidth
  84.     YPOS = col*Ywidth
  85.     canvas.create_rectangle((YPOS + Ystart), (XPOS + Xstart), (YPOS + Ystart + Ywidth), (XPOS + Xstart + Xwidth), fill = "black")
  86.     canvas.create_rectangle((YPOS + Ystart + 2), (XPOS + Xstart + 2), (YPOS + Ywidth + Ystart - 4), (XPOS + Xwidth + Xstart - 4), fill = color)
  87.  
  88. def newFallingPiece():
  89.     indexP = random.randint(0,6)
  90.     canvas.data.fallingPiece = canvas.data.tetrisPieces[indexP]
  91.     canvas.data.fallingColor = canvas.data.tetrisPieceColors[indexP]
  92.     canvas.data.fallingPieceRow = 0
  93.     canvas.data.fallingPieceCol = 4
  94.     drawFallingPiece()
  95.  
  96. def drawFallingPiece():
  97.     redrawAll()
  98.     if canvas.data.isGameOver == False:
  99.         fp = canvas.data.fallingPiece
  100.         fpcol = canvas.data.fallingColor
  101.         for row in range(len(fp)):
  102.             for col in range(len(fp[0])):
  103.                 if fp[row][col] == True:
  104.                     drawCell((row + canvas.data.fallingPieceRow), (col + canvas.data.fallingPieceCol), fpcol)
  105. def moveFallingPiece(drow,dcol):
  106.     canvas.data.fallingPieceRow += drow
  107.     canvas.data.fallingPieceCol += dcol
  108.     if fallingPieceIsLegal() == False:
  109.         canvas.data.fallingPieceRow -= drow
  110.         canvas.data.fallingPieceCol -= dcol
  111.         drawFallingPiece()
  112.         return False
  113.     drawFallingPiece()
  114.     return True
  115.  
  116. def rotateFallingPiece():
  117.     piece = canvas.data.fallingPiece
  118.     tempX = canvas.data.fallingPieceRow
  119.     tempY = canvas.data.fallingPieceCol
  120.     (initXC, initYC) = fallingPieceCenter(len(piece), len(piece[0]))
  121.     newTempW = len(piece[0])
  122.     newTempH = len(piece)
  123.     (finalXC, finalYC) = fallingPieceCenter(newTempW, newTempH)
  124.     tempX = tempX + (initXC - finalXC)
  125.     tempY = tempY + (initYC - finalYC)
  126.     newPiece = []
  127.     for row in range(newTempW):
  128.         p = []
  129.         for col in range(newTempH):
  130.             p.append("x")
  131.         newPiece.append(p)
  132.     for r in reversed(range(len(canvas.data.fallingPiece))):
  133.         for c in range(len(canvas.data.fallingPiece[0])):
  134.             newPiece[c][(len(canvas.data.fallingPiece) - 1) - r] = canvas.data.fallingPiece[r][c]
  135.     if fallingPieceIsLegal(newPiece, tempX, tempY):
  136.         canvas.data.fallingPieceRow = tempX
  137.         canvas.data.fallingPieceCol = tempY
  138.         canvas.data.fallingPiece = newPiece
  139.         drawFallingPiece()
  140.  
  141.  
  142. def fallingPieceCenter(width,height):
  143.     cW = canvas.data.fallingPieceRow + width/2
  144.     cH = canvas.data.fallingPieceCol + height/2
  145.     return (cW,cH)
  146.  
  147. def timerFired():
  148.     if canvas.data.isGameOver == False:
  149.         if (moveFallingPiece(1,0) == False):
  150.             if canvas.data.fallingPieceRow == 0:
  151.                 canvas.data.isGameOver = True
  152.             placeFallingPiece()
  153.             newFallingPiece()
  154.         delay = 700
  155.         canvas.after(delay, timerFired)
  156.  
  157. def placeFallingPiece():
  158.     #change BOARD values to color values of piece
  159.     for row in range(len(canvas.data.fallingPiece)):
  160.         for col in range(len(canvas.data.fallingPiece[0])):
  161.             if canvas.data.fallingPiece[row][col] == True:
  162.                 realX = row + canvas.data.fallingPieceRow
  163.                 realY = col + canvas.data.fallingPieceCol
  164.                 canvas.data.board[realX][realY] = canvas.data.fallingColor
  165.  
  166. def fallingPieceIsLegal(piece = [""], rowO = -5, colO = -5):
  167.     if rowO == -5:
  168.         rowO = canvas.data.fallingPieceRow
  169.     if colO == -5:
  170.         colO = canvas.data.fallingPieceCol
  171.     if piece[0] == "":
  172.         piece = canvas.data.fallingPiece
  173.     for row in range(len(piece)):
  174.         for col in range(len(piece[0])):
  175.             try:
  176.                 if ((piece[row][col] == True) & (canvas.data.board[row + rowO][col + colO] != "blue")):
  177.                     return False
  178.                 if (row + rowO) < 0:
  179.                     return False
  180.                 if (col + colO) < 0:
  181.                     return False
  182.             except:
  183.                 return False
  184.     return True
  185.  
  186. def keyPressed(event):
  187.     if event.keysym == "Right":
  188.             moveFallingPiece(0,1)
  189.     if event.keysym == "Left":
  190.             moveFallingPiece(0,-1)
  191.     if event.keysym == "Down":
  192.             moveFallingPiece(1,0)
  193.     if event.keysym == "Up":
  194.             rotateFallingPiece()
  195.     if (event.keysym == 'r'):
  196.         delay = False
  197.         canvas.data.isGameOver = False
  198.         run()
  199.     if (event.keysym == 'l'):
  200.         global root
  201.         canvas.data.isGameOver = True
  202.     if event.keysym == "space":
  203.             for x in reversed(range(len(canvas.data.board))):
  204.                 if moveFallingPiece(x,0):
  205.                     break
  206.     else:
  207.         drawFallingPiece()
  208. root.bind('<Key>', keyPressed)
  209.  
  210. scorebonus=['>>>',10,30,120,600]
  211. def removeFullRows():
  212.     oldRow = len(canvas.data.board)
  213.     counter = oldRow
  214.     while counter > 0:
  215.         lastFilled = True
  216.         rowsToRem = 0
  217.         while lastFilled == True:
  218.             for row in reversed(range(counter)):
  219.                 filled = True
  220.                 for col in range(len(canvas.data.board[row])):
  221.                     if canvas.data.board[row][col] == "blue":
  222.                         filled = False
  223.                         lastFilled = False
  224.                 if ((filled == True) & (lastFilled == True)):
  225.                     rowsToRem += 1
  226.             if rowsToRem > 0:
  227.                 canvas.data.score += scorebonus[rowsToRem]
  228.                 for row in range((counter - rowsToRem), counter):
  229.                     del canvas.data.board[row]
  230.                     newRow = []
  231.                     for col in canvas.data.board[0]:
  232.                         newRow.append("blue")
  233.                     canvas.data.board.insert(0, newRow)
  234.         counter -= 1
  235.            
  236. run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement