使用OpenCV与Python编写自己的俄罗斯方块小游戏 怎么创造游戏( 二 )


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]#这个if语句只是检查游戏是否需要终止if not np.all(board[coords[:,0], coords[:,1]] == 0):break 接下来我们 在主程序里面设置while true死循环用来我们一直检测键盘输入,直到游戏结束
首先,我们使用display()功能显示板子并接收键盘输入,并复制原始位置
while True:key = display(board, coords, color, next_info, held_info, score, SPEED)dummy = coords.copy() 然后我们检测键盘输入的字母以便控制方块的移动旋转等操作
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:右移动
W:直接到底部
S:往下加速
j&l:旋转
I:更换方块
接下来,我们需要检查与其他方块的碰撞,并防止该方块进入或旋转到另一方块中 。如果发生这种冲突,我们将使用coords存储在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() 如果它与现有棋子碰撞或到达棋板的底部,则停止向下移动
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 当一块到达底部或碰到另一块时,将放置方块,否则将方块向下移动
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 最后,我们按照设计规则,更新每次的得分,并实时记录
# 计算得分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


以上关于本文的内容,仅作参考!温馨提示:如遇健康、疾病相关的问题,请您及时就医或请专业人士给予相关指导!

「四川龙网」www.sichuanlong.com小编还为您精选了以下内容,希望对您有所帮助: