今天填一個(gè)坑,俄羅斯方塊!!
俄羅斯方塊的移動(dòng)不難實(shí)現(xiàn),但是旋轉(zhuǎn)就不太容易實(shí)現(xiàn),究其原因是因?yàn)镻ython中沒(méi)有數(shù)組這種數(shù)據(jù)結(jié)構(gòu),所以不能用矩陣的公式。今天把旋轉(zhuǎn)做出來(lái)了,剛好整理一下,把之前的都整合在一起
俄羅斯方塊-打印
功能:輸入字母,打印俄羅斯方塊的*圖形
# 盡可能吧俄羅斯方塊放在中間
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("請(qǐng)輸入俄羅斯方塊的形狀")
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()
俄羅斯方塊-移動(dòng)
這一版使用了函數(shù)+字典
功能:輸入字母,打印俄羅斯方塊的*圖形,輸入468可以移動(dòng)
# 文件名稱:Model-Tetris.PY
# 開(kāi)發(fā)工具: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
# 開(kāi)發(fā)工具:PyCharm
from Day05 import Model_Tetris
if __name__ == '__main__':
char = input("請(qǐng)輸入圖形:")
Model_Tetris.L_char(char)
while True:
num = int(input("請(qǐng)輸入移動(dòng)代碼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
俄羅斯方塊-旋轉(zhuǎn)
主要思想:預(yù)定義4X4的坐標(biāo)轉(zhuǎn)換,旋轉(zhuǎn)式查找字典中的轉(zhuǎn)換坐標(biāo),進(jìn)行相應(yīng)的替換
功能:輸入字母,打印俄羅斯方塊的*圖形,輸入468可以移動(dòng),輸入5可以旋轉(zhuǎn)
廢話不多說(shuō),直接給出代碼:
#!/usr/bin/python
# _*_coding:utf-8_*_
# 開(kāi)發(fā)人員:zys
# 開(kāi)發(fā)時(shí)間:2021/4/16 16:50
# 文件名稱:Test-Tetris.PY
# 開(kāi)發(fā)工具:PyCharm
import random
from Day06.test1 import Model_Tetris
if __name__ == '__main__':
# 隨機(jī)生成方法
# Character = "LOJSZIT"
# char = random.choice(Character)
char = "L"
Model_Tetris.L_char(char)
while True:
num = int(input("請(qǐng)輸入移動(dòng)代碼4/5/6/8:")) # 5是旋轉(zhuǎn)
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_*_
# 開(kāi)發(fā)人員:zys
# 開(kāi)發(fā)時(shí)間:2021/4/16 16:36
# 文件名稱:Model-Tetris.PY
# 開(kāi)發(fā)工具: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旋轉(zhuǎn)
'''這里補(bǔ)充一個(gè)知識(shí)點(diǎn):
字典中的key是不可變的,所以不能用list作為字典的key,
因?yàn)閘ist可以修改,這里采用了元組作為key,指明不可改變,
后面地址查值的時(shí)候需要進(jìn)行類型轉(zhuǎn)換。'''
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])]) # 對(duì)應(yīng)坐標(biāo)添加到新的坐標(biāo)
Tetris[char] = newTetris
總結(jié)
到此這篇關(guān)于Python控制臺(tái)輸出俄羅斯方塊的文章就介紹到這了,更多相關(guān)Python輸出俄羅斯方塊內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- python 基于pygame實(shí)現(xiàn)俄羅斯方塊
- Python控制臺(tái)輸出俄羅斯方塊移動(dòng)和旋轉(zhuǎn)功能
- Python Pygame實(shí)現(xiàn)俄羅斯方塊
- python如何寫個(gè)俄羅斯方塊
- python游戲?qū)崙?zhàn)項(xiàng)目之俄羅斯方塊的魅力