今天我们来聊聊俄罗斯方块,最近比较闲就简单的做了一个俄罗斯方块,其实说白了就是对一堆二维数组的处理。我们把其中方块的形状写成二维数组的形式,在这里我定义了六中形状,即:
1 public void InitData () { 2 tetrisData = new List<TetrisItemModel> (); 3 //第一种形状 4 TetrisItemModel item0 = new TetrisItemModel ( 5 new List<int[,]> { 6 new int[,] { 7 {0,1,0,0}, 8 {0,1,0,0}, 9 {0,1,0,0}, 10 {0,1,0,0}, 11 }, 12 new int[,] { 13 {1,1,1,1}, 14 {0,0,0,0}, 15 {0,0,0,0}, 16 {0,0,0,0}, 17 }, 18 new int[,] { 19 {0,1,0,0}, 20 {0,1,0,0}, 21 {0,1,0,0}, 22 {0,1,0,0}, 23 }, 24 new int[,] { 25 {1,1,1,1}, 26 {0,0,0,0}, 27 {0,0,0,0}, 28 {0,0,0,0}, 29 } 30 } 31 32 ); 33 34 //第二种形状 35 TetrisItemModel item1 = new TetrisItemModel ( 36 new List<int[,]> { 37 new int[,] { 38 {1,0,0,0}, 39 {1,1,0,0}, 40 {0,1,0,0}, 41 {0,0,0,0}, 42 }, 43 new int[,] { 44 {0,1,1,0}, 45 {1,1,0,0}, 46 {0,0,0,0}, 47 {0,0,0,0}, 48 }, 49 new int[,] { 50 {1,0,0,0}, 51 {1,1,0,0}, 52 {0,1,0,0}, 53 {0,0,0,0}, 54 }, 55 new int[,] { 56 {0,1,1,0}, 57 {1,1,0,0}, 58 {0,0,0,0}, 59 {0,0,0,0}, 60 } 61 } 62 63 ); 64 //第三种形状 65 TetrisItemModel item2 = new TetrisItemModel ( 66 new List<int[,]> { 67 new int[,] { 68 {0,1,0,0}, 69 {1,1,0,0}, 70 {0,1,0,0}, 71 {0,0,0,0}, 72 }, 73 new int[,] { 74 {0,1,0,0}, 75 {1,1,1,0}, 76 {0,0,0,0}, 77 {0,0,0,0}, 78 }, 79 new int[,] { 80 {1,0,0,0}, 81 {1,1,0,0}, 82 {1,0,0,0}, 83 {0,0,0,0}, 84 }, 85 new int[,] { 86 {1,1,1,0}, 87 {0,1,0,0}, 88 {0,0,0,0}, 89 {0,0,0,0}, 90 } 91 } 92 93 ); 94 95 //第四种形状 96 TetrisItemModel item3 = new TetrisItemModel ( 97 new List<int[,]> { 98 new int[,] { 99 {1,0,0,0}, 100 {1,0,0,0}, 101 {1,1,0,0}, 102 {0,0,0,0}, 103 }, 104 new int[,] { 105 {1,1,1,0}, 106 {1,0,0,0}, 107 {0,0,0,0}, 108 {0,0,0,0}, 109 }, 110 new int[,] { 111 {1,1,0,0}, 112 {0,1,0,0}, 113 {0,1,0,0}, 114 {0,0,0,0}, 115 }, 116 new int[,] { 117 {0,0,1,0}, 118 {1,1,1,0}, 119 {0,0,0,0}, 120 {0,0,0,0}, 121 } 122 } 123 124 ); 125 //第五种形状 126 TetrisItemModel item4 = new TetrisItemModel ( 127 new List<int[,]> { 128 new int[,] { 129 {0,1,0,0}, 130 {0,1,0,0}, 131 {1,1,0,0}, 132 {0,0,0,0}, 133 }, 134 new int[,] { 135 {1,0,0,0}, 136 {1,1,1,0}, 137 {0,0,0,0}, 138 {0,0,0,0}, 139 }, 140 new int[,] { 141 {1,1,0,0}, 142 {1,0,0,0}, 143 {1,0,0,0}, 144 {0,0,0,0}, 145 }, 146 new int[,] { 147 {1,1,1,0}, 148 {0,0,1,0}, 149 {0,0,0,0}, 150 {0,0,0,0}, 151 } 152 } 153 154 ); 155 //第六种形状 156 TetrisItemModel item5 = new TetrisItemModel ( 157 new List<int[,]> { 158 new int[,] { 159 {0,1,0,0}, 160 {1,1,0,0}, 161 {1,0,0,0}, 162 {0,0,0,0}, 163 }, 164 new int[,] { 165 {1,1,0,0}, 166 {0,1,1,0}, 167 {0,0,0,0}, 168 {0,0,0,0}, 169 }, 170 new int[,] { 171 {0,1,0,0}, 172 {1,1,0,0}, 173 {1,0,0,0}, 174 {0,0,0,0}, 175 }, 176 new int[,] { 177 {1,1,0,0}, 178 {0,1,1,0}, 179 {0,0,0,0}, 180 {0,0,0,0}, 181 } 182 } 183 184 );
随机出现的方块的形状就在里边随机,然后在对应的位置显示出小方块,随机出的方块的属性:
1 private int _currentX; 2 private int _currentY; 3 //4*4的二维数组 4 private int [,]data;
背景也是一个二维数组,俄罗斯方块主要考察的是二维数组,只要逻辑清楚了,做出来是很容易的