Python console output Tetris movement and rotation function

  • 2021-11-01 03:37:46
  • OfStack

Fill a pit today, Tetris! !
The movement of Tetris is not difficult to realize, but the rotation is not easy to realize. The reason is that there is no data structure such as array in Python, so the formula of matrix cannot be used. Today, I made the rotation, just sorted it out for 1 time, and integrated all the previous ones into 1 time

The number of views in these two days is a bit outrageous. Under Description 1, I didn't use pygame, but I can print, move and rotate the generated Tetris asterisk graphics on the console

Tetris-Print

Function: Enter letters and print * graphics of Tetris


#  Put Tetris in the middle as much as possible 
Tetris = {'L': [[1, 1], [1, 2], [1, 3], [2, 3]],
         'O': [[1, 1], [2, 1], [1, 2], [2, 2]],
         'J': [[2, 1], [2, 2], [1, 3], [2, 3]],
         'Z': [[1, 1], [2, 1], [2, 2], [3, 2]],
         'S': [[1, 1], [1, 2], [2, 2], [2, 3]],
         'I': [[1, 1], [1, 2], [1, 3], [1, 4]],
         'T': [[1, 1], [2, 1], [2, 2], [3, 1]]}
print(Tetris)

while True:
    figure = input(" Please enter the shape of Tetris ")
    flag = 0
    for i in range(0, 5):
        for j in range(0, 4):
            for li in range(0, len(Tetris[figure])):    # Tetris['L']
                if j == Tetris[figure][li][0] and i == Tetris[figure][li][1]:
                    flag = 1
            if flag == 1:
                print("*", end='')
            else:
                print(" ", end='')
            flag = 0
        print()

Tetris-Move

This version 1 uses functions + dictionaries
Function: Enter letters, print * graphics of Tetris, and enter 468 to move


#  File name: Model-Tetris.PY
#  Development Tools: PyCharm

Tetris = {'L': [[1, 1], [1, 2], [1, 3], [2, 3]],
         'O': [[1, 1], [2, 1], [1, 2], [2, 2]],
         'J': [[2, 1], [2, 2], [1, 3], [2, 3]],
         'Z': [[1, 1], [2, 1], [2, 2], [3, 2]],
         'S': [[1, 1], [1, 2], [2, 2], [2, 3]],
         'I': [[1, 1], [1, 2], [1, 3], [1, 4]],
         'T': [[1, 1], [2, 1], [2, 2], [3, 1]]}

def L_char(char):
    flag = 0
    for i in range(0, 7):
        for j in range(0, 7):
            for li in range(0, len(Tetris[char])):  # Tetris['L']
                if j == Tetris[char][li][0] and i == Tetris[char][li][1]:
                    flag = 1
            if flag == 1:
                print("*", end='')
            else:
                print(" ", end='')
            flag = 0
        print()

def move(char, num):
    if num == 2:
        for li in range(0, len(Tetris[char])):
            Tetris[char][li][1] += 1
    if num == 4:
        for li in range(0, len(Tetris[char])):
            Tetris[char][li][0] -= 1
    if num == 6:
        for li in range(0, len(Tetris[char])):
            Tetris[char][li][0] += 1
    if num == 8:
        for li in range(0, len(Tetris[char])):
            Tetris[char][li][1] -= 1

#  File name: Test-Tetris.PY
#  Development Tools: PyCharm   
from Day05 import Model_Tetris
if __name__ == '__main__':
    char = input(" Please enter a graphic: ")
    Model_Tetris.L_char(char)
    while True:
        num = int(input(" Please enter the moving code 2/4/6/8:"))
        if num == 8 or num == 2 or num == 4 or num == 6:
            Model_Tetris.move(char, num)
            Model_Tetris.L_char(char)
        else:
            break

Tetris-Rotation

Main idea: Predefined 4X4 coordinate transformation, rotary search the transformation coordinates in the dictionary, and carry out corresponding replacement
Functions: Enter letters, print * graphics of Tetris, input 468 can be moved, and input 5 can be rotated
Needless to say, give the code directly:


#!/usr/bin/python
# _*_coding:utf-8_*_
#  Developer: zys
#  Development time: 2021/4/16 16:50 
#  File name: Test-Tetris.PY
#  Development Tools: PyCharm   
import random

from Day06.test1 import Model_Tetris
if __name__ == '__main__':
    #  Random generation method 
    # Character = "LOJSZIT"
    # char = random.choice(Character)

    char = "L"
    Model_Tetris.L_char(char)
    while True:
        num = int(input(" Please enter the moving code 4/5/6/8:")) # 5 It's a spin 
        if num == 8 or num == 4 or num == 6:
            Model_Tetris.move(char, num)
            Model_Tetris.L_char(char)
        elif num == 5:
            Model_Tetris.revolve(char)
            Model_Tetris.L_char(char)
        else:
            break

#!/usr/bin/python
# _*_coding:utf-8_*_
#  Developer: zys
#  Development time: 2021/4/16 16:36 
#  File name: Model-Tetris.PY
#  Development Tools: PyCharm

Tetris = {'L': [[1, 1], [1, 2], [1, 3], [2, 3]],
         'O': [[1, 1], [1, 2], [2, 1], [2, 2]],
         'J': [[1, 2], [2, 2], [3, 1], [3, 2]],
         'Z': [[1, 1], [1, 2], [2, 2], [2, 3]],
         'S': [[1, 1], [2, 1], [2, 2], [3, 2]],
         'I': [[1, 1], [2, 1], [3, 1], [4, 1]],
         'T': [[1, 1], [1, 2], [2, 2], [1, 3]]}
# 4*4 Rotate 
''' Add here 1 Knowledge points: 
     In a dictionary key Is immutable, so you can't use it list As a dictionary key , 
     Because list It can be modified. Here, tuples are used as key , indicating that it is immutable, 
     Later, when the address is checked, type conversion is needed. '''
rotateDict = {(1, 1): [4, 1], (1, 2): [3, 1], (1, 3): [2, 1], (1, 4): [1, 1],
              (2, 1): [4, 2], (3, 1): [4, 3], (4, 1): [4, 4], (4, 2): [3, 4],
              (4, 3): [2, 4], (4, 4): [1, 4], (3, 4): [1, 3], (2, 4): [1, 2],
              (2, 2): [3, 2], (3, 2): [3, 3], (3, 3): [2, 3], (2, 3): [2, 2]}

def L_char(char):
    flag = 0
    for i in range(0, 5):
        for j in range(0, 5):
            for li in range(0, len(Tetris[char])):  # Tetris['L']
                if j == Tetris[char][li][0] and i == Tetris[char][li][1]:
                    flag = 1
            if flag == 1:
                print("*", end='')
            else:
                print(" ", end='')
            flag = 0
        print()

def move(char, num):
    if num == 2:
        for li in range(0, len(Tetris[char])):
            Tetris[char][li][1] += 1
    if num == 4:
        for li in range(0, len(Tetris[char])):
            Tetris[char][li][0] -= 1
    if num == 6:
        for li in range(0, len(Tetris[char])):
            Tetris[char][li][0] += 1
    if num == 8:
        for li in range(0, len(Tetris[char])):
            Tetris[char][li][1] -= 1


def revolve(char):
    newTetris = []  #  New list
    for i in range(len(Tetris[char])):
        newTetris.append(rotateDict[tuple(Tetris[char][i])])   #  Corresponding coordinates are added to the new coordinates 
    Tetris[char] = newTetris

Related articles: