后台-插件-广告管理-内容页广告位一(手机)

# Python控制台输出俄罗斯方块移动和旋转

2021-04-18 18:40:33其他编程人已围观

## 俄罗斯方块-打印

``````# 尽可能吧俄罗斯方块放在中间
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()
``````

## 俄罗斯方块-移动

``````# 文件名称：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
``````
``````# 文件名称：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
``````

## 俄罗斯方块-旋转

``````#!/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

``````
``````#!/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每日笔记—Day01 <<
>> Python每日笔记—Day02 <<
>> Python每日笔记—Day03 <<
>> Python每日笔记—Day04 <<
>> Python每日笔记—Day05 <<
>> Python每日笔记—Day06 <<
>> Python每日笔记—Day07 <<
>> Python每日笔记—Day08 <<

Tags：python   数据结构   字典   列表   元组

 后台-插件-广告管理-内容页广告位二(手机)

## 相关文章

 后台-插件-广告管理-内容页广告位三(手机)
 后台-插件-广告管理-内容页广告位四(手机)

## 文章评论

 留言与评论（共有 0 条评论）

 验证码：

## 站点信息

• 文章统计13614篇文章
• 浏览统计468次浏览
• 评论统计1个评论
• 标签管理标签云
• 统计数据：统计代码
• 微信公众号：扫描二维码，关注我们