Home [C# 筆記] Game 飛行棋
Post
Cancel

[C# 筆記] Game 飛行棋

跟B站老趙學飛行棋

遊戲規則:
如果玩家A踩到了玩家B 玩家B退6格
踩到了地雷 退6格
踩到了時空隧道 進10格
踩到了幸運輪盤 1.交換位置 2.轟炸對方 使對方退6格
踩到了暫停 暫停一回合
踩到了方塊 神馬都不幹

  1. 畫遊戲頭
  2. 初始化地圖(加載地圖所需要的資源)
  3. 畫地圖
  4. 玩遊戰

1.畫遊戲頭

1
2
3
4
5
6
7
8
9
10
11
12
static void ShowGame() {
    Console.ForegroundColor = ConsoleColor.Yellow; //設置前景顏色(文字會變色)
    Console.WriteLine("*******************");
    Console.ForegroundColor = ConsoleColor.Blue;
    Console.WriteLine("*******************");
    Console.ForegroundColor = ConsoleColor.Red;
    Console.WriteLine("*******飛行棋******");
    Console.ForegroundColor = ConsoleColor.DarkGreen;
    Console.WriteLine("*******************");
    Console.ForegroundColor = ConsoleColor.Cyan;
    Console.WriteLine("*******************");
}

2.初始化地圖(加載地圖所需要的資源)

初始化地圖後,會有5種值:01234
Maps[i]=0; //畫方塊 Maps[i]=1; //畫幸運轉盤
Maps[i]=2; //畫地雷
Maps[i]=3; //暫停▲
Maps[i]=4; //時空隨隊隧道卍

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/// <summary>
/// 初始化地圖:設置5種數值0-4
/// </summary>
public static void InitialMap()
{
    int[] luckyTurn = { 6, 23, 40, 55, 69, 83 }; //幸運轉盤◎
    for (int i = 0; i < luckyTurn.Length; i++) {
        //int index = luckyTurn[i];
        Maps[luckyTurn[i]] = 1;
    }

    int[] landMine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 }; //地電☆
    for (int i = 0; i < landMine.Length; i++) {
        Maps[landMine[i]] = 2;
    }

    int[] pause = { 9, 20, 60, 93 }; //暫停▲
    for (int i = 0; i < pause.Length; i++) {
        Maps[pause[i]] = 3;
    }

    int[] timeTunnel = { 20, 25, 45, 63, 72, 88, 90 }; //時空隨隊隧道卍
    for (int i = 0; i < timeTunnel.Length; i++) {
        Maps[timeTunnel[i]] = 4;
    }
}

3.畫地圖

畫地圖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/// <summary>
/// 畫地圖
/// </summary>
public static void DrawMap()
{
    #region 畫一行-每一個有30
    for (int i = 0; i < 30; i++)
    {
        Console.Write(DrawStringMap(i));
    }
    #endregion

    //畫完第一行,應該換行
    Console.WriteLine();
    #region 畫一豎
    for (int i = 30; i < 35; i++)
    {
        for (int j = 0; j <= 28; j++)
        {
            Console.Write("  "); //畫空格
        }
        Console.Write(DrawStringMap(i));
        Console.WriteLine();
    }
    #endregion
}

從畫地圖中抽出一個方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/// <summary>
/// 從畫地圖中抽出一個方法
/// </summary>
/// <param name="i"></param>
public static string DrawStringMap(int i)
{
    string str = "";
    #region 畫圖
    //如果玩家A玩家B坐標相同,並且都在這個地圖上,就顯示<>
    //PlayerPostion[0] == i 確保有一個玩家在地圖上
    if (PlayerPostion[0] == PlayerPostion[1] && PlayerPostion[0] == i)
    {
        str = "<>";//玩家A 玩家B位置相同
    } else if (PlayerPostion[0] == i)
    {
        str = "A"; //玩家A位置
    } else if (PlayerPostion[1] == i)
    {
        str = "B"; //玩家B位置
    } else
    {
        switch (Maps[i])
        {
            case 0:
                Console.ForegroundColor = ConsoleColor.Yellow;
                str = "□";
                break;
            case 1:
                Console.ForegroundColor = ConsoleColor.Green;
                str = "◎";
                break;
            case 2:
                Console.ForegroundColor = ConsoleColor.Blue;
                str = "☆";
                break;
            case 3:
                Console.ForegroundColor = ConsoleColor.Red;
                str = "▲";
                break;
            case 4:
                Console.ForegroundColor = ConsoleColor.Magenta;
                str = "卍";
                break;
        }//switch
    }//else
    #endregion
    return str;
}

畫完完整地圖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/// <summary>
/// 畫地圖
/// </summary>
public static void DrawMap()
{
    #region 畫第一橫行-每一個有30
    for (int i = 0; i < 30; i++)
    {
        Console.Write(DrawStringMap(i));
    }
    #endregion

    //畫完第一行,應該換行
    Console.WriteLine();

    #region 畫第一豎
    for (int i = 30; i < 35; i++)
    {
        for (int j = 0; j <= 28; j++) {
            Console.Write("  "); //畫空格
        }
        Console.Write(DrawStringMap(i));
        Console.WriteLine();
    }
    #endregion

    #region 畫第二橫行
    for (int i = 64; i >= 35; i--) {
        Console.Write(DrawStringMap(i));
    }
    #endregion

    //畫完第二行,應該換行
    Console.WriteLine();

    #region 畫第二豎
    for (int i = 65; i <= 69; i++) {
        Console.WriteLine(DrawStringMap(i));
    }
    #endregion

    #region 畫第三橫行
    for (int i = 70; i <= 99; i++) {
        Console.Write(DrawStringMap(i));
    }
    #endregion
}

4.玩家姓名

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# region 輸入玩家姓名
Console.WriteLine("請輸入玩家A的姓名:");
PlayerName[0] = Console.ReadLine()!;

while (PlayerName[0] == "")
{
    Console.WriteLine("玩家A的姓名不能為空,請重新輸入");
    PlayerName[0] = Console.ReadLine()!;
}

Console.WriteLine("請輸入玩家B的姓名:");
PlayerName[1] = Console.ReadLine()!;

while (PlayerName[1] == "" || PlayerName[1] == PlayerName[0])
{
    if (PlayerName[1] == "")
    {
        Console.WriteLine("玩家B的姓名不能為空,請重新輸入");
        PlayerName[1] = Console.ReadLine()!;
    } else if (PlayerName[1] == PlayerName[0])
    {

        Console.WriteLine("玩家B的姓名不能和玩家A一樣,請重新輸入");
        PlayerName[1] = Console.ReadLine()!;
    }
}
#endregion

5.玩遊戲之前的準備

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//玩家姓名輸入ok後,我們首先要清屏
Console.Clear(); //清屏

ShowGame(); //畫遊戲頭
Console.WriteLine($"{PlayerName[0]}的士兵用A表示");
Console.WriteLine($"{PlayerName[1]}的士兵用B表示");


/// <summary>
/// 畫地圖
/// </summary>
public static void DrawMap()
{
    Console.WriteLine("圖例:幸運轉盤◎ 地電☆  暫停▲ 時空隨隊隧道卍");
    ....

    //畫完之後要換行
    Console.WriteLine();
}

完整Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
class Program
{
    static int[] Maps = new int[100]; //用靜態字段來模擬全域變數
    static int[] PlayerPostion = new int[2]; //用靜態變數來記錄玩家坐標AB
    static string[] PlayerName = new string[2]; //儲存兩個玩家的姓名
    static bool[] Flags = new bool[2]; //兩個玩家的標記。默認是false

    static void Main(string[] args)
    {
        ShowGame(); //畫遊戲頭
        # region 輸入玩家姓名
        Console.WriteLine("請輸入玩家A的姓名:");
        PlayerName[0] = Console.ReadLine();

        while (PlayerName[0] == "")
        {
            Console.WriteLine("玩家A的姓名不能為空,請重新輸入");
            PlayerName[0] = Console.ReadLine();
        }

        Console.WriteLine("請輸入玩家B的姓名:");
        PlayerName[1] = Console.ReadLine();

        while (PlayerName[1] == "" || PlayerName[1] == PlayerName[0])
        {
            if (PlayerName[1] == "")
            {
                Console.WriteLine("玩家B的姓名不能為空,請重新輸入");
                PlayerName[1] = Console.ReadLine()!;
            } else if (PlayerName[1] == PlayerName[0])
            {

                Console.WriteLine("玩家B的姓名不能和玩家A一樣,請重新輸入");
                PlayerName[1] = Console.ReadLine()!;
            }
        }
        #endregion

        //玩家姓名輸入ok後,我們首先要清屏
        Console.Clear(); //清屏

        ShowGame(); //畫遊戲頭
        Console.WriteLine($"{PlayerName[0]}的士兵用A表示");
        Console.WriteLine($"{PlayerName[1]}的士兵用B表示");

        //在畫地圖前,要先初始化地圖
        InitialMap(); //初始化地圖
        DrawMap(); //畫地圖

        //當玩家A玩家B沒有一個人在終點的時候,兩個玩家不停的玩遊戰
        while (PlayerPostion[0] < 99 && PlayerPostion[1] < 99)
        {
            //玩家A
            if (Flags[0] == false)
            {
                PlayGame(0); //玩遊戲
            } else
            {
                Flags[0] = false; //如果踩到暫停,要能夠在下一回合可以玩,設為false
            }
            if (PlayerPostion[0] >= 99)
            {
                Console.WriteLine($"玩家{PlayerName[0]}Win了玩家{PlayerName[1]}");
                break;
            }
            //玩家B
            if (Flags[1] == false)
            {
                PlayGame(1); //玩遊戲
            } else
            {
                Flags[1] = false; //如果踩到暫停,要能夠在下一回合可以玩,設為false
            }
            if (PlayerPostion[1] >= 99)
            {
                Console.WriteLine($"玩家{PlayerName[1]}Win了玩家{PlayerName[0]}");
                break;
            }
        }//while

        Win();
        Console.ReadKey();
    }
    /// <summary>
    /// 畫遊戲頭
    /// </summary>
    public static void ShowGame()
    {
        Console.ForegroundColor = ConsoleColor.Yellow;
        Console.WriteLine("*******************");
        Console.ForegroundColor = ConsoleColor.Blue;
        Console.WriteLine("*******************");
        Console.ForegroundColor = ConsoleColor.Red;
        Console.WriteLine("*******飛行棋******");
        Console.ForegroundColor = ConsoleColor.DarkGreen;
        Console.WriteLine("*******************");
        Console.ForegroundColor = ConsoleColor.Cyan;
        Console.WriteLine("*******************");
    }
    /// <summary>
    /// 初始化地圖:設置5種數值0-4
    /// </summary>
    public static void InitialMap()
    {
        int[] luckyTurn = { 6, 23, 40, 55, 69, 83 }; //幸運轉盤◎
        for (int i = 0; i < luckyTurn.Length; i++)
        {
            //int index = luckyTurn[i];
            Maps[luckyTurn[i]] = 1;
        }

        int[] landMine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 }; //地電☆
        for (int i = 0; i < landMine.Length; i++)
        {
            Maps[landMine[i]] = 2;
        }

        int[] pause = { 9, 20, 60, 93, 1, 2, 3, 4, 5, 7 }; //暫停▲
        for (int i = 0; i < pause.Length; i++)
        {
            Maps[pause[i]] = 3;
        }

        int[] timeTunnel = { 20, 25, 45, 63, 72, 88, 90 }; //時空隨隊隧道卍
        for (int i = 0; i < timeTunnel.Length; i++)
        {
            Maps[timeTunnel[i]] = 4;
        }
    }

    /// <summary>
    /// 畫地圖
    /// </summary>
    public static void DrawMap()
    {
        Console.WriteLine("圖例:幸運轉盤◎ 地電☆  暫停▲ 時空隨隊隧道卍");

        #region 畫第一橫行-每一個有30
        for (int i = 0; i < 30; i++)
        {
            Console.Write(DrawStringMap(i));
        }
        #endregion

        //畫完第一行,應該換行
        Console.WriteLine();

        #region 畫第一豎
        for (int i = 30; i < 35; i++)
        {
            for (int j = 0; j <= 28; j++)
            {
                Console.Write("  "); //畫空格
            }
            Console.Write(DrawStringMap(i));
            Console.WriteLine();
        }
        #endregion

        #region 畫第二橫行
        for (int i = 64; i >= 35; i--)
        {
            Console.Write(DrawStringMap(i));
        }
        #endregion

        //畫完第二行,應該換行
        Console.WriteLine();

        #region 畫第二豎
        for (int i = 65; i <= 69; i++)
        {
            Console.WriteLine(DrawStringMap(i));
        }
        #endregion

        #region 畫第三橫行
        for (int i = 70; i <= 99; i++)
        {
            Console.Write(DrawStringMap(i));
        }
        #endregion

        //畫完之後要換行
        Console.WriteLine();
    }

    /// <summary>
    /// 從畫地圖中抽出一個方法
    /// </summary>
    /// <param name="i"></param>
    public static string DrawStringMap(int i)
    {
        string str = "";
        #region 畫圖
        //如果玩家A玩家B坐標相同,並且都在這個地圖上,就顯示<>
        //PlayerPostion[0] == i 確保有一個玩家在地圖上
        if (PlayerPostion[0] == PlayerPostion[1] && PlayerPostion[0] == i)
        {
            str = "<>";//玩家A 玩家B位置相同
        } else if (PlayerPostion[0] == i)
        {
            str = "A"; //玩家A位置
        } else if (PlayerPostion[1] == i)
        {
            str = "B"; //玩家B位置
        } else
        {
            switch (Maps[i])
            {
                case 0:
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    str = "□";
                    break;
                case 1:
                    Console.ForegroundColor = ConsoleColor.Green;
                    str = "◎";
                    break;
                case 2:
                    Console.ForegroundColor = ConsoleColor.Blue;
                    str = "☆";
                    break;
                case 3:
                    Console.ForegroundColor = ConsoleColor.Red;
                    str = "▲";
                    break;
                case 4:
                    Console.ForegroundColor = ConsoleColor.Magenta;
                    str = "卍";
                    break;
            }//switch
        }//else
        #endregion
        return str;
    }

    /// <summary>
    /// 玩遊戲
    /// </summary>
    public static void PlayGame(int playerNumber)
    {
        Random rnd = new Random();
        int random = rnd.Next(1, 7);//1-6

        Console.WriteLine($"{PlayerName[playerNumber]}按任意鍵開始擲骰子");
        Console.ReadKey(true); //true隱藏按下的鍵
        Console.WriteLine($"{PlayerName[playerNumber]}擲出{random}");
        PlayerPostion[playerNumber] += random;
        ChangesPostion();
        Console.ReadKey(true);
        Console.WriteLine($"{PlayerName[playerNumber]}按任意鍵開始行動");
        Console.ReadKey(true);
        Console.WriteLine($"{PlayerName[playerNumber]}行動完了");
        Console.ReadKey(true);

        //玩家A有可能踩到了玩家B 地電 時空隧道  幸運輪盤 方塊 暫停
        if (PlayerPostion[playerNumber] == PlayerPostion[1 - playerNumber])
        {
            Console.WriteLine($"玩家{PlayerName[playerNumber]}踩到了玩家{PlayerName[1 - playerNumber]}, 玩家{PlayerName[1 - playerNumber]}退6格");
            PlayerPostion[1 - playerNumber] -= 6;
            ChangesPostion();
            Console.ReadKey(true);
        } else //踩到了關卡
        {
            //玩家的坐標
            switch (Maps[PlayerPostion[playerNumber]])
            {
                case 0:
                    Console.WriteLine($"玩家{PlayerName[playerNumber]}踩到了方塊,安全");
                    Console.ReadKey(true);
                    break;
                case 1:
                    Console.WriteLine($"玩家{PlayerName[playerNumber]}踩到幸運轉盤,請選擇:1.交換位置2.轟炸對方");
                    string input = Console.ReadLine();
                    while (true)
                    {
                        if (input == "1")
                        {
                            Console.WriteLine($"玩家{PlayerName[playerNumber]}跟玩家{PlayerName[1 - playerNumber]}交換位置");
                            Console.ReadKey(true);
                            int temp = PlayerPostion[playerNumber];
                            PlayerPostion[0] = PlayerPostion[1];
                            PlayerPostion[1] = temp;
                            Console.WriteLine("交換位置完成,請按任意鍵");
                            Console.ReadKey(true);
                            break;
                        } else if (input == "2")
                        {
                            Console.WriteLine($"玩家{PlayerName[playerNumber]}選擇轟炸玩家,玩家{PlayerName[1 - playerNumber]}退6格");
                            Console.ReadKey(true);
                            PlayerPostion[1 - playerNumber] -= 6;
                            ChangesPostion();
                            Console.WriteLine($"玩家{PlayerName[1 - playerNumber]}退6格");
                            Console.ReadKey(true);
                            break;
                        } else
                        {
                            Console.WriteLine("只能輸入1或2:1.交換位置2.轟炸對方");
                            input = Console.ReadLine();
                        }
                    }
                    break;
                case 2:
                    Console.WriteLine($"玩家{PlayerName[playerNumber]}踩到地雷,退6格");
                    PlayerPostion[playerNumber] -= 6;
                    ChangesPostion();
                    Console.ReadKey(true);
                    break;
                case 3:
                    Console.WriteLine($"玩家{PlayerName[playerNumber]}踩到了暫停,暫停一回");
                    Flags[playerNumber] = true;
                    Console.ReadKey(true);
                    break;
                case 4:
                    Console.WriteLine($"玩家{PlayerName[playerNumber]}踩時空隧道 進10格");
                    PlayerPostion[playerNumber] += 10;
                    ChangesPostion();
                    Console.ReadKey(true);
                    break;
            }//switch
        }//else
        ChangesPostion(); //perfect
        Console.Clear(); //清屏
        DrawMap();//重畫地圖
    }

    /// <summary>
    /// 當玩家坐標改變的時候調用
    /// </summary>
    public static void ChangesPostion()
    {

        if (PlayerPostion[0] < 0)
        {
            PlayerPostion[0] = 0;
        }
        if (PlayerPostion[0] > 99)
        {
            PlayerPostion[0] = 99;
        }
        if (PlayerPostion[1] < 0)
        {
            PlayerPostion[1] = 0;
        }
        if (PlayerPostion[1] > 99)
        {
            PlayerPostion[1] = 99;
        }
    }

    /// <summary>
    /// 勝利之後的圖案顯示
    /// </summary>
    public static void Win()
    {
        Console.ForegroundColor = ConsoleColor.Red;
        Console.WriteLine("          ■■         ■■");
        Console.WriteLine("■■■■■ ■■ ■■     ■■■■■■■    ■■");
        Console.WriteLine("■■■■■ ■■ ■■     ■■■■■      ■■     ■■");
        Console.WriteLine("■■ ■■ ■■■■■■■■     ■■   ■■ ■■     ■■");
        Console.ForegroundColor = ConsoleColor.Blue;
        Console.WriteLine("■■ ■■ ■■■■■■■■     ■■   ■■ ■■     ■■");
        Console.WriteLine("■■■■■■■  ■■    ■■■■■■■■ ■■ ■■     ■■");
        Console.WriteLine("■■ ■■    ■■    ■■■■■■■■ ■■ ■■     ■■");
        Console.WriteLine("■■ ■■  ■■■■■■     ■■■   ■■ ■■     ■■");
        Console.WriteLine("■■■■■  ■■■■■■    ■■■■■  ■■ ■■     ■■");
        Console.ForegroundColor = ConsoleColor.Green;
        Console.WriteLine("■■ ■■    ■■     ■■ ■■■■ ■■ ■■");
        Console.WriteLine("■■ ■■    ■■    ■■■ ■■ ■ ■■ ■■");
        Console.WriteLine("■■ ■■    ■■     ■  ■■      ■■     ■■");
        Console.ForegroundColor = ConsoleColor.Cyan;
        Console.WriteLine("■■ ■■ ■■■■■■■■     ■■      ■■     ■■");
        Console.WriteLine("■■ ■■■ ■■■■■■■■    ■■       ■■           ■");
        Console.WriteLine("■■ ■■              ■■       ■■");
    }
}

用c#实现飞行棋小游戏

This post is licensed under CC BY 4.0 by the author.