#region 名字
中间的代码可以折叠起来
#endregion
如何获取用户的操作:
ConsoleKeyInfo info = Console.ReadKey();
string anjian = info.Key.ToString();
Console.WriteLine(anjian);
推箱子:
1 int x = 2, y = 7, zhong = 0;//定义小人的原始位置以及中间变量 2 #region 地图 3 int[,] ditu = new int[10, 10] { 4 { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, 5 { 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, 6 { 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, 7 { 1, 0, 0, 0, 0, 0, 4, 0, 0, 1 }, 8 { 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, 9 { 1, 0, 0, 3, 0, 0, 0, 0, 0, 1 }, 10 { 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, 11 { 1, 0, 2, 0, 0, 0, 0, 0, 0, 1 }, 12 { 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, 13 { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } }; 14 #endregion 15 16 17 //1墙,2人,3箱子,4终点,5箱子归位 18 for (; ; ) 19 { 20 #region 打印地图 21 Console.Clear(); 22 for (int i = 0; i < 10; i++) 23 { 24 for (int l = 0; l < 10; l++) 25 { 26 if (ditu[i, l] == 0) 27 { 28 Console.Write(" "); 29 } 30 else if (ditu[i, l] == 1) 31 { 32 Console.Write("■"); 33 } 34 else if (ditu[i, l] == 2) 35 { 36 Console.Write("♀"); 37 } 38 else if (ditu[i, l] == 3) 39 { 40 Console.Write("□"); 41 } 42 else if (ditu[i, l] == 4) 43 { 44 Console.Write("☆"); 45 } 46 else if (ditu[i, l] == 5) 47 { 48 Console.Write("★"); 49 } 50 } 51 Console.WriteLine(); 52 } 53 if (ditu[3, 6] == 5) 54 { 55 Console.WriteLine("游戏胜利!"); 56 break; 57 } 58 #endregion 59 #region 控制人物 60 ConsoleKeyInfo info = Console.ReadKey(); 61 string anjian = info.Key.ToString(); 62 if (anjian == "W")//上 63 { 64 if (ditu[y - 1, x] == 0)//小人走 65 { 66 zhong = ditu[y, x]; 67 ditu[y, x] = ditu[y - 1, x]; 68 ditu[y - 1, x] = zhong; 69 y--; 70 } 71 else if (ditu[y - 1, x] == 3)//小人碰见箱子 72 { 73 if (ditu[y - 2, x] == 0)//推箱子走 74 { 75 zhong = ditu[y, x]; 76 ditu[y, x] = ditu[y - 2, x]; 77 ditu[y - 2, x] = ditu[y - 1, x]; 78 ditu[y - 1, x] = zhong; 79 y--; 80 } 81 else if (ditu[y - 2, x] == 4)//把箱子推到终点 82 { 83 zhong = ditu[y, x]; 84 ditu[y, x] = 0; 85 ditu[y - 1, x] = zhong; 86 ditu[y - 2, x] = 5; 87 } 88 } 89 } 90 else if (anjian == "S")//下 91 { 92 if (ditu[y + 1, x] == 0) 93 { 94 zhong = ditu[y, x]; 95 ditu[y, x] = ditu[y + 1, x]; 96 ditu[y + 1, x] = zhong; 97 y++; 98 } 99 else if (ditu[y + 1, x] == 3) 100 { 101 if (ditu[y + 2, x] == 0) 102 { 103 zhong = ditu[y, x]; 104 ditu[y, x] = ditu[y + 2, x]; 105 ditu[y + 2, x] = ditu[y + 1, x]; 106 ditu[y + 1, x] = zhong; 107 y++; 108 } 109 else if (ditu[y + 2, x] == 4) 110 { 111 zhong = ditu[y, x]; 112 ditu[y, x] = 0; 113 ditu[y + 1, x] = zhong; 114 ditu[y + 2, x] = 5; 115 } 116 } 117 } 118 else if (anjian == "A")//左 119 { 120 if (ditu[y, x - 1] == 0) 121 { 122 zhong = ditu[y, x]; 123 ditu[y, x] = ditu[y, x - 1]; 124 ditu[y, x - 1] = zhong; 125 x--; 126 } 127 else if (ditu[y, x - 1] == 3) 128 { 129 if (ditu[y, x - 2] == 0) 130 { 131 zhong = ditu[y, x]; 132 ditu[y, x] = ditu[y, x - 2]; 133 ditu[y, x - 2] = ditu[y, x - 1]; 134 ditu[y, x - 1] = zhong; 135 x--; 136 } 137 else if (ditu[y, x - 2] == 4) 138 { 139 zhong = ditu[y, x]; 140 ditu[y, x] = 0; 141 ditu[y, x - 1] = zhong; 142 ditu[y, x - 2] = 5; 143 } 144 } 145 } 146 else if (anjian == "D")//右 147 { 148 if (ditu[y, x + 1] == 0) 149 { 150 zhong = ditu[y, x]; 151 ditu[y, x] = ditu[y, x + 1]; 152 ditu[y, x + 1] = zhong; 153 x++; 154 } 155 else if (ditu[y, x + 1] == 3) 156 { 157 if (ditu[y, x + 2] == 0) 158 { 159 zhong = ditu[y, x]; 160 ditu[y, x] = ditu[y, x + 2]; 161 ditu[y, x + 2] = ditu[y, x + 1]; 162 ditu[y, x + 1] = zhong; 163 x++; 164 } 165 else if (ditu[y, x + 2] == 4) 166 { 167 zhong = ditu[y, x]; 168 ditu[y, x] = 0; 169 ditu[y, x + 1] = zhong; 170 ditu[y, x + 2] = 5; 171 } 172 } 173 } 174 175 #endregion 176 177 }