if __name__ == "__main__":while not quit:if switch:held_piece, current_piece = current_piece, held_pieceswitch = Falseelse:current_piece = next_piecenext_piece = choice(["I", "T", "L", "J", "Z", "S", "O"])· if flag > 0:flag -= 1 #確定方塊的顏色和位置current_piece,next_piece以及held_pieceif held_piece == "":held_info = np.array([[0, 0]]), [0, 0, 0]else:held_info = get_info(held_piece)next_info = get_info(next_piece)coords, color = get_info(current_piece)if current_piece == "I":top_left = [-2, 3]#這個(gè)if語句只是檢查游戲是否需要終止if not np.all(board[coords[:,0], coords[:,1]] == 0):break 接下來我們 在主程序里面設(shè)置while true死循環(huán)用來我們一直檢測鍵盤輸入,直到游戲結(jié)束
首先,我們使用display()功能顯示板子并接收鍵盤輸入,并復(fù)制原始位置
while True:key = display(board, coords, color, next_info, held_info, score, SPEED)dummy = coords.copy() 然后我們檢測鍵盤輸入的字母以便控制方塊的移動(dòng)旋轉(zhuǎn)等操作
if key == ord("a"):if np.min(coords[:,1]) > 0:coords[:,1] -= 1if current_piece == "I":top_left[1] -= 1elif key == ord("d"):if np.max(coords[:,1]) < 9:coords[:,1] += 1if current_piece == "I":top_left[1] += 1elif key == ord("j") or key == ord("l"):if current_piece != "I" and current_piece != "O":if coords[1,1] > 0 and coords[1,1] < 9:arr = coords[1] - 1 + np.array([[[x, y] for y in range(3)] for x in range(3)])pov = coords - coords[1] + 1elif current_piece == "I":arr = top_left + np.array([[[x, y] for y in range(4)] for x in range(4)])pov = np.array([np.where(np.logical_and(arr[:,:,0] == pos[0], arr[:,:,1] == pos[1])) for pos in coords])pov = np.array([k[0] for k in np.swapaxes(pov, 1, 2)])if current_piece != "O":if key == ord("j"):arr = np.rot90(arr, -1)else:arr = np.rot90(arr)coords = arr[pov[:,0], pov[:,1]]elif key == ord("w"):drop = Trueelif key == ord("i"):if flag == 0:if held_piece == "":held_piece = current_pieceelse:switch = Trueflag = 2breakelif key == 8 or key == 27:quit = Truebreak 這里我們分別檢測字母:
a:左移動(dòng)
d:右移動(dòng)
W:直接到底部
S:往下加速
j&l:旋轉(zhuǎn)
I:更換方塊
接下來,我們需要檢查與其他方塊的碰撞,并防止該方塊進(jìn)入或旋轉(zhuǎn)到另一方塊中 。如果發(fā)生這種沖突,我們將使用coords存儲(chǔ)在dummy變量中的副本將新位置更改回原始位置
if np.max(coords[:,0]) < 20 and np.min(coords[:,0]) >= 0:if not (current_piece == "I" and (np.max(coords[:,1]) >= 10 or np.min(coords[:,1]) < 0)):if not np.all(board[coords[:,0], coords[:,1]] == 0):coords = dummy.copy()else:coords = dummy.copy()else:coords = dummy.copy() 如果它與現(xiàn)有棋子碰撞或到達(dá)棋板的底部,則停止向下移動(dòng)
if drop:while not place:if np.max(coords[:,0]) != 19:for pos in coords:if not np.array_equal(board[pos[0] + 1, pos[1]], [0, 0, 0]):place = Truebreakelse:place = Trueif place:breakcoords[:,0] += 1score += 1if current_piece == "I":top_left[0] += 1drop = False 當(dāng)一塊到達(dá)底部或碰到另一塊時(shí),將放置方塊,否則將方塊向下移動(dòng)
else:if np.max(coords[:,0]) != 19:for pos in coords:if not np.array_equal(board[pos[0] + 1, pos[1]], [0, 0, 0]):place = Truebreakelse:place = Trueif place:for pos in coords:board[tuple(pos)] = colorplace = Falsebreakcoords[:,0] += 1if key == ord("s"):score += 1if current_piece == "I":top_left[0] += 1 最后,我們按照設(shè)計(jì)規(guī)則,更新每次的得分,并實(shí)時(shí)記錄
# 計(jì)算得分lines = 0for line in range(20):if np.all([np.any(pos != 0) for pos in board[line]]):lines += 1board[1:line+1] = board[:line]if lines == 1:score += 40elif lines == 2:score += 100elif lines == 3:score += 300elif lines == 4:score += 1200
以上關(guān)于本文的內(nèi)容,僅作參考!溫馨提示:如遇健康、疾病相關(guān)的問題,請(qǐng)您及時(shí)就醫(yī)或請(qǐng)專業(yè)人士給予相關(guān)指導(dǎo)!
「愛刨根生活網(wǎng)」www.malaban59.cn小編還為您精選了以下內(nèi)容,希望對(duì)您有所幫助:- 臥室家具顏色與門的搭配
- 茶文化的收獲與感受
- 關(guān)于藝術(shù)與宗教的關(guān)系,正確的有
- 筑壘地域與要塞主要的不同
- 鹽亭方言大全 揣怎么讀
- 不會(huì)使用PS沒關(guān)系 ps怎么摳圖
- 任嘉倫新劇《與君初相識(shí)》曝光-《與君初相識(shí)》講的什么內(nèi)容
- 怎么使用跑步機(jī)健身最好!
- 一文讀懂降噪耳機(jī)與普通耳機(jī)的區(qū)別 什么是降噪耳機(jī)
- 舍賓與健美操大比拼
