Python控制台输出俄罗斯方块的方法实例

时间:2022-05-30 11:11:35

今天填一个坑,俄罗斯方块!!

俄罗斯方块的移动不难实现,但是旋转就不太容易实现,究其原因是因为Python中没有数组这种数据结构,所以不能用矩阵的公式。今天把旋转做出来了,刚好整理一下,把之前的都整合在一起

俄罗斯方块-打印

功能:输入字母,打印俄罗斯方块的*图形

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 尽可能吧俄罗斯方块放在中间
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("请输入俄罗斯方块的形状")
    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()

俄罗斯方块-移动

这一版使用了函数+字典

功能:输入字母,打印俄罗斯方块的*图形,输入468可以移动

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 文件名称:Model-Tetris.PY
# 开发工具: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
?
1
2
3
4
5
6
7
8
9
10
11
12
13
# 文件名称:Test-Tetris.PY
# 开发工具:PyCharm  
from Day05 import Model_Tetris
if __name__ == '__main__':
    char = input("请输入图形:")
    Model_Tetris.L_char(char)
    while True:
        num = int(input("请输入移动代码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

俄罗斯方块-旋转

主要思想:预定义4X4的坐标转换,旋转式查找字典中的转换坐标,进行相应的替换

功能:输入字母,打印俄罗斯方块的*图形,输入468可以移动,输入5可以旋转

废话不多说,直接给出代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/usr/bin/python
# _*_coding:utf-8_*_
# 开发人员:zys
# 开发时间:2021/4/16 16:50
# 文件名称:Test-Tetris.PY
# 开发工具:PyCharm  
import random
 
from Day06.test1 import Model_Tetris
if __name__ == '__main__':
    # 随机生成方法
    # Character = "LOJSZIT"
    # char = random.choice(Character)
 
    char = "L"
    Model_Tetris.L_char(char)
    while True:
        num = int(input("请输入移动代码4/5/6/8:")) # 5是旋转
        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
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/python
# _*_coding:utf-8_*_
# 开发人员:zys
# 开发时间:2021/4/16 16:36
# 文件名称:Model-Tetris.PY
# 开发工具: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旋转
'''这里补充一个知识点:
    字典中的key是不可变的,所以不能用list作为字典的key,
    因为list可以修改,这里采用了元组作为key,指明不可改变,
    后面地址查值的时候需要进行类型转换。'''
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 = []  # 新的list
    for i in range(len(Tetris[char])):
        newTetris.append(rotateDict[tuple(Tetris[char][i])])   # 对应坐标添加到新的坐标
    Tetris[char] = newTetris

总结

到此这篇关于Python控制台输出俄罗斯方块的文章就介绍到这了,更多相关Python输出俄罗斯方块内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/to_upper/article/details/115750393