最近闲来无事,回顾一下以前学过的C#控制台小程序,特发此篇:
主体思路:利用二维数组来实现。
效果图:
首先:VS2010,创建控制台程序,
然后,添加类Labyrinth,该类的代码如下,
1 using System; 2 3 namespace 迷宫 4 { 5 class Labyrinth 6 { 7 //地图 8 private char[,] map ={ {'*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',}, 9 {'*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',}, 10 {'*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',}, 11 {'*',' ','*','*',' ',' ',' ','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',}, 12 {'*',' ','*','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',' ',' ',' ',' ',' ',' ','*','*','*','*','*','*','*','*','*',}, 13 {'*',' ',' ',' ',' ',' ',' ',' ',' ','*','*','*','*','*','*','*',' ',' ',' ',' ',' ',' ',' ','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ','*','*',' ','*','*','*','*','*','*','*','*','*',}, 14 {'*','*','*','*','*','*','*','*',' ','*',' ','*','*','*','*','*',' ','*','*','*','*','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ','*','*',' ','*','*','*','*','*','*','*','*','*',}, 15 {'*','*','*','*','*','*','*','*',' ','*',' ','*','*','*','*','*',' ','*','*','*','*','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ','*','*',' ','*','*','*','*','*','*','*','*','*',}, 16 {'*','*','*','*','*','*','*','*',' ',' ',' ',' ',' ',' ',' ',' ',' ','*','*','*','*','*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','$',}, 17 {'*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',}, 18 }; 19 private int x = 1, y = 1;//当前小人坐标 20 private bool flag = false;//标志是否找到出口 21 22 public Labyrinth() 23 { 24 Init(); 25 Display(); 26 } 27 28 private void Display()//显示 29 { 30 Console.Clear(); 31 for (int i = 0; i < 10; i++) 32 { 33 for (int j = 0; j < 60; j++) 34 { 35 Console.Write(map[i, j]); 36 } 37 Console.WriteLine(); 38 } 39 } 40 private void Init()//初始化小人 41 { 42 map[x, y] = '0'; 43 } 44 private void MoveLeft()//左方向 45 { 46 if (y==1) 47 { 48 return; 49 } 50 if (map[x,y-1]=='*') 51 { 52 return; 53 } 54 map[x,y]=' '; 55 y--; 56 isFindExit(); 57 map[x, y] = '0'; 58 } 59 private void MoveRight()//右方向 60 { 61 if (y==59) 62 { 63 return; 64 } 65 if (map[x,y+1]=='*') 66 { 67 return; 68 } 69 map[x, y] = ' '; 70 y++; 71 isFindExit(); 72 map[x, y] = '0'; 73 } 74 75 private void MoveUp()//上方向 76 { 77 if (x == 1) 78 { 79 return; 80 } 81 if (map[x-1, y] == '*') 82 { 83 return; 84 } 85 map[x, y] = ' '; 86 x--; 87 isFindExit(); 88 map[x, y] = '0'; 89 } 90 private void MoveDown()//下方向 91 { 92 if (x == 10) 93 { 94 return; 95 } 96 if (map[x + 1, y] == '*') 97 { 98 return; 99 } 100 map[x, y] = ' '; 101 x++; 102 isFindExit(); 103 map[x, y] = '0'; 104 } 105 106 private void isFindExit()//是否找到出口 107 { 108 if (map[x, y] == '$') 109 { 110 flag = true; 111 return; 112 } 113 } 114 private void Move(ConsoleKey DownKey)//移动 115 { 116 if (DownKey == ConsoleKey.A) 117 { 118 MoveLeft(); 119 } 120 if (DownKey == ConsoleKey.D) 121 { 122 MoveRight(); 123 } 124 if (DownKey == ConsoleKey.W) 125 { 126 MoveUp(); 127 } 128 if (DownKey == ConsoleKey.S) 129 { 130 MoveDown(); 131 } 132 } 133 public void Start()//游戏开始 134 { 135 while (true) 136 { 137 Console.WriteLine("键盘A(左)D(右)W(上)S(下)"); 138 ConsoleKey DownKey = Console.ReadKey().Key;//接受键盘录入 139 Move(DownKey);//移动 140 if (flag)//是否 141 { 142 Console.Clear();//清屏 143 Console.WriteLine("过关!"); 144 break; 145 } 146 Display();//显示 147 } 148 } 149 } 150 }
主程序代码:
1 using System; 2 3 namespace 迷宫 4 { 5 class Program 6 { 7 static void Main(string[] args) 8 { 9 Labyrinth labyrinth = new Labyrinth(); 10 labyrinth.Start(); 11 Console.Read(); 12 } 13 } 14 }