服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - c# 编写的简单飞行棋游戏

c# 编写的简单飞行棋游戏

2022-11-22 12:13静态类 C#

这个简单的飞行棋游戏主要是讲的方法怎么应用,充分的去理解方法和方法的调用。整体收获还是很大的。感兴趣的朋友可以参考下

项目效果

c# 编写的简单飞行棋游戏

c# 编写的简单飞行棋游戏

实现代码

?
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
using System;
 
namespace 飞行棋项目
{
    class Program
    {
        ///1、画游戏头
        ///2、初始化地图
        ///把整数数组中数字编程控制台中显示的特殊字符显示的过程,就是初始化地图
        ///3、画地图
        ///4、玩游戏
        //我们用静态数组用来模拟全局变量,这个数组代表地图长度以及地图坐标
        public static int[] Maps = new int[100];
        //声明一个静态数组用来存储玩家A跟玩家B的坐标
        public static int[] PlayerPos = new int[2];
        //存储两个玩家的姓名
        public static string[] PlayerName = new string[2];
 
        public static bool[] Flag = new bool[2];
 
        static void Main(string[] args)
        {
 
            TitleName();
            #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
                {
                    Console.WriteLine("玩家B的姓名不能和玩家A的相同,请重新输入");
                    PlayerName[1] = Console.ReadLine();
                }
            }
            #endregion
            //玩家姓名输入正确之后
            Console.Clear();//清屏功能
            TitleName();
            Console.WriteLine("{0}玩家用A表示\n{1}玩家用B表示", PlayerName[0], PlayerName[1]);
            //在画地图之前首先要初始化地图
            InitailMap();
            DrawMap();
            //当玩家A跟玩家B没有一个人在终点的时候,两个玩家就不停的去玩游戏
            while (PlayerPos[0] < 99 && PlayerPos[1] < 99)
            {
                if (Flag[0] == false)
                {
                    PlayGame(0);
                }
                else
                {
                    Flag[0] = false;
                }
                if(PlayerPos [0]>=99)
                {
                    Console.WriteLine("玩家{0}赢了", PlayerName[0]);
                    break;
                }
                if (Flag[1]==false)
                {
                    PlayGame(1);
                }
                else
                {
                    Flag[1] = false;
                }
                if(PlayerPos[1] >= 99)
                {
                    Console.WriteLine("玩家{0}赢了", PlayerName[1]);
                    break;
                }
            }//while
            Console.ReadKey();
 
 
        }
        /// <summary>
        /// 画游戏头
        /// </summary>
        public static void TitleName()
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("******************");
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.WriteLine("******************");
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("******************");
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("****飞行棋项目****");
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("******************");
            Console.ForegroundColor = ConsoleColor.Gray;
            Console.WriteLine("******************");
            Console.ForegroundColor = ConsoleColor.DarkMagenta;
            Console.WriteLine("******************");
        }
        /// <summary>
        /// 将不同的值赋予不同的特殊符号,方便画出地图
        /// </summary>
        public static void InitailMap()
        {
            int[] luckyturn = { 6, 23, 38, 45, 78, 90 };//幸运轮盘
            for (int i = 0; i < luckyturn.Length; i++)
            {
                Maps[luckyturn[i]] = 1;
            }
            int[] landline = { 5, 22, 31, 44, 75, 87, 92 };//地雷
            for (int i = 0; i < landline.Length; i++)
            {
                Maps[landline[i]] = 2;
            }
            int[] pause = { 7, 15, 25, 46, 56, 84 };//暂停
            for (int i = 0; i < pause.Length; i++)
            {
                Maps[pause[i]] = 3;
            }
            int[] timeTunnel = { 9, 18, 29, 37, 42, 63, 72, 88, 96 };//时空隧道
            for (int i = 0; i < timeTunnel.Length; i++)
            {
                Maps[timeTunnel[i]] = 4;
            }
        }
        /// <summary>
        /// 画地图
        /// </summary>
        public static void DrawMap()
        {
            Console.WriteLine("图例:幸运轮盘:▲  地雷:☆  暂停:○  时空隧道:¥");
            #region 第一横行
            //第一横行
            for (int i = 0; i < 30; i++)
            {
                Console.Write(DrawStringMap(i));//直接调用画地图的那个方法
            }//for
            #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();
        }
        #region 从画地图的方法中抽象出来的一种方法
        /// <summary>
        /// 从画地图的方法中抽象出来的一种方法
        /// </summary>
        /// <param name="i">传参进来的一个变量</param>
        /// <returns></returns>
        public static string DrawStringMap(int i)
        {
            //画图
            string str = "";
            if (PlayerPos[0] == PlayerPos[1] && PlayerPos[0] == i)
            {
                str = "<>";
            }
            else if (PlayerPos[0] == i)
            {
                str = "A";
            }
            else if (PlayerPos[1] == i)
            {
                str = "B";
            }
            else
            {
                switch (Maps[i])
                {
                    case 0:
                        Console.ForegroundColor = ConsoleColor.Red;
                        str = "□";
                        break;
                    case 1:
                        Console.ForegroundColor = ConsoleColor.Green;
                        str = "▲";
                        break;
                    case 2:
                        Console.ForegroundColor = ConsoleColor.Gray;
                        str = "☆";
                        break;
                    case 3:
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        str = "○";
                        break;
                    case 4:
                        Console.ForegroundColor = ConsoleColor.Blue;
                        str = "¥";
                        break;
                }//switch
            }//if
            return str;
        }
        #endregion
        /// <summary>
        /// 玩游戏
        /// </summary>
        /// <param name="playerNumber">传递进来的一个参数来判定是谁在玩游戏</param>
        public static void PlayGame(int playerNumber)
        {
            Random r = new Random();//设定随机数
            int rNumber = r.Next(1, 7);//限定随机数范围在1-7之间
            Console.WriteLine("{0}按下任意键开始掷骰子", PlayerName[playerNumber]);
            Console.ReadKey(true);
            Console.WriteLine("{0}掷出了{1}", PlayerName[playerNumber], rNumber);//掷出了随机数1-7之间
            PlayerPos[playerNumber] += rNumber;//玩家坐标加上掷出的随机数
            ChangePos();//在每一个玩家坐标变化的地方都要进行一次限制,限制玩家坐标在地图0-99之内
            Console.ReadKey(true);
            if (PlayerPos[playerNumber] == PlayerPos[1 - playerNumber])
            {
                Console.WriteLine("玩家{0}踩到了玩家{1},玩家{2}退6格", PlayerName[playerNumber], PlayerName[1 - playerNumber], PlayerName[1 - playerNumber]);
                PlayerPos[1 - playerNumber] -= 6;
                ChangePos();
                Console.ReadKey(true);
            }
            else
            {
                switch (Maps[PlayerPos[playerNumber]])
                {
                    case 0:
                        Console.WriteLine("玩家{0}踩到了方块,啥都没有", PlayerName[playerNumber]);
                        Console.ReadKey(true);
                        break;
                    case 1:
                        Console.WriteLine("玩家{0}踩到了幸运轮盘,请选择1--交换位置,2--轰炸对方", PlayerName[playerNumber]);
                        Console.ReadKey(true);
                        string input = Console.ReadLine();
                        while (true)
                        {
                            if (input == "1")
                            {
                                Console.WriteLine("玩家{0}跟玩家{1}交换位置", PlayerName[playerNumber], PlayerName[1 - playerNumber]);
                                Console.ReadKey(true);
                                int temp = PlayerPos[playerNumber];
                                PlayerPos[playerNumber] = PlayerPos[1 - playerNumber];
                                PlayerPos[1 - playerNumber] = temp;
                                ChangePos();
                                Console.WriteLine("交换完成,按任意键继续游戏");
                                Console.ReadKey(true);
                                break;
                            }
                            else if (input == "2")
                            {
                                Console.WriteLine("玩家{0}选择轰炸玩家{1},玩家{2}退6格", PlayerName[playerNumber], PlayerName[1 - playerNumber], PlayerName[1 - playerNumber]);
                                Console.ReadKey(true);
                                PlayerPos[1 - playerNumber] -= 6;
                                ChangePos();
                                Console.WriteLine("玩家{0}退了6格,按任意键继续游戏", PlayerName[1 - playerNumber]);
                                Console.ReadKey(true);
                                break;
                            }
                            else
                            {
                                Console.WriteLine("你输入的文本有误,请重新输入!");
                                input=Console.ReadLine();
                                Console.ReadKey(true);
                            }
                        }
                        break;
                    case 2:
                        Console.WriteLine("玩家{0}踩到了地雷,退6格", PlayerName[playerNumber]);
                        Console.ReadKey(true);
                        PlayerPos[playerNumber] -= 6;
                        ChangePos();
                        Console.ReadKey(true);
                        break;
                    case 3:
                        Console.WriteLine("玩家{0}踩到了暂停,暂停一回合", PlayerName[playerNumber]);
                        Flag[playerNumber] = true;
                        Console.ReadKey(true);
                        break;
                    case 4:
                        Console.WriteLine("玩家{0}踩到了时空隧道,前进10格", PlayerName[playerNumber]);
                        Console.ReadKey(true);
                        PlayerPos[playerNumber] += 10;
                        ChangePos();
                        Console.ReadKey(true);
                        break;
 
                }//switch
 
            }//else
            ChangePos();//perfect
            Console.Clear();
            DrawMap();
        }
        /// <summary>
        /// 当玩家坐标发生改变的时候调用这个方法,用于限制玩家坐标在0-99之间
        /// </summary>
        public static void ChangePos()
        {
            if (PlayerPos[0] < 0)
            {
                PlayerPos[0] = 0;
            }
            if (PlayerPos[0] >= 99)
            {
                PlayerPos[0] = 99;
            }
            if (PlayerPos[1] < 0)
            {
                PlayerPos[1] = 0;
            }
            if (PlayerPos[1] >= 99)
            {
                PlayerPos[1] = 99;
            }
        }
    }
}

