KRITSADA

The Best Tetris with microbit micropython

Oct 28th, 2019
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from microbit import *
  2. from random import choice
  3.  
  4. #Set up the tetris grid
  5. grid=[[1,0,0,0,0,0,1],[1,0,0,0,0,0,1],[1,0,0,0,0,0,1],[1,0,0,0,0,0,1],[1,0,0,0,0,0,1],[1,1,1,1,1,1,1]]
  6. #Store a list of 4 bricks, each brick is a 2x2 grid
  7. bricks = [[9,9],[9,0]],[[9,9],[0,9]],[[9,9],[9,9]],[[9,9],[0,0]]
  8. #select a brick randomly and position it at the center/top of the grid (y=0,x=3)
  9. brick = choice(bricks)
  10. x=3
  11. y=0
  12. frameCount=0
  13.  
  14. #A function to return the maximum of two values
  15. def max(a,b):
  16.     if a>=b:
  17.         return a
  18.     else:
  19.         return b
  20.        
  21. #A function to hide the 2x2 brick on the LED screen
  22. def hideBrick():
  23.     if x>0:
  24.         display.set_pixel(x-1,y,grid[y][x])
  25.     if x<5:
  26.         display.set_pixel(x+1-1,y,grid[y][x+1])
  27.     if x>0 and y<4:
  28.         display.set_pixel(x-1,y+1,grid[y+1][x])
  29.     if x<5 and y<4:
  30.         display.set_pixel(x+1-1,y+1,grid[y+1][x+1])
  31.        
  32. #A function to show the 2x2 brick on the LED screen
  33. def showBrick():
  34.     if x>0:
  35.         display.set_pixel(x-1,y,max(brick[0][0],grid[y][x]))
  36.     if x<5:
  37.         display.set_pixel(x+1-1,y,max(brick[0][1],grid[y][x+1]))
  38.     if x>0 and y<4:
  39.         display.set_pixel(x-1,y+1,max(brick[1][0],grid[y+1][x]))
  40.     if x<5 and y<4:  
  41.         display.set_pixel(x+1-1,y+1,max(brick[1][1],grid[y+1][x+1]))
  42.  
  43. #A function to rotate the 2x2 brick
  44. def rotateBrick():
  45.     pixel00 = brick[0][0]
  46.     pixel01 = brick[0][1]
  47.     pixel10 = brick[1][0]
  48.     pixel11 = brick[1][1]
  49.     #Check if the rotation is possible
  50.     if not ((grid[y][x]>0 and pixel00>0) or (grid[y+1][x]>0 and pixel10>0) or (grid[y][x+1]>0 and pixel01>0) or (grid[y+1][x+1]>0 and pixel11>0)):
  51.         hideBrick()        
  52.         brick[0][0] = pixel10
  53.         brick[1][0] = pixel11
  54.         brick[1][1] = pixel01
  55.         brick[0][1] = pixel00
  56.         showBrick()    
  57.  
  58. #A function to move/translate the brick
  59. def moveBrick(delta_x,delta_y):
  60.     global x,y
  61.     move=False
  62.     #Check if the move if possible: no collision with other blocks or borders of the grid
  63.     if delta_x==-1 and x>0:
  64.         if not ((grid[y][x-1]>0 and brick[0][0]>0) or (grid[y][x+1-1]>0 and brick[0][1]>0) or (grid[y+1][x-1]>0 and brick[1][0]>0) or (grid[y+1][x+1-1]>0 and brick[1][1]>0)):
  65.             move=True
  66.     elif delta_x==1 and x<5:
  67.         if not ((grid[y][x+1]>0 and brick[0][0]>0) or (grid[y][x+1+1]>0 and brick[0][1]>0) or (grid[y+1][x+1]>0 and brick[1][0]>0) or (grid[y+1][x+1+1]>0 and brick[1][1]>0)):
  68.             move=True
  69.     elif delta_y==1 and y<4:  
  70.         if not ((grid[y+1][x]>0 and brick[0][0]>0) or (grid[y+1][x+1]>0 and brick[0][1]>0) or (grid[y+1+1][x]>0 and brick[1][0]>0) or (grid[y+1+1][x+1]>0 and brick[1][1]>0)):
  71.             move=True
  72.     #If the move is possible, update x,y coordinates of the brick
  73.     if move:      
  74.         hideBrick()
  75.         x+=delta_x
  76.         y+=delta_y
  77.         showBrick()
  78.        
  79.     #Return True or False to confirm if the move took place
  80.     return move    
  81.  
  82. #A function to check for and remove completed lines
  83. def checkLines():
  84.     global score
  85.     removeLine=False
  86.     #check each line one at a time
  87.     for i in range(0, 5):
  88.         #If 5 blocks are filled in (9) then a line is complete (9*5=45)
  89.         if (grid[i][1]+grid[i][2]+grid[i][3]+grid[i][4]+grid[i][5])==45:
  90.             removeLine = True
  91.             #Increment the score (10 pts per line)
  92.             score+=10
  93.             #Remove the line and make all lines above fall by 1:
  94.             for j in range(i,0,-1):
  95.                 grid[j] = grid[j-1]
  96.             grid[0]=[1,0,0,0,0,0,1]    
  97.     if removeLine:
  98.         #Refresh the LED screen
  99.         for i in range(0, 5):
  100.             for j in range(0, 5):
  101.                 display.set_pixel(i,j,grid[j][i+1])
  102.     return removeLine
  103.    
  104. gameOn=True
  105. score=0
  106. showBrick()
  107. #Main Program Loop - iterates every 50ms
  108. while gameOn:
  109.     sleep(50)
  110.     frameCount+=1
  111.     #Capture user inputs
  112.     if button_a.is_pressed() and button_b.is_pressed():
  113.         rotateBrick()
  114.     elif button_a.is_pressed():
  115.         moveBrick(-1,0)
  116.     elif button_b.is_pressed():
  117.         moveBrick(1,0)
  118.    
  119.     #Every 15 frames try to move the brick down
  120.     if frameCount==15 and moveBrick(0,1) == False:
  121.         frameCount=0
  122.         #The move was not possible, the brick stays in position
  123.         grid[y][x]=max(brick[0][0],grid[y][x])
  124.         grid[y][x+1]=max(brick[0][1],grid[y][x+1])
  125.         grid[y+1][x]=max(brick[1][0],grid[y+1][x])
  126.         grid[y+1][x+1]=max(brick[1][1],grid[y+1][x+1])
  127.        
  128.         if checkLines()==False and y==0:
  129.             #The brick has reached the top of the grid - Game Over
  130.             gameOn=False  
  131.         else:
  132.             #select a new brick randomly
  133.             x=3
  134.             y=0
  135.             brick = choice(bricks)
  136.             showBrick()
  137.    
  138.     if frameCount==15:
  139.        frameCount=0
  140.  
  141. #End of Game
  142. sleep(2000)
  143. display.scroll("Game Over: Score: " + str(score))
Add Comment
Please, Sign In to add comment