二维列表自定义工具helper
1 """ 2 二维列表工具 3 """ 4 5 6 class Vector2: 7 """ 8 向量 9 """ 10 11 def __init__(self, x=0, y=0): 12 self.x = x 13 self.y = y 14 15 # 将函数转移到类中,就是静态方法. 16 @staticmethod 17 def right(): 18 return Vector2(0, 1) 19 20 @staticmethod 21 def up(): 22 return Vector2(-1, 0) 23 24 @staticmethod 25 def left(): 26 return Vector2(0, -1) 27 28 @staticmethod 29 def down(): 30 return Vector2(1, 0) 31 32 @staticmethod 33 def right_up(): 34 return Vector2(-1, 1) 35 36 @staticmethod 37 def right_down(): 38 return Vector2(1, 1) 39 40 @staticmethod 41 def left_up(): 42 return Vector2(1, 1) 43 44 @staticmethod 45 def left_down(): 46 return Vector2(1, -1) 47 48 49 class DoubleListHelper: 50 """ 51 二维列表助手类 52 定义:在开发过程中,所有对二维列表的常用操作. 53 """ 54 55 @staticmethod 56 def get_elements(list_target, v_pos, v_dir, count): 57 result = [] 58 for i in range(count): 59 v_pos.x += v_dir.x 60 v_pos.y += v_dir.y 61 result.append(list_target[v_pos.x][v_pos.y]) 62 return result 63 64 65 # 测试............. 66 list01 = [ 67 ["00", "01", "02", "03"], 68 ["10", "11", "12", "13"], 69 ["20", "21", "22", "23"], 70 ] 71 72 73 # 获取23位置 向左 2个元素 74 # re02 = DoubleListHelper.get_elements(list01, Vector2(2, 3), Vector2.left(), 2) 75 # print(re02) 76 # 获取02位置 向下 2个元素 77 # re03 = DoubleListHelper.get_elements(list01, Vector2(0, 2), Vector2.down(), 2) 78 # print(re03) 79 # 获取20位置 右上 2个元素 80 # re04 = DoubleListHelper.get_elements(list01, Vector2(2, 0), Vector2.right_up(), 2) 81 # print(re04)