基于winform制作的图形界面程序

效果

c# 编写的简单飞行棋游戏

代码

?
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
using System;
using System.Drawing;
using System.Windows.Forms;
 
namespace 飞行棋
{
 public partial class Form1 : Form
 {
  public Form1()
  {
   InitializeComponent();
  }
  //创建一个数组装游戏地板
  int[] mapList = new int[390];
  //创建装图片的数组
  PictureBox[] mappic = new PictureBox[390];
  //创建路的数组
  int[] road = new int[100];
  //创建地图
  Panel map = new Panel();
  int size = 30;
  //创建骰子
  PictureBox dice = new PictureBox();
  //初始位置的停放区域红色的
  Panel plan1 = new Panel();
  //初始位置的停放区域绿色的
  Panel plan2 = new Panel();
  private void Form1_Load(object sender, EventArgs e)
  {
   this.Size = new Size(1200, 600);
   this.FormBorderStyle = FormBorderStyle.FixedSingle;
   this.Location = new Point(250, 100);
   this.BackColor = Color.Wheat;
   map.Size = new Size(30*size, 13*size);
   map.BorderStyle = BorderStyle.FixedSingle;
   map.Location = new Point(40,160);
   this.Controls.Add(map);
   //显示图片的方法
   Init();
 
    
   plan1.Size = new Size(100, 100);
   plan1.Location = new Point(map.Left, map.Top - 120);
   plan1.BackgroundImage = Image.FromFile("../../img/circle.png");
   plan1.BackgroundImageLayout = ImageLayout.Stretch;
   this.Controls.Add(plan1);
    
   plan2.Size = new Size(100, 100);
   plan2.Location = new Point(map.Left+120 ,map.Top - 120);
   plan2.BackgroundImage = Image.FromFile("../../img/circle.png");
   plan2.BackgroundImageLayout = ImageLayout.Stretch;
   this.Controls.Add(plan2);
 
   PictureBox redPlayer = new PictureBox();
   redPlayer.Size = new Size(80, 80);
   redPlayer.Image = Image.FromFile("../../img/red.png");
   redPlayer.Location = new Point(10, 10);
   redPlayer.SizeMode = PictureBoxSizeMode.StretchImage;
   plan1.Controls.Add(redPlayer);
 
   PictureBox greenPlayer = new PictureBox();
   greenPlayer.Size = new Size(80, 80);
   greenPlayer.Image = Image.FromFile("../../img/green.png");
   greenPlayer.Location = new Point(10, 10);
   greenPlayer.SizeMode = PictureBoxSizeMode.StretchImage;
   plan2.Controls.Add(greenPlayer);
 
   //创建对话框
   tall.Size = new Size(200, 500);
   tall.Location = new Point(map.Right + 20, 50);
   tall.ReadOnly = true;
   this.Controls.Add(tall);
   //玩家一:
   name.Size = new Size(100, 30);
   name.Location = new Point(45,10);
   this.Controls.Add(name);
   //玩家二:
   names.Size = new Size(100, 30);
   names.Location = new Point(160,10 );
   this.Controls.Add(names);
   btn.Location = new Point(300,10);
   btn.Text = "开始游戏";
   btn.Size = new Size(100, 30);
   btn.BackColor = Color.Pink;
   btn.Click += Btn_Click;
   this.Controls.Add(btn);
 
   //骰子
   dice.Size = new Size(80, 80);
   dice.Image = Image.FromFile("../../img/roll.png");
   dice.Location = new Point(map.Right-160,map.Top-120);
   dice.SizeMode = PictureBoxSizeMode.StretchImage;
   this.Controls.Add(dice);
   dice.MouseClick += Dice_MouseClick;
    
    
  }
 
  private void Btn_Click(object sender, EventArgs e)
  {
 
   //创建初始的弹框
   Tall(string.Format("请两人投掷骰子,{0}先手,点大的先走",name.Text));
    
   playName[0] = name.Text;
   playName[1] = names.Text;
   tall.Focus();
  }
 
  TextBox name = new TextBox();
  TextBox names = new TextBox();
  Button btn = new Button();
   
  //创键记录的对话框
  RichTextBox tall = new RichTextBox();
  //创建随机
  Random r = new Random();
  // 轮流投掷骰子 0代表红方 1代表绿方
  bool[] playStart = new bool[2] { true, false };
  //记录双方当前的位置
  int[] playPosition = new int[2] { -1, -1 };
  int[] playStand = new int[2] { -1, -1 };
  // 记录骰子的点数
  int[] shaizi = new int[2];
  string[] playName = new string[2];
  //数组存储点数
  int[] num = new int[2] ;
  //1.轮流投掷筛子决定谁先出门
  private void Dice_MouseClick(object sender, MouseEventArgs e)
  {
   PlayDice();
   PlayGame();
 
 
  }
  private void PlayDice()
  {
   //红方投掷 红方为true 投掷完之后变为false 绿方投掷
   if (playStart[0])
   {
    shaizi[0] = r.Next(1,7);
    Tall(String.Format("{1}投掷出{0}点", shaizi[0],playName[0]));
    playStart[0] = !playStart[0];
 
   }
   else
   {
    playStart[0] = !playStart[0];
   }
   //绿方投掷 绿方为false 绿方投掷完之后变为true .轮到红方投掷
   if (playStart[1])
   {
    shaizi[1] = r.Next(1, 7);
    Tall(String.Format("{1}投掷出{0}点", shaizi[1],playName[1]));
    playStart[1] = !playStart[1];
 
   }
   else
   {
    playStart[1] = !playStart[1];
   }
  }
  //决定游戏谁先手
  private void OutDoor()
  {
   //红方为true 绿方为false的时候一回合完了 一回合完之后对应的骰子数都不为0
   if (playStart[0]&&!playStart[1]&&(shaizi[0]!=0||shaizi[1]!=0))
   {
    if (shaizi[0] == shaizi[1])
    {
     Tall("双方点数相同,请重新投掷");
    }
    else
    {
     //第二回合
     st = false;
     if (shaizi[0] > shaizi[1])
     {
      Tall(String.Format("{0}点数较大,先行一步,{0}投掷",name.Text));
      playStart[0] = true;
      playStart[1] = false;
 
     }
     if (shaizi[0] < shaizi[1])
     {
      Tall(String.Format("{0}点数较大,先行一步,{0}投掷", names.Text));
      playStart[0] = false;
      playStart[1] = true;
     }
    }
   }
 
  }
 
 
  bool st = true;
  //控制游戏谁先走
  private void PlayGame()
  {
   //判断游戏刚开始时候比点数先行
   if (st == true)
   {
    OutDoor();
    //都为false的时候绿方投掷
    if (!playStart[0]&&playStart[1])
    {
     Tall(string.Format("请{0}投掷",names.Text));
    }//都为true的时候红方投掷
    else if(playStart[0]&&!playStart[1])
    {
     Tall(string.Format("请{0}投掷!",name.Text));
    }
   }
   else
   {
    if (playStart[0] && !playStart[1])//如果绿方大, 绿方为true
    {
     PlayReturn(1);
    }//红方
    else if (!playStart[0] && playStart[1])
    {
     PlayReturn(0);
    }
   }
 
  }
  /// <summary>
  /// 双方轮流游戏
  /// </summary>
  /// 传入参数index0为红方 1为绿方
  bool[] re = new bool[2] { false, false };
  private void PlayReturn(int index)
  {
   //都没出门
   if (playPosition[index] == -1)
   {
 
    switch (shaizi[index])
    {
     case 2:
     case 4:
      Tall(String.Format("{0}可以起步", playName[index]));
      playPosition[index] = 0;
      playStand[index] = 0;
      //如果两个位置相等
      if (playPosition[1] == playPosition[0])
      {
       mappic[road[playPosition[index]]].Image = imageList1.Images[2];
      }
      else
      {
       mappic[road[playPosition[index]]].Image = imageList1.Images[index];
      }
      break;
     case 6:
      playStart[index] = true;
      playStart[1 - index] = false;
      Tall(String.Format("{0}可以起步", playName[index]));
      playPosition[index] = 0;
      playStand[index] = 0;
      if (playPosition[1] == playPosition[0])
      {
       mappic[road[playPosition[index]]].Image = imageList1.Images[2];
      }
      else
      {
       mappic[road[playPosition[index]]].Image = imageList1.Images[index];
      }
      Tall(String.Format("请{0}投掷骰子", playName[index]));
      break;
     default:
      Tall(String.Format("很遗憾,{0}投掷出{1}点无法起步,轮到{2}投掷", playName[index], shaizi[index], playName[1 - index]));
      break;
    }
    if (playPosition[0] != -1)
    {
     plan1.Controls.Clear();
    }
    if (playPosition[1] != -1)
    {
     plan2.Controls.Clear();
    }
   }
   else
   {
    //改变位置之前记录好之前的位置
    playStand[index] = playPosition[index];
    playPosition[index] += shaizi[index];
    if (playPosition[index] >= 99)
    {
     MessageBox.Show(playName[index] + "获胜");
     playPosition[index] = 99;
     //改变图片
     Change(index);
     return;
    }
    Tall(string.Format("{0}移动了{1}步", playName[index], shaizi[index]));
    //改变图片
    Change(index);
 
    //判断移动完成之后的位置是如何
    if (playPosition[index] == playPosition[1 - index])
    {
     playPosition[1 - index] = 0;
     playStand[1 - index] = playPosition[1 - index];
     Tall(String.Format("厉害!{0}精准的将{1}踩回原点,{0}当前的位置是{2},{1}当前的位置是{3},", playName[index], playName[1 - index], playPosition[index], playPosition[1 - index]));
     mappic[road[playPosition[index]]].Image = imageList1.Images[index];
     mappic[road[playPosition[1 - index]]].Image = imageList1.Images[1 - index];
     Tall(string.Format("{0}开始投掷", playName[1 - index]));
    }
 
    switch (mapList[road[playPosition[index]]])
    {
     case 1:
      Tall(string.Format("{0}安全到达!当前位置是{1}", playName[index], playPosition[index]));
      Tall(String.Format("{0}开始投掷!", playName[1-index]));
      break;
     case 2:
      Tall(string.Format("很不幸,{0}踩中了香蕉皮,后退6步,当前位置是{1}", playName[index], playPosition[index]));
      playStand[index] = playPosition[index];
      playPosition[index] -= 6;
      Change(index);
      /*Tall(string.Format("{0}当前位置是{1}", playName[index], playPosition[index]));*/
      Tall(string.Format("{0}开始投掷", playName[1 - index]));
      break;
     case 3:
      Tall(String.Format("恭喜!{0}踩中时空隧道,前进6步!当前位置是{1}", playName[index], playPosition[index]));
      playStand[index] = playPosition[index];
      playPosition[index] += 6;
      Change(index);
      /*Tall(string.Format("{0}当前位置是{1}", playName[index], playPosition[index]));*/
      Tall(string.Format("{0}开始投掷", playName[1 - index]));
      break;
     case 4:
      Tall(string.Format("好可惜,{0}踩中了陷阱,暂停一回合", playName[index]));
      re[index] = true;
      re[1 - index] =false;
      break;
     case 5:
      Tall(string.Format("真好,{0}踩中幸运星,在玩一回合!当前位置是{1}", playName[index], playPosition[index]));
      playStart[index] = true;
      playStart[1 - index] = false;
      Tall(string.Format("{0}继续投掷!", playName[index]));
      break;
     case 6:
      Tall(string.Format("真好!{0}踩中秘籍,请选择措施!当前位置是{1}", playName[index], playPosition[index]));
      DialogResult result = MessageBox.Show("是否与对方更换位置!", "移魂大法", MessageBoxButtons.YesNo);
      if (result == DialogResult.Yes)
      {
       int temp = playPosition[index];
       playPosition[index] = playPosition[1 - index];
       playPosition[1 - index] = temp;
       playStand[index] = playPosition[index];
       playStand[1 - index] = playPosition[1 - index];
       mappic[road[playPosition[index]]].Image = imageList1.Images[index];
       mappic[road[playPosition[1 - index]]].Image = imageList1.Images[1 - index];
 
      }
      Tall(string.Format("{0}当前位置是{1},{2}的位置是{3}", playName[index], playPosition[index], playName[1 - index], playPosition[1 - index]));
      Tall(string.Format("{0}开始投掷。", playName[1 - index]));
      break;
     case 7:
      Tall(string.Format("幸运!{0}获得手枪,可选择击退对方3步!当前位置是{1}", playName[index], playPosition[index]));
      DialogResult res = MessageBox.Show("是否选择击退对方三步!", "手枪!", MessageBoxButtons.YesNo);
      if (res == DialogResult.Yes)
      {
       playStand[1 - index] = playPosition[1 - index];
       playPosition[1 - index] -= 3;
       mappic[road[playPosition[1 - index]]].Image = imageList1.Images[1 - index];
       Change(1 - index);
      }
      /* Tall(string.Format("{0}被击退对方3步!当前位置是{1}", playName[1 - index], playPosition[1 - index]));*/
      Tall(string.Format("{0}开始投掷。", playName[1 - index]));
      break;
     default:
      break;
    }
    if (re[index] && !re[1 - index])
    {
     playStart[index] = true;
     playStart[1 - index] = false;
     re[index] = false;
     re[1 - index] = false;
    }
 
   }
  }
 
  private void Change( int index)
  {
   //如果移动完之后再同一个位置
   if (playPosition[1] == playPosition[0])
   {
    mappic[road[playPosition[index]]].Image = imageList1.Images[2];
   }
   else
   {//移动完成之后显示对应玩家的图片
    mappic[road[playPosition[index]]].Image = imageList1.Images[index];
   }
   //原本位置图片的显示,如果两人在同一个位置站着,并且都在路上自己离开之后,留下对方的图片在原地在起点的时候
   if (playStand[0]==playStand[1]&&playStand[0]!=-1&&playStand[1]!=-1&&playPosition[1-index]==0)
   {
    mappic[road[playStand[index]]].Image = imageList1.Images[1 - index];
    mappic[road[playPosition[index]]].Image = imageList1.Images[ index];
   }
   else //如果两人不再同一位置判断之前的脚下是什么
   {
    switch (mapList[road[playStand[index]]])
    {
     //整个地图的图片
     case 0:
      mappic[road[playStand[index]]].Image = Image.FromFile("../../img/water.gif");
      break;
     //游戏区域的路
     case 1:
      mappic[road[playStand[index]]].Image = Image.FromFile("../../img/grass.png");
      break;
     case 2:
      mappic[road[playStand[index]]].Image = Image.FromFile("../../img/sk.jpg");
      break;
     case 3:
      mappic[road[playStand[index]]].Image = Image.FromFile("../../img/xj.jpg");
      break;
     case 4:
      mappic[road[playStand[index]]].Image = Image.FromFile("../../img/xianjing.jpg");
      break;
     case 5:
      mappic[road[playStand[index]]].Image = Image.FromFile("../../img/xx.jpg");
      break;
     case 6:
      mappic[road[playStand[index]]].Image = Image.FromFile("../../img/jh.jpg");
      break;
     case 7:
      mappic[road[playStand[index]]].Image = Image.FromFile("../../img/sq.jpg");
      break;
     case 10:
      mappic[road[playStand[index]]].Image = Image.FromFile("../../img/start.png");
      break;
     case 11:
      mappic[road[playStand[index]]].Image = Image.FromFile("../../img/end.bmp");
      break;
     
    }
   }
  }
 
  void Tall(string str)
  {
   MessageBox.Show(str);
   tall.AppendText(str+"\r\n");
  }
 
  //创建一个显示所有图片的方法
  void Init()
  {
   //先调用地图的
   CreateMap();
   //在调用道具的 先有地图再有道具
   CreateGear();
   for (int i = 0; i < mapList.Length; i++)
   {
    //创建图片每循环一次创建一个
    PictureBox pic = new PictureBox();
    //图片的大小等于30
    pic.Size = new Size(size, size);
    //判断mapList索引对应的东西
    switch (mapList[i])
    {
     //整个地图的图片
     case 0:
     pic.Image = Image.FromFile("../../img/water.gif");
     break;
     //游戏区域的路
     case 1:
     pic.Image = Image.FromFile("../../img/grass.png");
     break;
     case 2:
     pic.Image = Image.FromFile("../../img/sk.jpg");
     break;
     case 3:
     pic.Image = Image.FromFile("../../img/xj.jpg");
     break;
     case 4:
     pic.Image = Image.FromFile("../../img/xianjing.jpg");
     break;
     case 5:
     pic.Image = Image.FromFile("../../img/xx.jpg");
     break;
     case 6:
     pic.Image = Image.FromFile("../../img/jh.jpg");
     break;
     case 7:
     pic.Image = Image.FromFile("../../img/sq.jpg");
     break;
     case 10:
     pic.Image = Image.FromFile("../../img/start.png");
     break;
     case 11:
     pic.Image = Image.FromFile("../../img/end.bmp");
     break;
 
    }
    //拉伸图片
    pic.SizeMode = PictureBoxSizeMode.StretchImage;
    mappic[i] = pic;
    //算出图片的坐标
    pic.Left = i % 30 * size;
    pic.Top = i / 30 * size;
    map.Controls.Add(pic);
   }
  }
  //给整个地图添加图片
  void CreateMap()
  {
   //调用铺路的方法
   CreateRoad();
   for (int i = 0; i < road.Length; i++)
   {
    mapList[road[i]] = 1;
   }
   //起始图片的索引位置
   mapList[0] = 10;
   //结束图片对应的索引位置
   mapList[mapList.Length - 1] = 11;
  }
  //算出路怎么铺
  void CreateRoad()
  {
   //111111
   //  1
   //111111
   //1
   //111111
   //第一行铺的路30个
   for (int i = 0; i < 30; i++)
   {
    road[i] = i;
   }
   //第2个列的路
   for (int i = 30; i <= 35; i++)
   {
    road[i] = road[i - 1] + 30;
   }
   //第三个路
   for (int i = 36; i <65; i++)
   {
    road[i] = road[i - 1] - 1;
   }
   //第4列的路
   for (int i = 65; i <=70; i++)
   {
    road[i] = road[i - 1] + 30;
   }
   //第五行的数
   for (int i =71; i <100; i++)
   {
    road[i] = road[i - 1] + 1;
   }
  }
 
  //定义道具的数组
  int[] back = { 7, 27, 42, 62, 73, 96 };
  int[] forword = { 10, 25, 33, 65, 80, 88 };
  int[] stop = { 3, 20, 35, 50, 60, 70, 90 };
  int[] star = { 5, 28, 45, 71, 85 };
  int[] change = { 4, 55, 75, 98 };
  int[] gun = { 11, 32, 66, 83 };
  //定义一个道具放置位置的方法
  void CreateGear()
  {
   for (int i = 0; i < back.Length; i++)
   {
    //将地图对应的路然后将索引换成对应的道具
    mapList[road[back[i]]] = 2;
   }
   for (int i = 0; i < forword.Length; i++)
   {
    mapList[road[forword[i]]] = 3;
   }
   for (int i = 0; i < stop.Length; i++)
   {
    mapList[road[stop[i]]] = 4;
   }
   for (int i = 0; i <star.Length; i++)
   {
    mapList[road[star[i]]] = 5;
   }
   for (int i = 0; i < change.Length; i++)
   {
    mapList[road[change[i]]] = 6;
   }
   for (int i = 0; i < gun.Length; i++)
   {
    mapList[road[gun[i]]] = 7;
   }
  }
 }
}

以上就是c# 编写的简单飞行棋游戏的详细内容,更多关于c# 飞行棋游戏的资料请关注服务器之家其它相关文章!

