二氧化碳中有二氧化硫怎么除雜(二氧化碳中有二氧化硫怎么除雜方程式)
2023-11-13
更新時(shí)間:2023-11-13 18:05:56作者:佚名
俄羅斯方塊小游戲
關(guān)于此小游戲的規(guī)則,我們不再介紹,本期文章主要用代碼來(lái)實(shí)現(xiàn),我們知道俄羅斯方塊小游戲主要有如下7個(gè)方塊組成,我們?cè)诖a中使用字母“ O”,“ I”,“ S”,“ Z”,“ L”,“ J”和“ T”表示它們。
俄羅斯方塊
import cv2import numpy as npfrom random import choiceSPEED = 1 #控制速度board = np.uint8(np.zeros([20, 10, 3]))quit = Falseplace = Falsedrop = Falseswitch = Falseheld_piece = ""flag = 0score = 0
首先我們導(dǎo)入需要的第三方庫(kù)以及定義好需要的變量
# 所有方塊造型next_piece = choice(["O", "I", "S", "Z", "L", "J", "T"])def get_info(piece): if piece == "I": coords = np.array([[0, 3], [0, 4], [0, 5], [0, 6]]) color = [255, 155, 15] elif piece == "T": coords = np.array([[1, 3], [1, 4], [1, 5], [0, 4]]) color = [138, 41, 175] elif piece == "L": coords = np.array([[1, 3], [1, 4], [1, 5], [0, 5]]) color = [2, 91, 227] elif piece == "J": coords = np.array([[1, 3], [1, 4], [1, 5], [0, 3]]) color = [198, 65, 33] elif piece == "S": coords = np.array([[1, 5], [1, 4], [0, 3], [0, 4]]) color = [55, 15, 215] elif piece == "Z": coords = np.array([[1, 3], [1, 4], [0, 4], [0, 5]]) color = [1, 177, 89] else: coords = np.array([[0, 4], [0, 5], [1, 4], [1, 5]]) color = [2, 159, 227] return coords, color
然后我們建立7個(gè)俄羅斯方塊的造型與顏色,以上建立完成后,我們需要搭建一個(gè)游戲界面以及鼠標(biāo)控制事件
def display(board, coords, color, next_info, held_info, score, SPEED): border = np.uint8(127 - np.zeros([20, 1, 3])) border_ = np.uint8(127 - np.zeros([1, 34, 3])) dummy = board.copy() dummy[coords[:,0], coords[:,1]] = color right = np.uint8(np.zeros([20, 10, 3])) right[next_info[0][:,0] 2, next_info[0][:,1]] = next_info[1] left = np.uint8(np.zeros([20, 10, 3])) left[held_info[0][:,0] 2, held_info[0][:,1]] = held_info[1] dummy = np.concatenate((border, left, border, dummy, border, right, border), 1) dummy = np.concatenate((border_, dummy, border_), 0) dummy = dummy.repeat(20, 0).repeat(20, 1) dummy = cv2.putText(dummy, str(score), (520, 200), cv2.FONT_HERSHEY_DUPLEX, 1, [0, 0, 255], 2)# 控制鍵 dummy = cv2.putText(dummy, "A - move left", (45, 200), cv2.FONT_HERSHEY_DUPLEX, 0.6, [0, 0, 255]) dummy = cv2.putText(dummy, "D - move right", (45, 225), cv2.FONT_HERSHEY_DUPLEX, 0.6, [0, 0, 255]) dummy = cv2.putText(dummy, "S - move down", (45, 250), cv2.FONT_HERSHEY_DUPLEX, 0.6, [0, 0, 255]) dummy = cv2.putText(dummy, "W - hard drop", (45, 275), cv2.FONT_HERSHEY_DUPLEX, 0.6, [0, 0, 255]) dummy = cv2.putText(dummy, "J - rotate left", (45, 300), cv2.FONT_HERSHEY_DUPLEX, 0.6, [0, 0, 255]) dummy = cv2.putText(dummy, "L - rotate right", (45, 325), cv2.FONT_HERSHEY_DUPLEX, 0.6, [0, 0, 255]) dummy = cv2.putText(dummy, "I - hold", (45, 350), cv2.FONT_HERSHEY_DUPLEX, 0.6, [0, 0, 255]) cv2.imshow("Tetris", dummy) key = cv2.waitKey(int(1000/SPEED)) return key
游戲界面
搭建的游戲界面如上,我們?cè)诮缑孀筮咃@示每個(gè)鼠標(biāo)按鍵的功能,中間是主游戲區(qū)域,最右邊為積分區(qū)域
以上建立完成后,我們開(kāi)始主函數(shù)的部分
我們有一個(gè)while循環(huán),在每次迭代中,我們都會(huì)在游戲中放置一個(gè)新棋子。
在俄羅斯方塊中,我們可以按某個(gè)鍵來(lái)固定一塊。通過(guò)與當(dāng)前方塊交換,可以保留將來(lái)使用的一種方塊。
在下面的代碼中,我們首先檢查用戶(hù)是否要使用switch變量將當(dāng)前方塊與保留的方塊交換。如果switch變量設(shè)置為false,我們將分配current_piece,next_piece并隨機(jī)選擇一個(gè)new next_piece
if __name__ == "__main__": while not quit: if switch: held_piece, current_piece = current_piece, held_piece switch = False else: current_piece = next_piece next_piece = choice(["I", "T", "L", "J", "Z", "S", "O"])·
if flag