俄罗斯方块小游戏是当年风靡一时的小游戏,该游戏由一个棋盘组成,该棋盘跨度为10个单元格,高度为20个单元格,如下所示。
俄罗斯方块小游戏
关于此小游戏的规则,我们不再介绍,本期文章主要用代码来实现,我们知道俄罗斯方块小游戏主要有如下7个方块组成,我们在代码中使用字母“ O”,“ I”,“ S”,“ Z”,“ L”,“ J”和“ T”表示它们。
俄罗斯方块
使用OpenCV和python创建俄罗斯方块
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
首先我们导入需要的第三方库以及定义好需要的变量
# 所有方块造型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个俄罗斯方块的造型与颜色,以上建立完成后,我们需要搭建一个游戏界面以及鼠标控制事件
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
游戏界面
搭建的游戏界面如上,我们在界面左边显示每个鼠标按键的功能,中间是主游戏区域,最右边为积分区域
以上建立完成后,我们开始主函数的部分
我们有一个while循环,在每次迭代中,我们都会在游戏中放置一个新棋子。
在俄罗斯方块中,我们可以按某个键来固定一块。通过与当前方块交换,可以保留将来使用的一种方块。
在下面的代码中,我们首先检查用户是否要使用switch变量将当前方块与保留的方块交换。如果switch变量设置为false,我们将分配current_piece,next_piece并随机选择一个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
TAG:[db:关键词]