当前位置:   article > 正文

C#小游戏:飞行棋_c#飞行棋代码及运行结果

c#飞行棋代码及运行结果

最近在学习c#在看:B站的这个视频
在学到飞行棋时先自己写了一个。下面是运行结果:
这里是游戏开始的玩家输入,一共两名玩家
游戏运行中
游戏结束

我也是新手代码还有很多不完善的地方,但目前功能还没发生什么错误,大家可以来玩一下。

 using System;

namespace 飞行棋
{
    class Program
    {
        public struct Player {
            public int _playlocation;
            public string _playerName;
            public int _playValue;
        }

        static void Main(string[] args)
        {

            GameShow();
            Console.ForegroundColor = ConsoleColor.White;
            int[] map = new int[200];// □
            int[] luckturn = {1,20,61,84,90,999};// ◎幸运转盘位置数组  因为自己写的方法的原因,这里几个数组得从大到小排列,最后一个起限制作用
            int[] bomb = {6,24,27,44,80,98,999};//● 炸弹位置数组
            int[] transfer = {5,11,30,36,77,83,999};//∩ 传送门位置数组 传送门比较特殊,相关代码就要调用该数组。而方法之间存在相互关联,因此有些方法也要传入该数组
            int[] stop = { 12, 31, 59, 66, 88,999};//▽ 暂停位置数组 
            MapValue(map, luckturn, bomb, transfer, stop);
            MapPaint(map);
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine("请输入玩家A的姓名");
            Player playerA = PlayerIni();
            Console.WriteLine("初始化成功!\n玩家A的姓名是:{0}\n\n请输入玩家B的姓名", playerA._playerName);
            Player playerB = PlayerIni();
            Console.WriteLine("玩家B的昵称是{0}\n按任意键开始游戏:", playerB._playerName);
            Console.ReadKey();
            Console.Clear();
            playerB._playValue = 200;//方便区分玩家A、B
            Player winner = GameStart(map,playerA,playerB,transfer);//主要运行的内容都在这个方法里了
            Console.WriteLine("游戏结束玩家{0}胜利!", winner._playerName);
            GameEnd(winner);
        }
        public static void playerActionMove(int[] map,ref Player x,ref Player y,int[] transfer)//玩家行动 x代表当前行动玩家 y代表另一位玩家
        {
            Console.ForegroundColor = ConsoleColor.White;
            map[x._playlocation] -= x._playValue;
            Console.WriteLine("按任意键,玩家{0}投色子",x._playerName);
            Console.ReadKey();
            Console.Clear();
            Random r = new Random();
            int rNumber = r.Next(1, 7);
            Console.WriteLine("玩家{0}掷出了{1}向前移动了{2}格\n",x._playerName,rNumber,rNumber);
            x._playlocation += rNumber;
            map[x._playlocation] += x._playValue;
            PlayerTread(map,ref x,ref y);
            while (true)
            {             
                if (map[x._playlocation] == x._playValue + 1)//这边本来打算用switch——case的 但不能 case x.playlocation(我也不知道为啥)就没用。
                {
                    MapFunctionLuck(map,ref x, ref y);
                    continue;
                }
                if (map[x._playlocation] == x._playValue + 2)
                {
                    MapFunctionBomb(map, ref x);
                    continue;
                }
                break;
            }
            MapFunctionTransfer(map, ref x,transfer);
        }
        public static Player GameStart(int[] map,Player x,Player y,int[] transfer)
        {
            map[0] = 300;
            MapPaint(map);
            while (true)
            {
                if(!MapFuctionStop(map,x))
                {
                    playerActionMove(map, ref x, ref y, transfer);
                    if (x._playlocation >= 99)
                    {
                        return x;
                    }
                    MapPaint(map);
                }
                if (!MapFuctionStop(map,y))
                {
                    playerActionMove(map, ref y, ref x, transfer);
                    if (y._playlocation >= 99)
                    {
                        return y;
                    }
                    MapPaint(map);
                }
            }
        }//游戏开始 
        public static void MapFunctionLuck(int[] map, ref Player x,ref Player y)
        {
            Console.WriteLine("恭喜你踩到了幸运转盘,你可以选择\n1.让自己前进随机1-6格\n2.让对方后退随机1-6格");
            string s = Console.ReadLine();
            Random r = new Random();
            int rNumber = r.Next(1, 7);
            if (s == "1")
            {
                map[x._playlocation] -= x._playValue;
                Console.WriteLine("干得漂亮,你向前移动了{0}格",rNumber);
                x._playlocation += rNumber;
                map[x._playlocation] += x._playValue;
            }
            else if (s == "2")
            {
                map[y._playlocation] -= (y._playValue+1);
                Console.WriteLine("芜湖~对方向后移动了{0}", rNumber);
                y._playlocation -= rNumber;
                map[y._playlocation] += y._playValue;
            }
            else if (s == "李杨太帅了")
            {
                x._playlocation += 100;
                Console.WriteLine("说的好!一看就是赢的料!");
            }
            else
            {
                Console.WriteLine("输入错误,请重新输入");
                MapFunctionLuck(map,ref x,ref y);
            }
        }//幸运轮盘代码 让玩家选择 1.自己前进1-6格 2.让对方后退1-6格
        public static void MapFunctionBomb(int[] map,ref Player x)//炸弹代码 让踩到的玩家后退三格
        {
            map[x._playlocation] -= x._playValue;
            x._playlocation -= 3;
            map[x._playlocation] += x._playValue;
            Console.WriteLine("玩家{0}被炸弹炸退了三格", x._playerName);
        }
        public static void MapFunctionTransfer(int[] map, ref Player x, int[] transfer)//传送门代码  让玩家传送到下一个传送门上 传送门记得一个玩家不能踩一个来回移动
        {
            if (map[x._playlocation] == x._playValue + 3)
            {
                for (int i = 0; i < transfer.Length - 1; i++)
                {
                    if (transfer[i] == x._playlocation)
                    {
                        if (i == 5)
                        {
                            map[x._playlocation] -= x._playValue;
                            x._playlocation = transfer[i - 1];
                            map[x._playlocation] += x._playValue;
                            Console.WriteLine("一股神秘力量将你带回了上一个传送门");
                            break;
                        }
                        map[x._playlocation] -= x._playValue;
                        x._playlocation = transfer[i + 1];
                        map[x._playlocation] += x._playValue;
                        Console.WriteLine("你成功传送到了下一个传送门");
                        break;
                    }
                }
            }
        }
        public static bool MapFuctionStop(int[] map,Player x)//暂停代码 让玩家暂停一回合 目前还没想清楚怎么让它跳过这一回合,只能改为踩过之后暂停这个方块就消失了
        {
            bool isStop = false;
            if (map[x._playlocation] == x._playValue + 4)
            {
                isStop = true;
                map[x._playlocation] -= 4;
            }
            return isStop;
            Console.WriteLine("你被暂停啦");
        }
        public static void PlayerTread(int[] map, ref Player x,ref Player y)//一名玩家踩到另一名玩家导致其后退6格
        {
            if(x._playlocation == y._playlocation)
            {
                map[y._playlocation] -= y._playValue;
                y._playlocation -= 6;
                if(y._playlocation < 0) //防止在前六格踩到超出地图边界
                {
                    y._playlocation = 0;
                }
                map[y._playlocation] += y._playValue;
                Console.WriteLine("漂亮!{0}踩到了{1},使{2}后退了六格", x._playerName, y._playerName, y._playerName);
            }
        }
        public static Player PlayerIni()
        {
            Player player = new Player();
            player._playlocation = 0;
            player._playerName = Console.ReadLine();
            player._playValue = 100;
            return player;
        }//初始化玩家
        public static void MapPaint(int[] map)
        {
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.WriteLine("□:表示空格  ◎:表示幸运转盘  ●:表示炸弹  ∩:表示传送门  ▽:表示暂停  A、B:表示玩家  ‖:表示两玩家在同一位置");
            Console.ForegroundColor = ConsoleColor.Cyan;
            string s1 = "";
            for(int i = 0;i < 30; i++)//这里我还不知道Console.write()可以不自动换行输出,因此改变了我整个代码的结构,这里我分了五段分别赋给s1让它输出五遍得到完整地图
            {
                switch (map[i]){
                    case 0:s1 += "□"; break;
                    case 1:s1 += "◎"; break;
                    case 2:s1 += "●"; break;
                    case 3:s1 += "∩"; break;
                    case 4:s1 += "▽"; break;
                    case 100: case 101:case 102:case 103:case 104: s1 += "A";break;
                    case 200: case 201:case 202:case 203:case 204: s1 += "B";break;
                    case 300: case 302:case 304:case 306:case 308: s1 += "‖";break;
                }
            }
            Console.WriteLine(s1);
            s1 = "";
            for(int i = 30; i < 35; i++)
            {
                switch (map[i])
                {
                    case 0: s1 = "□"; break;
                    case 1: s1 = "◎"; break;
                    case 2: s1 = "●"; break;
                    case 3: s1 = "∩"; break;
                    case 4: s1 = "▽"; break;
                    case 100: case 101: case 102: case 103: case 104: s1 = "A"; break;
                    case 200: case 201: case 202: case 203: case 204: s1 = "B"; break;
                    case 300: case 302: case 304: case 306: case 308: s1 = "‖"; break;
                }
                Console.WriteLine("                                                          {0}",s1);
            }
            s1 = "";
            for (int i = 64; i > 34; i--)
            {
                switch (map[i])
                {
                    case 0: s1 += "□"; break;
                    case 1: s1 += "◎"; break;
                    case 2: s1 += "●"; break;
                    case 3: s1 += "∩"; break;
                    case 4: s1 += "▽"; break;
                    case 100: case 101: case 102: case 103: case 104: s1 += "A"; break;
                    case 200: case 201: case 202: case 203: case 204: s1 += "B"; break;
                    case 300: case 302: case 304: case 306: case 308: s1 += "‖"; break;
                }
            }
            Console.WriteLine(s1);
            s1 = "";
            for (int i = 65; i < 70; i++)
            {
                switch (map[i])
                {
                    case 0: s1 = "□"; break;
                    case 1: s1 = "◎"; break;
                    case 2: s1 = "●"; break;
                    case 3: s1 = "∩"; break;
                    case 4: s1 = "▽"; break;
                    case 100: case 101: case 102: case 103: case 104: s1 = "A"; break;
                    case 200: case 201: case 202: case 203: case 204: s1 = "B"; break;
                    case 300: case 302: case 304: case 306: case 308: s1 = "‖"; break;
                }
                Console.WriteLine("{0}", s1);
            }
            s1 = ""; for (int i = 70; i < 100; i++)
            {
                switch (map[i])
                {
                    case 0: s1 += "□"; break;
                    case 1: s1 += "◎"; break;
                    case 2: s1 += "●"; break;
                    case 3: s1 += "∩"; break;
                    case 4: s1 += "▽"; break;
                    case 100: case 101: case 102: case 103: case 104: s1 += "A"; break;
                    case 200: case 201: case 202: case 203: case 204: s1 += "B"; break;
                    case 300: case 302: case 304: case 306: case 308: s1 += "‖"; break;
                }
            }
            Console.WriteLine(s1);
        }//地图绘制
        public static void MapValue(int[] map,int[] luckturn,int[] bomb,int[] transfer,int[] stop)//给map赋值用
        {
            int l = 0, b = 0, t = 0, s = 0;
            for(int i =0;i < map.Length; i++)
            {
                if(i == luckturn[l])
                {
                    map[i] = 1;
                    l++;
                }
                if(i == bomb[b])
                {
                    map[i] = 2;
                    b++;
                }
                if(i == transfer[t])
                {
                    map[i] = 3;
                    t++;
                }
                if(i == stop[s])
                {
                    map[i] = 4;
                    s++;
                }
            }
        }
        public static void GameShow()//游戏头
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("******************************");
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("******************************");
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("*****飞行棋游戏,芜湖!*******");
            Console.ForegroundColor = ConsoleColor.DarkBlue;
            Console.WriteLine("******************************");
            Console.ForegroundColor = ConsoleColor.DarkGray;
            Console.WriteLine("******************************");
        }
        public static void GameEnd(Player winner)//游戏尾
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("******************************");
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("******************************");
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("*****恭喜{0}获得胜利!********",winner._playerName);
            Console.ForegroundColor = ConsoleColor.DarkBlue;
            Console.WriteLine("******************************");
            Console.ForegroundColor = ConsoleColor.DarkGray;
            Console.WriteLine("******************************");
        }
    }
}

  • 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
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/546614
推荐阅读
相关标签
  

闽ICP备14008679号