Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_Tetris.py
- from Tkinter import *
- import random
- delay = True
- def init():
- iPiece = [
- [ True, True, True, True]]
- jPiece = [
- [ True, False, False ],
- [ True, True, True]]
- lPiece = [
- [ False, False, True],
- [ True, True, True]]
- oPiece = [
- [ True, True],
- [ True, True]]
- sPiece = [
- [ False, True, True],
- [ True, True, False ]]
- tPiece = [
- [ False, True, False ],
- [ True, True, True]]
- zPiece = [
- [ True, True, False ],
- [ False, True, True]]
- tetrisPieces = [ iPiece, jPiece, lPiece, oPiece, sPiece, tPiece, zPiece ]
- tetrisPieceColors = [ "red", "yellow", "purple", "violet", "cyan", "green", "orange" ]
- canvas.data.tetrisPieces = tetrisPieces
- canvas.data.tetrisPieceColors = tetrisPieceColors
- root = Tk()
- canvas = Canvas(root, width = 500, height = 700)
- class Struct: pass
- canvas.data = Struct()
- canvas.pack()
- root.resizable(width = 0, height = 0)
- init()
- def run(rows=14, cols=10):
- canvas.data.score = 0
- canvas.data.isGameOver = False
- board = []
- for r in range(rows):
- blueAcross = []
- for c in range(cols):
- blueAcross.append("blue")
- board.append(blueAcross)
- canvas.data.board = board
- newFallingPiece()
- if delay == True:
- timerFired()
- root.mainloop()
- def redrawAll(rows=14, cols=10):
- canvas.delete(ALL)
- drawGame(rows, cols)
- drawScore()
- def drawGame(rows, cols):
- canvas.create_rectangle(0, 0, 500, 700, fill = "yellow")
- drawBoard(rows, cols)
- removeFullRows()
- def drawScore():
- canvas.create_text(300, 25, text = canvas.data.score, fill = "black", font = "Times 25 italic" )
- def drawBoard(rows, cols):
- if canvas.data.isGameOver == False:
- for r in range(rows):
- for c in range(cols):
- drawCell(r, c, canvas.data.board[r][c])
- else:
- canvas.create_rectangle(0, 0, 1000, 1000, fill = "yellow")
- canvas.create_text(250, 100, fill = "red", text = "GAME OVER! Press 'r' to\nstart another game!", font = "Times 30 italic")
- def drawCell(row, col, color):
- Ystart = 45
- Xstart = 45
- Xwidth = 45
- Ywidth = 45
- XPOS = row*Xwidth
- YPOS = col*Ywidth
- canvas.create_rectangle((YPOS + Ystart), (XPOS + Xstart), (YPOS + Ystart + Ywidth), (XPOS + Xstart + Xwidth), fill = "black")
- canvas.create_rectangle((YPOS + Ystart + 2), (XPOS + Xstart + 2), (YPOS + Ywidth + Ystart - 4), (XPOS + Xwidth + Xstart - 4), fill = color)
- def newFallingPiece():
- indexP = random.randint(0,6)
- canvas.data.fallingPiece = canvas.data.tetrisPieces[indexP]
- canvas.data.fallingColor = canvas.data.tetrisPieceColors[indexP]
- canvas.data.fallingPieceRow = 0
- canvas.data.fallingPieceCol = 4
- drawFallingPiece()
- def drawFallingPiece():
- redrawAll()
- if canvas.data.isGameOver == False:
- fp = canvas.data.fallingPiece
- fpcol = canvas.data.fallingColor
- for row in range(len(fp)):
- for col in range(len(fp[0])):
- if fp[row][col] == True:
- drawCell((row + canvas.data.fallingPieceRow), (col + canvas.data.fallingPieceCol), fpcol)
- def moveFallingPiece(drow,dcol):
- canvas.data.fallingPieceRow += drow
- canvas.data.fallingPieceCol += dcol
- if fallingPieceIsLegal() == False:
- canvas.data.fallingPieceRow -= drow
- canvas.data.fallingPieceCol -= dcol
- drawFallingPiece()
- return False
- drawFallingPiece()
- return True
- def rotateFallingPiece():
- piece = canvas.data.fallingPiece
- tempX = canvas.data.fallingPieceRow
- tempY = canvas.data.fallingPieceCol
- (initXC, initYC) = fallingPieceCenter(len(piece), len(piece[0]))
- newTempW = len(piece[0])
- newTempH = len(piece)
- (finalXC, finalYC) = fallingPieceCenter(newTempW, newTempH)
- tempX = tempX + (initXC - finalXC)
- tempY = tempY + (initYC - finalYC)
- newPiece = []
- for row in range(newTempW):
- p = []
- for col in range(newTempH):
- p.append("x")
- newPiece.append(p)
- for r in reversed(range(len(canvas.data.fallingPiece))):
- for c in range(len(canvas.data.fallingPiece[0])):
- newPiece[c][(len(canvas.data.fallingPiece) - 1) - r] = canvas.data.fallingPiece[r][c]
- if fallingPieceIsLegal(newPiece, tempX, tempY):
- canvas.data.fallingPieceRow = tempX
- canvas.data.fallingPieceCol = tempY
- canvas.data.fallingPiece = newPiece
- drawFallingPiece()
- def fallingPieceCenter(width,height):
- cW = canvas.data.fallingPieceRow + width/2
- cH = canvas.data.fallingPieceCol + height/2
- return (cW,cH)
- def timerFired():
- if canvas.data.isGameOver == False:
- if (moveFallingPiece(1,0) == False):
- if canvas.data.fallingPieceRow == 0:
- canvas.data.isGameOver = True
- placeFallingPiece()
- newFallingPiece()
- delay = 700
- canvas.after(delay, timerFired)
- def placeFallingPiece():
- #change BOARD values to color values of piece
- for row in range(len(canvas.data.fallingPiece)):
- for col in range(len(canvas.data.fallingPiece[0])):
- if canvas.data.fallingPiece[row][col] == True:
- realX = row + canvas.data.fallingPieceRow
- realY = col + canvas.data.fallingPieceCol
- canvas.data.board[realX][realY] = canvas.data.fallingColor
- def fallingPieceIsLegal(piece = [""], rowO = -5, colO = -5):
- if rowO == -5:
- rowO = canvas.data.fallingPieceRow
- if colO == -5:
- colO = canvas.data.fallingPieceCol
- if piece[0] == "":
- piece = canvas.data.fallingPiece
- for row in range(len(piece)):
- for col in range(len(piece[0])):
- try:
- if ((piece[row][col] == True) & (canvas.data.board[row + rowO][col + colO] != "blue")):
- return False
- if (row + rowO) < 0:
- return False
- if (col + colO) < 0:
- return False
- except:
- return False
- return True
- def keyPressed(event):
- if event.keysym == "Right":
- moveFallingPiece(0,1)
- if event.keysym == "Left":
- moveFallingPiece(0,-1)
- if event.keysym == "Down":
- moveFallingPiece(1,0)
- if event.keysym == "Up":
- rotateFallingPiece()
- if (event.keysym == 'r'):
- delay = False
- canvas.data.isGameOver = False
- run()
- if (event.keysym == 'l'):
- global root
- canvas.data.isGameOver = True
- if event.keysym == "space":
- for x in reversed(range(len(canvas.data.board))):
- if moveFallingPiece(x,0):
- break
- else:
- drawFallingPiece()
- root.bind('<Key>', keyPressed)
- scorebonus=['>>>',10,30,120,600]
- def removeFullRows():
- oldRow = len(canvas.data.board)
- counter = oldRow
- while counter > 0:
- lastFilled = True
- rowsToRem = 0
- while lastFilled == True:
- for row in reversed(range(counter)):
- filled = True
- for col in range(len(canvas.data.board[row])):
- if canvas.data.board[row][col] == "blue":
- filled = False
- lastFilled = False
- if ((filled == True) & (lastFilled == True)):
- rowsToRem += 1
- if rowsToRem > 0:
- canvas.data.score += scorebonus[rowsToRem]
- for row in range((counter - rowsToRem), counter):
- del canvas.data.board[row]
- newRow = []
- for col in canvas.data.board[0]:
- newRow.append("blue")
- canvas.data.board.insert(0, newRow)
- counter -= 1
- run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement