利用数组制作的简单推箱子游戏
代码及简要分析如下:
1 //推箱子小游戏 2 //定义一个三维数组存放地图,三维数组由单独的二维数组组成,本游戏中只有三个地图 3 int[][,] a = new int[3][,]; 4 //用二维数组创建地图,0是空位,1是墙,2是人,3是箱子,4是终点 5 int[,] b0 = new int[10, 10]{ 6 {1,1,1,1,1,1,1,1,1,1}, 7 {1,0,0,0,1,0,1,0,0,1}, 8 {1,0,0,0,1,0,1,0,0,1}, 9 {1,0,3,0,1,0,1,0,0,1}, 10 {1,0,0,0,1,1,1,0,0,1}, 11 {1,0,0,0,0,0,0,0,0,1}, 12 {1,0,0,1,0,0,0,0,0,1}, 13 {1,2,0,1,1,1,1,0,0,1}, 14 {1,0,0,1,0,4,0,0,0,1}, 15 {1,1,1,1,1,1,1,1,1,1}}; 16 int[,] b1 = new int[10, 10]{ 17 {1,1,1,1,1,1,1,1,1,1}, 18 {1,0,0,0,1,0,1,0,0,1}, 19 {1,0,3,0,1,0,1,3,0,1}, 20 {1,0,0,0,1,0,1,0,0,1}, 21 {1,0,0,0,1,1,1,0,0,1}, 22 {1,0,0,0,0,0,0,0,0,1}, 23 {1,0,0,1,4,0,0,0,0,1}, 24 {1,2,0,1,1,1,1,0,0,1}, 25 {1,0,0,1,0,4,0,0,0,1}, 26 {1,1,1,1,1,1,1,1,1,1}}; 27 int[,] b2 = new int[10, 10]{ 28 {1,1,1,1,1,1,1,1,1,1}, 29 {1,0,4,1,0,0,0,0,0,1}, 30 {1,0,0,1,0,0,0,0,4,1}, 31 {1,0,0,0,0,0,0,0,4,1}, 32 {1,0,0,3,0,0,0,0,0,1}, 33 {1,1,1,0,0,0,0,0,0,1}, 34 {1,0,0,2,0,0,0,0,0,1}, 35 {1,0,3,0,1,0,3,0,0,1}, 36 {1,0,0,0,1,0,0,0,0,1}, 37 {1,1,1,1,1,1,1,1,1,1}}; 38 a[0] = b0; 39 a[1] = b1; 40 a[2] = b2; 41 42 43 for (int z = 0; z < 3; z++) //如果增加地图,需修改判断条件 44 {//for1 45 Console.Clear(); 46 Console.WriteLine("第{0}关!", z + 1); //清屏并显示第几关 47 48 ConsoleKeyInfo start = Console.ReadKey(); 49 string st = start.Key.ToString().ToLower(); //以上两行为读取按键信息,转成字符串格式并小写 50 //游戏开始 51 if (st == "spacebar") //按空格键,游戏开始 52 {//if1 53 int[,] map = a[z]; //读取地图 54 55 //判断人的位置,i为行,j为列 56 int i = 0, j = 0; 57 for (int m = 0; m < 10; m++) 58 { 59 for (int n = 0; n < 10; n++) 60 { 61 if (map[m, n] == 2) 62 { 63 i = m; 64 j = n; 65 break; 66 } 67 } 68 } 69 70 //判断终点个数 71 int over = 0; 72 for (int m = 0; m < 10; m++) 73 { 74 for (int n = 0; n < 10; n++) 75 { 76 if (map[m, n] == 4) 77 { 78 over++; 79 } 80 } 81 } 82 83 //显示及操作 84 for (; true; ) 85 {//for2 86 //输出显示 87 Console.Clear(); 88 for (int x = 0; x < 10; x++) 89 { 90 for (int y = 0; y < 10; y++) 91 { 92 if (map[x, y] == 0) 93 { 94 Console.Write(" "); 95 } 96 else if (map[x, y] == 1) 97 { 98 Console.Write("■"); 99 } 100 else if (map[x, y] == 2 || map[x, y] == 6) 101 { 102 Console.Write("♀"); 103 } 104 else if (map[x, y] == 3 || map[x, y] == 7) 105 { 106 Console.Write("□"); 107 } 108 else if (map[x, y] == 4) 109 { 110 Console.Write("※"); 111 } 112 } 113 Console.Write("\n"); //换行 114 } 115 116 //判断有箱子的终点个数 117 int over1 = 0; 118 for (int m = 0; m < 10; m++) 119 { 120 for (int n = 0; n < 10; n++) 121 { 122 if (map[m, n] == 7) 123 { 124 over1++; 125 } 126 } 127 } 128 129 //判断是否所有终点有箱子 130 if (over1 == over) 131 { 132 Console.WriteLine("过关!"); 133 break; 134 } 135 136 //操作部分 137 ConsoleKeyInfo K = Console.ReadKey(); 138 string k = K.Key.ToString(); 139 k = k.ToLower(); 140 if (k == "uparrow") //判断人是否是向上 141 { 142 if (map[i - 1, j] == 0 || map[i - 1, j] == 4) //判断人上方是不是空位 143 { 144 map[i - 1, j] = map[i - 1, j] + 2; 145 map[i, j] = map[i, j] - 2; 146 i--; //人移动之后,改变其所在行数 147 } 148 else if ((map[i - 1, j] == 3 || map[i - 1, j] == 7) && map[i - 2, j] != 1) //人上方是箱子,判断箱子上方是否是空位 149 { 150 map[i - 2, j] = map[i - 2, j] + 3; 151 map[i - 1, j] = map[i - 1, j] - 3 + 2; 152 map[i, j] = map[i, j] - 2; 153 i--; //人移动之后,改变其所在行数 154 } 155 else //如果人移动,输出提示音 156 Console.Write("\a"); 157 } 158 else if (k == "downarrow") 159 { 160 if (map[i + 1, j] == 0 || map[i + 1, j] == 4) //判断人下方是不是空位 161 { 162 map[i + 1, j] = map[i + 1, j] + 2; 163 map[i, j] = map[i, j] - 2; 164 i++; //人移动之后,改变其所在行数 165 } 166 else if ((map[i + 1, j] == 3 || map[i + 1, j] == 7) && map[i + 2, j] != 1) //人下方是箱子,判断箱子下方是否是空位 167 { 168 map[i + 2, j] = map[i + 2, j] + 3; 169 map[i + 1, j] = map[i + 1, j] - 3 + 2; 170 map[i, j] = map[i, j] - 2; 171 i++; //人移动之后,改变其所在行数 172 } 173 else //如果人移动,输出提示音 174 Console.Write("\a"); 175 } 176 else if (k == "leftarrow") 177 { 178 if (map[i, j - 1] == 0 || map[i, j - 1] == 4) //判断人左方是不是空位 179 { 180 map[i, j - 1] = map[i, j - 1] + 2; 181 map[i, j] = map[i, j] - 2; 182 j--; //人移动之后,改变其所在列数 183 } 184 else if ((map[i, j - 1] == 3 || map[i, j - 1] == 7) && map[i, j - 2] != 1) //人左方是箱子,判断箱子左方是否是空位 185 { 186 map[i, j - 2] = map[i, j - 2] + 3; 187 map[i, j - 1] = map[i, j - 1] - 3 + 2; 188 map[i, j] = map[i, j] - 2; 189 j--; //人移动之后,改变其所在列数 190 } 191 else //如果人移动,输出提示音 192 Console.Write("\a"); 193 } 194 else if (k == "rightarrow") 195 { 196 if (map[i, j + 1] == 0 || map[i, j + 1] == 4) //判断人右方是不是空位 197 { 198 map[i, j + 1] = map[i, j + 1] + 2; 199 map[i, j] = map[i, j] - 2; 200 j++; //人移动之后,改变其所在列数 201 } 202 else if ((map[i, j + 1] == 3 || map[i, j + 1] == 7) && map[i, j + 2] != 1) //人右方是箱子,判断箱子右方是否是空位 203 { 204 map[i, j + 2] = map[i, j + 2] + 3; 205 map[i, j + 1] = map[i, j + 1] - 3 + 2; 206 map[i, j] = map[i, j] - 2; 207 j++; //人移动之后,改变其所在列数 208 } 209 else //如果人移动,输出提示音 210 Console.Write("\a"); 211 } 212 else if (k == "b") //按“B”键,重新开始 213 { 214 z--; //用来抵消for1的“z++” 215 break; //跳出for2循环 216 } 217 }//for2 218 }//if1 219 else if (st == "n") //按“N”键,进入下一关 220 { 221 continue; //for1进入下一次循环 222 } 223 else 224 Console.Write("\a"); 225 }//for1
时间: 2024-11-16 01:55:12