今天填一个坑,俄罗斯方块!!
俄罗斯方块的移动不难实现,但是老师给定了字典数据结构,旋转就不太容易实现,当然也可以用list嵌套list来做。今天把旋转做出来了,刚好整理一下,把之前的都整合在一起
说明一下,没有使用pygame,只是在控制台可以打印、移动和旋转生成的俄罗斯方块星号图形
俄罗斯方块-打印
功能:输入字母,打印俄罗斯方块的*图形
# 尽可能吧俄罗斯方块放在中间
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可以移动
# 文件名称: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
俄罗斯方块-旋转
主要思想:预定义4X4的坐标转换,旋转式查找字典中的转换坐标,进行相应的替换
功能:输入字母,打印俄罗斯方块的*图形,输入468可以移动,输入5可以旋转
废话不多说,直接给出代码:
#!/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 <<
昨晚熬到12点,求赞QAQ
如果这篇文章对你有帮助的话,点个赞呗