SoFunction
Updated on 2024-11-15

python implementation of tetris game (improved version)

This article shares a python implementation of the Tetris game, following thePrevious article.The improved version for your reference is as follows

1. Added square preview section

2. Added start button

In the company internship to take the time to write, huh. I think Python is pretty good, I used to feel like a toy language. I hope I can do more great things with it! Cheer up.

Screenshots are below:

The code is as follows:

#coding=utf-8
from Tkinter import *;
from random import *;
import thread;  
from tkMessageBox import showinfo;
import threading;
from time import sleep;
class BrickGame(object):
 
 # Does it start
 start = True;
 # Is it reaching the bottom
 isDown = True;
 
 #Forms
 window = None;
 #frame
 frame1 = None;
 frame2 = None;
 
 #buttons
 btnStart = None;
 
 # Drawing class
 canvas = None;
 canvas1 = None;
 
 # Title
 title = "BrickGame";
 #Width and height
 width = 450;
 height = 670;
 
 # Rows and columns
 rows = 20;
 cols = 10;
 
 #Falling Cubes thread
 downThread = None;
 
 # Several kinds of squares
 brick = [
  
  [
     [
       [1,1,1],
       [0,0,1],
       [0,0,0]
     ],
     [
       [0,0,1],
       [0,0,1],
       [0,1,1]
     ],
     [
       [0,0,0],
       [1,0,0],
       [1,1,1]
     ],
     [
       [1,1,0],
       [1,0,0],
       [1,0,0]
     ]
  ],
  [
     [
        [0,0,0],
        [0,1,1],
        [0,1,1]
     ],
     [
        [0,0,0],
        [0,1,1],
        [0,1,1]
     ],
     [
        [0,0,0],
        [0,1,1],
        [0,1,1]
     ],
     [
        [0,0,0],
        [0,1,1],
        [0,1,1]
     ]     
  ],
  [
     [
        [1,1,1],
        [0,1,0],
        [0,1,0]
     ],
     [
        [0,0,1],
        [1,1,1],
        [0,0,1]
     ],
     [
        [0,1,0],
        [0,1,0],
        [1,1,1]
     ],
     [
        [1,0,0],
        [1,1,1],
        [1,0,0]
     ]
  ],
  [
     [
        [0,1,0],
        [0,1,0],
        [0,1,0]
     ],
     [
        [0,0,0],
        [1,1,1],
        [0,0,0]
     ],
     [
        [0,1,0],
        [0,1,0],
        [0,1,0]
     ],
     [
        [0,0,0],
        [1,1,1],
        [0,0,0]
     ]
  ]
 ];
 
 # The current square
 curBrick = None;
 # The current array of squares
 arr = None;
 arr1 = None;
 # Current square shape
 shape = -1;
 # Rows and columns of the current square (top leftmost corner)
 curRow = -10;
 curCol = -10;
 
 # Background
 back = list();
 # Plaid
 gridBack = list();
 preBack = list();
 
 # Initialization
 def init(self):
  
  for i in range(0,):
   
   (i,list());
   (i,list());
  
  for i in range(0,):
   
   for j in range(0,):
    
    [i].insert(j,0);
    [i].insert(j,.create_rectangle(30*j,30*i,30*(j+1),30*(i+1),fill="black"));
    
  for i in range(0,3):
   
   (i,list());
   
  for i in range(0,3):
   
   for j in range(0,3):
    
    [i].insert(j,self.canvas1.create_rectangle(30*j,30*i,30*(j+1),30*(i+1),fill="black"));
 
 # Drawing the grid of the game
 def drawRect(self):
  
  for i in range(0,):
     
     for j in range(0,):
      
     
      if [i][j]==1:
       
       ([i][j],fill="blue",outline="white");
       
      elif [i][j]==0:
       
       ([i][j],fill="black",outline="white");
       
  #Drawing preview squares
  for i in range(0,len(self.arr1)):
   
   for j in range(0,len(self.arr1[i])):
    
    if self.arr1[i][j]==0:
     
     self.([i][j],fill="black",outline="white");
     
    elif self.arr1[i][j]==1:
     
     self.([i][j],fill="orange",outline="white");
 
       
  #Draw the square that is currently in motion
  if !=-10 and !=-10:
   
   for i in range(0,len()):
    
    for j in range(0,len([i])):
     
     if [i][j]==1:     
      
      ([+i][+j],fill="blue",outline="white");
      
  # Determine if the cube has moved to the bottom
  if :
   
   for i in range(0,3):
    
    for j in range(0,3):
     
     if [i][j]!=0:
      
      [+i][+j] = [i][j];
      
   #Judge whole line elimination
   ();
   
   #To determine if you're dead
   ();
    
   # Get the next square
   ();
 
 # Determine if there is an entire line that needs to be eliminated
 def removeRow(self):
  
  for i in range(0,):
 
   tag1 = True;   
   for j in range(0,):
    
    if [i][j]==0:
     
     tag1 = False;
     break;
   
   if tag1==True:
    
    # Move from top to bottom
    for m in xrange(i-1,0,-1):
     
     for n in range(0,):
      
      [m+1][n] = [m][n];
   
 #Get the current square
 def getCurBrick(self):
  
   = randint(0,len()-1);
   = 0;
  # The current array of squares
   = [][];
  self.arr1 = ;
  
   = 0;
   = 1;
  
  #False if it goes to the bottom
   = False;
  
 # Listen to keyboard input
 def onKeyboardEvent(self,event):
  
  #Not started, don't have to listen for keystrokes
  if  == False:
   
   return;
  
  # Record the original value
  tempCurCol = ;
  tempCurRow = ;
  tempShape = ;
  tempArr = ;
  direction = -1;
  
  if ==37:
   
   # Left shift
   -=1;
   direction = 1;
  elif ==38:
   #Vary the shape of the cube
   +=1;
   direction = 2;
   
   if >=4:
    
    =0;
    = [][];
  elif ==39:
   
   direction = 3;
   # Shift right
   +=1;
  elif ==40:
   
   direction = 4;
   #Downshift
   +=1;
   
  if (direction)==False:
   
    = tempCurCol;
    = tempCurRow;
    = tempShape;
    = tempArr;
    
  ();
   
  return True;
 
 # Determine if the current square has reached the border
 def isEdge(self,direction):
  
  tag = True;
 
  # To the left, judge the boundaries
  if direction==1:
   
   for i in range(0,3):
    
    for j in range(0,3):
     
     if [j][i]!=0 and (+i<0 or [+j][+i]!=0):
      
      tag = False;
      break;
  # To the right, judge the boundaries
  elif direction==3:
   
   for i in range(0,3):
    
    for j in range(0,3):
     
     if [j][i]!=0 and (+i>= or [+j][+i]!=0):
      
      tag = False;
      break;
  # Downward, judging the bottom
  elif direction==4:
   
   for i in range(0,3):
    
    for j in range(0,3):
     
     if [i][j]!=0 and (+i>= or [+i][+j]!=0):
      
      tag = False;
       = True;
      break;
  # Perform the deformation and determine the boundaries
  elif direction==2:
   
   if <0:
    
    =0;
   
   if +2>=:
    
     = -3;
    
   if +2>=:
    
     = -3;
  
  
  return tag;
 
 #Move the cube down.
 def brickDown(self):
  
  while True:
   
   if ==False:
    
    print("exit thread");
    break;
   
   tempRow = ;
   +=1;
   
   if (4)==False:
    
     = tempRow;
    
   ();
     
   # Down one square every second #
   sleep(1); 
   
 #Click to start
 def clickStart(self):
  
   = True;
  
  for i in range(0,):
   
   for j in range(0,):
    
    [i][j] = 0;
    ([i][j],fill="black",outline="white");
    
  for i in range(0,len()):
   
   for j in range(0,len([i])):
    
    self.([i][j],fill="black",outline="white");
    
  ();
  ();
  
   = (target=,args=());
  ();  
  
 #To determine if you're dead
 def isDead(self):
  
  for j in range(0,len([0])):
   
   if [0][j]!=0:
    
    showinfo("Tip.","You're hung over, let's have another round!");
     = False;
    break;
   
 # Run
 def __init__(self):
  
   = Tk();
  ();
  (,);
  (,);    
  
  self.frame1 = Frame(,width=300,height=600,bg="black");
  self.(x=20,y=30);
  
  self.frame2 = Frame(,width=90,height=90,bg="black");
  self.(x=340,y=60);
  
   = Canvas(self.frame1,width=300,height=600,bg="black");
  self.canvas1 = Canvas(self.frame2,width=90,height=90,bg="black");
  
   = Button(,text="Begin.",command=);
  (x=340,y=400,width=80,height=25);
  
  ();
  
  #Get the current square
  ();
  
  # Follow the array and draw the grid
  ();  
     
  ();
  self.();
  
  # Listen to keyboard events
  ("<KeyPress>",); 
  
  #Start the cube drop thread
   = (target=,args=());
  ();  
  
  (); 
  
  =False;
  
 pass;
 
if __name__=='__main__':
 
 brickGame = BrickGame();

More great Tetris articles on the topic:Tetris Games Collection Undertake a study.

This is the whole content of this article.