c#语言基础-飞行棋项目

一、项目目标

首界面:

静态:

  • 游戏名
  • 提示双方姓名输入

动态:

  • 输入双方姓名,红方姓名输入完成后任意键切换输入蓝方姓名
  • 对双方姓名输入是否规范情况进行判断
  • 完成后任意键进入游戏

游戏界面:

  • 显示初始界面

  • 按任意键扔骰子 显示结果,后任意键切换另一玩家
  • 若一玩家踩到另一玩家所在位置,另一玩家向后退六格
  • 幸运轮盘符号为⊙,若玩家踩到幸运轮盘,该玩家可以选择:1与另一玩家的位置,2让另一玩家向后退6格,通过键盘输入判断玩家选择
  • 地雷为☆,若玩家踩到地雷,向后退六格
  • 暂停为△,若玩家踩到暂停,该玩家暂停一局,下局失去扔骰子的权利
  • 时空隧道为□,若玩家踩到时空隧道,向前进十格

  •  任意一方到达终点显示该玩家胜利

 

 二、代码实现

  1. Main()
     1 static void Main(string[] args)
     2         {
     3             GameShow();
     4             InitialMap();
     5             Intname();
     6             Console.WriteLine("{0}的飞机为林", PlayerNames[0]);
     7             Console.WriteLine("{0}的飞机为邓", PlayerNames[1]);
     8             Console.Clear();
     9             DrawMap();
    10             while (PlayerPos[0] < 99 && PlayerPos[1] < 99)
    11             {
    12                 if (Jump[0] == false)
    13                     play(0);
    14                 else Jump[0] = false;
    15                 if (PlayerPos[0] == 99)
    16                 {
    17                     Console.ForegroundColor = ConsoleColor.Red;
    18                     Console.WriteLine("恭喜玩家{0}获得了胜利", PlayerNames[0]);
    19                     Console.ReadKey();
    20                     break;
    21                 }
    22 
    23                 if (Jump[1]==false)
    24                 play(1);
    25                 else Jump[1] = false;
    26                 if (PlayerPos[1] == 99)
    27                 {
    28                     Console.ForegroundColor = ConsoleColor.Blue;
    29                     Console.WriteLine("恭喜玩家{0}获得了胜利", PlayerNames[1]);
    30                     Console.ReadKey();
    31                     break;
    32                 }
    33                 
    34             }
    35                
    36             Console.ReadKey();
    37
  2.  GameShow() 初始界面游戏标题块设计
     1 public static void GameShow()
     2         {
     3             Console.ForegroundColor = ConsoleColor.Red  ;
     4             Console.WriteLine("--------------------------------");
     5             Console.ForegroundColor = ConsoleColor.Yellow;
     6             Console.WriteLine("--------------------------------");
     7             Console.ForegroundColor = ConsoleColor.Green;
     8             Console.WriteLine("--------------------------------");
     9             Console.ForegroundColor = ConsoleColor.Cyan;
    10             Console.WriteLine("------#一个简单的飞行棋#--------");
    11             Console.ForegroundColor = ConsoleColor.Blue;
    12             Console.WriteLine("--------------------------------");
    13             Console.ForegroundColor = ConsoleColor.White;
    14             Console.WriteLine("--------------------------------");
    15             Console.ForegroundColor = ConsoleColor.Magenta;
    16             Console.WriteLine("--------------------------------");
    17             Console.WriteLine();
    18         }
    GameShow()
  3. InitialMap() 初始化游戏地图
     1  public static void InitialMap()
     2         {
     3             int[] luckyturn = {6,23,40,55,69,83};
     4             int[] landMine = { 5,13,17,33,38,50,64,80,94};
     5             int[] pause = { 9, 27, 60, 93 };
     6             int[] timeTunnel = { 20, 25, 45, 63, 72, 88, 90 };
     7             for(int i=0;i<luckyturn.Length;i++)
     8             {
     9                 Maps[luckyturn[i]] = 1;
    10             }
    11             for (int i = 0; i < landMine.Length; i++)
    12             {
    13                 Maps[landMine[i]] = 2;
    14             }
    15             for (int i = 0; i < pause.Length; i++)
    16             {
    17                 Maps[pause[i]] = 3;
    18             }
    19             for (int i = 0; i < timeTunnel.Length; i++)
    20             {
    21                 Maps[timeTunnel[i]] = 4;
    22             }
    23         }
    InitialMap()

    使用一维数组Maps构建地图,static int[] Maps = new int[100]; 不同机关使用不同数字表示。

  4. DrawMap() 按规定位置画地图
     1         public static void DrawMap()
     2         {
     3             Console.ForegroundColor = ConsoleColor.White;
     4             Console.WriteLine("幸运轮盘⊙,地雷☆,暂停△,时空隧道□");
     5             Console.WriteLine();
     6             #region 第一行
     7             for (int i=0;i<30;i++)
     8             {
     9                 DrawStep(i);
    10                 #endregion
    11             }
    12             Console.WriteLine();
    13             for (int i = 30; i < 35; i++)
    14             {
    15                 for (int J = 0; J < 29; J++)
    16                 {
    17                     Console.Write("  ");
    18                 }
    19                 DrawStep(i);
    20                 Console.WriteLine();
    21             }
    22             for(int i=64;i>=35;i--)
    23             {
    24                 DrawStep(i);
    25             }
    26             Console.WriteLine();
    27             for(int i=65;i<70;i++)
    28             {
    29                 DrawStep(i);
    30                 Console.WriteLine();
    31             }
    32             for(int i=70;i<100;i++)
    33             {
    34                 DrawStep(i);
    35             }
    36             Console.WriteLine();
    37             Console.WriteLine();
    38         }
    DrawMap()

    DrawStep() 根据存在PlayerPos中的玩家所在位置(static int[] PlayerPos = new int[2])画玩家标志、根据Maps数组内对应的数字画关卡和路。

     1 public static void DrawStep(int i)
     2         {
     3             if (PlayerPos[0] == PlayerPos[1] && PlayerPos[1] == i)
     4             {
     5                 Console.ForegroundColor = ConsoleColor.Green;
     6                 Console.Write("&&");
     7             }
     8             else if (PlayerPos[0] == i)
     9             {
    10                 Console.ForegroundColor = ConsoleColor.Red;
    11 
    12                 Console.Write("");
    13             }
    14             else if (PlayerPos[1] == i)
    15             {
    16                 Console.ForegroundColor = ConsoleColor.Blue;
    17 
    18                 Console.Write("");
    19             }
    20             else
    21             {
    22                 Console.ForegroundColor = ConsoleColor.Yellow;
    23                 switch (Maps[i])
    24                 {
    25                     case 0:
    26                         Console.Write("");
    27                         break;
    28                     case 1:
    29                         Console.Write("");
    30                         break;
    31                     case 2:
    32                         Console.Write("");
    33                         break;
    34                     case 3:
    35                         Console.Write("");
    36                         break;
    37                     case 4:
    38                         Console.Write("");
    39                         break;
    40 
    41                 }
    42             }
    43         }
    DrawStep(int i)
  5. Intname() 输入玩家姓名

     1 public static void Intname()
     2         {
     3             Console.ForegroundColor = ConsoleColor.White;
     4             Console.WriteLine("请输入红方姓名:");
     5             PlayerNames[0] = Console.ReadLine();
     6             while(PlayerNames[0]=="")
     7             {
     8                 Console.WriteLine("玩家必须拥有姓名!");
     9                 PlayerNames[0] = Console.ReadLine();
    10             }
    11             Console.WriteLine("请输入蓝方姓名:");
    12             PlayerNames[1] = Console.ReadLine();
    13             while (PlayerNames[1] == ""|| PlayerNames[1] == PlayerNames[0])
    14             {
    15                 if(PlayerNames[1] == "")
    16                 Console.WriteLine("玩家必须拥有姓名!");
    17                 if (PlayerNames[1] == PlayerNames[0])
    18                 Console.WriteLine("不可以重名");
    19                 PlayerNames[0] = Console.ReadLine();
    20             }
    21         }
    Intname()
  6. play() 玩游戏

     1 public static void play(int number)
     2         {
     3             
     4             Random ran = new Random();
     5             int step = ran.Next(1,7);
     6             Console.ForegroundColor = ConsoleColor.White;
     7             Console.WriteLine("{0}按任意键扔色子", PlayerNames[number]);
     8             Console.ReadKey(true);
     9             Console.WriteLine("哇哦,{0}扔出了{1}!",PlayerNames[number],step);
    10             PlayerPos[number] += step;
    11             ChangePos();
    12             Console.WriteLine("{0}前进了{1}格", PlayerNames[number],step);
    13             if(PlayerPos[number] ==PlayerPos[1-number])
    14             {
    15                 Console.WriteLine("吼吼,{0}踩到了{1},{1}向后退六格",PlayerNames[number],PlayerNames[1-number]);
    16                 PlayerPos[1- number] -= 6;
    17                 ChangePos();
    18 
    19             }
    20             else if(Maps[PlayerPos[number]] !=0)
    21             {
    22                     switch (Maps[PlayerPos[number]])
    23                     {
    24                         case 1: PlayerPos[number] -= 6;
    25                             Console.WriteLine("吼吼,{0}踩到了地雷,向后退六格", PlayerNames[number]);
    26                             break;
    27                         case 2:
    28                             Console.WriteLine("恭喜{0}踩到幸运罗盘", PlayerNames[number]);
    29                             Console.WriteLine("{0},你现在有两个选择,1是交换你和{1}的位置,2是让{2}向后退6格",PlayerNames[number], PlayerNames[1-number],PlayerNames[1-number]);
    30                             string input = Console.ReadLine();
    31                             {
    32                                 if(input=="1")
    33                                 {
    34                                     Console.WriteLine("吼吼,{0}选择和{1}交换位置",PlayerNames[number], PlayerNames[1 - number]);
    35                                     int temp = PlayerPos[number];
    36                                     PlayerPos[number] = PlayerPos[1- number];
    37                                     PlayerPos[1- number] = temp;
    38                                 break;
    39                                 }
    40                                 else
    41                                 {
    42                                     Console.WriteLine("吼吼,请{0}向后退6格", PlayerNames[1 - number]);
    43                                     PlayerPos[1- number] -=6;
    44                                     ChangePos();
    45                                     break;
    46                                 }
    47                             }
    48                         case 3:Console.WriteLine("{0}踩到了时空隧道,前进10格", PlayerNames[number]);
    49                             PlayerPos[number] +=10;
    50                             ChangePos();
    51                             break;
    52                         case 4:
    53                             Console.WriteLine("{0}踩到了暂停格,暂停一局", PlayerNames[number]);
    54                             Jump[number] = true;
    55                         break;
    56                     }
    57             }
    58             Console.ReadKey();
    59             Console.Clear();
    60             DrawMap();
    61         }
    play(int number)

    通过参数number进行两玩家之间的切换,其中尤其要注意的是当玩家踩到暂停格,将bool类型的Jump数组该玩家位置设置为true,在主函数中判断Jump状态以决定是否进行该轮游戏。

  7. ChangePos() 越界控制
     1 public static void ChangePos()
     2         {
     3             if(PlayerPos[0]<0)
     4             {
     5                 PlayerPos[0] = 0;
     6             }
     7             if(PlayerPos[0]>=99)
     8             {
     9                 PlayerPos[0] = 99;
    10             }
    11             if (PlayerPos[1] < 0)
    12             {
    13                 PlayerPos[1] = 0;
    14             }
    15             if (PlayerPos[1] >= 99)
    16             {
    17                 PlayerPos[1] = 99;
    18             }
    19         }
    ChangePos()

    判断玩家的移动是否超出范围。

三、完整代码

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Threading.Tasks;
  6 
  7 namespace flyqi
  8 {
  9     class Program
 10     {
 11         static int[] Maps = new int[100];
 12         static int[] PlayerPos = new int[2];
 13         static string[] PlayerNames = new string[2];
 14         static bool[] Jump = new bool[2];
 15         static void Main(string[] args)
 16         {
 17             GameShow();
 18             InitialMap();
 19             Intname();
 20             Console.WriteLine("{0}的飞机为张", PlayerNames[0]);
 21             Console.WriteLine("{0}的飞机为韩", PlayerNames[1]);
 22             Console.Clear();
 23             DrawMap();
 24             while (PlayerPos[0] < 99 && PlayerPos[1] < 99)
 25             {
 26                 if (Jump[0] == false)
 27                     play(0);
 28                 else Jump[0] = false;
 29                 if (PlayerPos[0] == 99)
 30                 {
 31                     Console.ForegroundColor = ConsoleColor.Red;
 32                     Console.WriteLine("恭喜玩家{0}获得了胜利", PlayerNames[0]);
 33                     Console.ReadKey();
 34                     break;
 35                 }
 36 
 37                 if (Jump[1]==false)
 38                 play(1);
 39                 else Jump[1] = false;
 40                 if (PlayerPos[1] == 99)
 41                 {
 42                     Console.ForegroundColor = ConsoleColor.Blue;
 43                     Console.WriteLine("恭喜玩家{0}获得了胜利", PlayerNames[1]);
 44                     Console.ReadKey();
 45                     break;
 46                 }
 47                 
 48             }
 49                
 50             Console.ReadKey();
 51         }
 52         /// <summary>
 53         /// 游戏头部
 54         /// </summary>
 55         public static void GameShow()
 56         {
 57             Console.ForegroundColor = ConsoleColor.Red  ;
 58             Console.WriteLine("--------------------------------");
 59             Console.ForegroundColor = ConsoleColor.Yellow;
 60             Console.WriteLine("--------------------------------");
 61             Console.ForegroundColor = ConsoleColor.Green;
 62             Console.WriteLine("--------------------------------");
 63             Console.ForegroundColor = ConsoleColor.Cyan;
 64             Console.WriteLine("------#一个简单的飞行棋#--------");
 65             Console.ForegroundColor = ConsoleColor.Blue;
 66             Console.WriteLine("--------------------------------");
 67             Console.ForegroundColor = ConsoleColor.White;
 68             Console.WriteLine("--------------------------------");
 69             Console.ForegroundColor = ConsoleColor.Magenta;
 70             Console.WriteLine("--------------------------------");
 71             Console.WriteLine();
 72         }
 73         /// <summary>
 74         /// 初始化地图
 75         /// </summary>
 76         public static void InitialMap()
 77         {
 78             int[] luckyturn = {6,23,40,55,69,83};
 79             int[] landMine = { 5,13,17,33,38,50,64,80,94};
 80             int[] pause = { 9, 27, 60, 93 };
 81             int[] timeTunnel = { 20, 25, 45, 63, 72, 88, 90 };
 82             for(int i=0;i<luckyturn.Length;i++)
 83             {
 84                 Maps[luckyturn[i]] = 1;
 85             }
 86             for (int i = 0; i < landMine.Length; i++)
 87             {
 88                 Maps[landMine[i]] = 2;
 89             }
 90             for (int i = 0; i < pause.Length; i++)
 91             {
 92                 Maps[pause[i]] = 3;
 93             }
 94             for (int i = 0; i < timeTunnel.Length; i++)
 95             {
 96                 Maps[timeTunnel[i]] = 4;
 97             }
 98         }
 99         public static void DrawStep(int i)
100         {
101             if (PlayerPos[0] == PlayerPos[1] && PlayerPos[1] == i)
102             {
103                 Console.ForegroundColor = ConsoleColor.Green;
104                 Console.Write("&&");
105             }
106             else if (PlayerPos[0] == i)
107             {
108                 Console.ForegroundColor = ConsoleColor.Red;
109 
110                 Console.Write("");
111             }
112             else if (PlayerPos[1] == i)
113             {
114                 Console.ForegroundColor = ConsoleColor.Blue;
115 
116                 Console.Write("");
117             }
118             else
119             {
120                 Console.ForegroundColor = ConsoleColor.Yellow;
121                 switch (Maps[i])
122                 {
123                     case 0:
124                         Console.Write("");
125                         break;
126                     case 1:
127                         Console.Write("");
128                         break;
129                     case 2:
130                         Console.Write("");
131                         break;
132                     case 3:
133                         Console.Write("");
134                         break;
135                     case 4:
136                         Console.Write("");
137                         break;
138 
139                 }
140             }
141         }
142         public static void DrawMap()
143         {
144             Console.ForegroundColor = ConsoleColor.White;
145             Console.WriteLine("幸运轮盘⊙,地雷☆,暂停△,时空隧道□");
146             Console.WriteLine();
147             #region 第一行
148             for (int i=0;i<30;i++)
149             {
150                 DrawStep(i);
151                 #endregion
152             }
153             Console.WriteLine();
154             for (int i = 30; i < 35; i++)
155             {
156                 for (int J = 0; J < 29; J++)
157                 {
158                     Console.Write("  ");
159                 }
160                 DrawStep(i);
161                 Console.WriteLine();
162             }
163             for(int i=64;i>=35;i--)
164             {
165                 DrawStep(i);
166             }
167             Console.WriteLine();
168             for(int i=65;i<70;i++)
169             {
170                 DrawStep(i);
171                 Console.WriteLine();
172             }
173             for(int i=70;i<100;i++)
174             {
175                 DrawStep(i);
176             }
177             Console.WriteLine();
178             Console.WriteLine();
179         }
180         public static void Intname()
181         {
182             Console.ForegroundColor = ConsoleColor.White;
183             Console.WriteLine("请输入红方姓名:");
184             PlayerNames[0] = Console.ReadLine();
185             while(PlayerNames[0]=="")
186             {
187                 Console.WriteLine("玩家必须拥有姓名!");
188                 PlayerNames[0] = Console.ReadLine();
189             }
190             Console.WriteLine("请输入蓝方姓名:");
191             PlayerNames[1] = Console.ReadLine();
192             while (PlayerNames[1] == ""|| PlayerNames[1] == PlayerNames[0])
193             {
194                 if(PlayerNames[1] == "")
195                 Console.WriteLine("玩家必须拥有姓名!");
196                 if (PlayerNames[1] == PlayerNames[0])
197                 Console.WriteLine("不可以重名");
198                 PlayerNames[0] = Console.ReadLine();
199             }
200         }
201         public static void play(int number)
202         {
203             
204             Random ran = new Random();
205             int step = ran.Next(1,7);
206             Console.ForegroundColor = ConsoleColor.White;
207             Console.WriteLine("{0}按任意键扔色子", PlayerNames[number]);
208             Console.ReadKey(true);
209             Console.WriteLine("哇哦,{0}扔出了{1}!",PlayerNames[number],step);
210             PlayerPos[number] += step;
211             ChangePos();
212             Console.WriteLine("{0}前进了{1}格", PlayerNames[number],step);
213             if(PlayerPos[number] ==PlayerPos[1-number])
214             {
215                 Console.WriteLine("吼吼,{0}踩到了{1},{1}向后退六格",PlayerNames[number],PlayerNames[1-number]);
216                 PlayerPos[1- number] -= 6;
217                 ChangePos();
218 
219             }
220             else if(Maps[PlayerPos[number]] !=0)
221             {
222                     switch (Maps[PlayerPos[number]])
223                     {
224                         case 1: PlayerPos[number] -= 6;
225                             Console.WriteLine("吼吼,{0}踩到了地雷,向后退六格", PlayerNames[number]);
226                             break;
227                         case 2:
228                             Console.WriteLine("恭喜{0}踩到幸运罗盘", PlayerNames[number]);
229                             Console.WriteLine("{0},你现在有两个选择,1是交换你和{1}的位置,2是让{2}向后退6格",PlayerNames[number], PlayerNames[1-number],PlayerNames[1-number]);
230                             string input = Console.ReadLine();
231                             {
232                                 if(input=="1")
233                                 {
234                                     Console.WriteLine("吼吼,{0}选择和{1}交换位置",PlayerNames[number], PlayerNames[1 - number]);
235                                     int temp = PlayerPos[number];
236                                     PlayerPos[number] = PlayerPos[1- number];
237                                     PlayerPos[1- number] = temp;
238                                 break;
239                                 }
240                                 else
241                                 {
242                                     Console.WriteLine("吼吼,请{0}向后退6格", PlayerNames[1 - number]);
243                                     PlayerPos[1- number] -=6;
244                                     ChangePos();
245                                     break;
246                                 }
247                             }
248                         case 3:Console.WriteLine("{0}踩到了时空隧道,前进10格", PlayerNames[number]);
249                             PlayerPos[number] +=10;
250                             ChangePos();
251                             break;
252                         case 4:
253                             Console.WriteLine("{0}踩到了暂停格,暂停一局", PlayerNames[number]);
254                             Jump[number] = true;
255                         break;
256                     }
257             }
258             Console.ReadKey();
259             Console.Clear();
260             DrawMap();
261         }
262         public static void ChangePos()
263         {
264             if(PlayerPos[0]<0)
265             {
266                 PlayerPos[0] = 0;
267             }
268             if(PlayerPos[0]>=99)
269             {
270                 PlayerPos[0] = 99;
271             }
272             if (PlayerPos[1] < 0)
273             {
274                 PlayerPos[1] = 0;
275             }
276             if (PlayerPos[1] >= 99)
277             {
278                 PlayerPos[1] = 99;
279             }
280         }
281 
282     }
283 }
all in

 

posted @ 2021-10-11 18:52  问就是啥也不会  阅读(415)  评论(0)    收藏  举报