原文链接:https://www.cnblogs.com/May1184958246/p/14902376.html

延伸 · 阅读

精彩推荐
  • C#C#开发微信门户及应用(2) 微信消息处理和应答

    C#开发微信门户及应用(2) 微信消息处理和应答

    文章主要为大家详细介绍了C#开发微信门户及应用第二篇,微信消息处理和应答,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    伍华聪11512022-01-07
  • C#C#编程自学之类和对象

    C#编程自学之类和对象

    C#一种面向对象的编程语言,是专门为.NET应用而开发出的语言。在.NET运行库的支持下,.NET框架的各种优点在C#中表现得淋漓尽致。所以学习类和对象以及如...

    C#教程网10662021-10-28
  • C#C# 判断时间段是否相交的实现方法

    C# 判断时间段是否相交的实现方法

    这篇文章主要介绍了C# 判断时间段是否相交的实现方法的相关资料,希望通过本文能帮助到大家,让大家实现这样的功能,需要的朋友可以参考下...

    _iorilan9352022-01-25
  • C#C#深浅拷贝的深入解析

    C#深浅拷贝的深入解析

    这篇文章主要给大家介绍了关于C#深浅拷贝的深入解析,文中通过示例代码介绍的非常详细,对大家的学习或者使用C#具有一定的参考学习价值,需要的朋友...

    小世界的野孩子4762022-08-09
  • C#c# 基于任务的异步编程模式(TAP)

    c# 基于任务的异步编程模式(TAP)

    这篇文章主要介绍了c# 基于任务的异步编程模式(TAP)的相关资料,帮助大家更好的理解和学习c# 异步编程的相关知识,感兴趣的朋友可以了解下...

    一只独行的猿9522022-10-13
  • C#C# 实现简单打印的实例代码

    C# 实现简单打印的实例代码

    C# 实现简单打印的实例代码,需要的朋友可以参考一下...

    C#教程网3602020-12-18
  • C#C#中ValueTuple的原理详解

    C#中ValueTuple的原理详解

    C# 7.0已经出来一段时间了,大家都知道新特性里面有个对元组的优化:ValueTuple,下面这篇文章主要给大家介绍了关于C#中ValueTuple原理的相关资料,需要的朋...

    lindexi9972022-02-24
  • C#关于finalize机制和引用、引用队列的用法详解

    关于finalize机制和引用、引用队列的用法详解

    下面小编就为大家带来一篇关于finalize机制和引用、引用队列的用法详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来...

    C#教程网5062021-12-06