您的位置:首页 > 编程语言 > C#

【C#】骑士飞行棋

2015-12-06 15:03 405 查看
学习C#已经有一段时间了,来给大家简单的介绍一下C#。

1、.NET与C#的概念

.net:一般指.NETFramework框架。一种平台,一种技术

C#:一种编程语言,可以开发基于.net平台的编程语言。

2、现在用《骑士飞行棋》这一个小的游戏来总结一下我这阶段所学习的内容。


(1)游戏头

<span style="font-size:18px;"> public static void ShowUI()
        {
            Console.WriteLine("***********************");
            Console.WriteLine("*                     *");
            Console.WriteLine("* 终极骑士飞行棋 10.18 *");
            Console.WriteLine("*                     *");
            Console.WriteLine("***********************");
        }</span>


(2)初始画地图

<span style="font-size:18px;">  public static void InitMap()
        {
            //初始化地图
            //我用0表示普通,显示给用户就是□
            //....1....幸运轮盘,显示组用户就是◎
            //....2....地雷,显示给用户就是☆
            //....3....暂停,显示给用户就是▲
            //....4....时空隧道,显示组用户就是卐
            int[] luckyturn = { 6, 23, 40, 55, 69, 83 };//幸运转盘◎
            int[] landMine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 };//地雷☆
            int[] pause = { 9, 27, 60, 93 };//暂停▲
            int[] timeTunnel = { 20, 25, 45, 63, 72, 88, 90 };//时空隧道卐

            //把数组中下标为6,23,40,55,69,83的地方的值改为1
            for (int i = 0; i < luckyturn.Length; i++)
            {
                //int temp = luckyturn[i];
                //Map[ temp]= 1;
                Map[luckyturn[i]] = 1;
            }
            for (int i = 0; i < landMine.Length; i++)
            {
                Map[landMine[i]] = 2;
            }
            for (int i = 0; i < pause.Length; i++)
            {
                Map[pause[i]] = 3;
            }
            for (int i = 0; i < timeTunnel.Length; i++)
            {
                Map[timeTunnel[i]] = 4;
            }
        }</span>

(3)画地图

先定义一个方法DrawStringMap()也是画地图的第一行

<span style="font-size:18px;">public static string  DrawStringMap(int pos)
        {
            string temp = "";
            #region  画地图第一行的逻辑 
            if (PlayerPos[0] == PlayerPos[1] && PlayerPos[0] == pos)
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    temp = "<>";
                }
                else if (PlayerPos[0] == pos)//如果玩家A在地图上就画A
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    temp = "A";
                }
                else if (PlayerPos[1] == pos)//如果玩家B在地图上就画B
               {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    temp = "B";
                }
                else
                {
                    switch (Map[pos])//如果玩家A和玩家B不再一起也不再这个坐标上就画显示的地图图标
                    {
                        case 0:
                            Console.ForegroundColor = ConsoleColor.Gray  ;
                            temp = "□";
                            break;
                        case 1:
                            Console.ForegroundColor = ConsoleColor.Red ;
                            temp = "◎";
                            break;
                        case 2:
                            Console.ForegroundColor = ConsoleColor.Blue ;
                            temp = "☆";
                            break;
                        case 3:
                            Console.ForegroundColor = ConsoleColor.Green ;
                            temp = "▲";
                            break;
                        case 4:
                            Console.ForegroundColor = ConsoleColor.Magenta ;
                            temp = "卐";
                            break;
                    }//end switch
                }//end else

            return temp;
            #endregion
        }</span>
画地图第一竖行

<span style="font-size:18px;">   #region 画地图第一竖行
            for (int i = 30; i <= 34; i++)
            {
                for (int j = 0; j <=28; j++)
                {
                    Console.Write("  ");
                }
                Console.WriteLine(DrawStringMap(i));</span>
(4)提示用户输入姓名

<span style="font-size:18px;"> Console.WriteLine("请输入玩家A的姓名");
            PlayerNames[0] = Console.ReadLine();

            while (PlayerNames[0]=="")
            {
                Console.WriteLine("玩家A的姓名不能为空,请重新输入");
                PlayerNames[0] = Console.ReadLine();
            }
           
            Console.WriteLine("请输入玩家B的姓名");
            PlayerNames[1] = Console.ReadLine();
            while (PlayerNames [1]==PlayerNames [0] ||PlayerNames [1]=="")
            {
                if (PlayerNames[1] == PlayerNames[0])
                {
                    //玩家B的姓名为空或者玩家B的姓名和玩家A的姓名相同
                    Console.WriteLine("玩家B的姓名和玩家A的姓名{0}相同",PlayerNames [0]);
                }
                else
                {
                    Console.WriteLine("玩家B的姓名为空,请重新输入");
                }
            }</span>

(5)关卡键的设置

<span style="font-size:18px;">public static void RowTouzi(int playerPos)
        {
            Random r = new Random();
            int num = r.Next(1, 7);
            string msg = "";
            Console.WriteLine("{0}按任意键开始掷骰子", PlayerNames[playerPos]);
            Console.ReadKey(true);
            Console.WriteLine("{0}掷出了{1}", PlayerNames[playerPos], num );
            Console.WriteLine("{0}按任意键开始行动。。。", PlayerNames[playerPos]);
            ConsoleKeyInfo c = Console.ReadKey(true);
           
            Console.ReadKey(true);//按任意键看是行动
            PlayerPos[playerPos] += num ;
            CheckPos();
            if (PlayerPos[playerPos] == PlayerPos[1- playerPos])
            {
                msg = string.Format("玩家{0}踩到了玩家{1},玩家{2}退6格",PlayerNames [playerPos ],PlayerNames [1-playerPos ],PlayerNames [1-playerPos ]);
                PlayerPos[1- playerPos] -= 6;
                CheckPos();

            }
            else
            {
                switch (Map[PlayerPos[playerPos]])
                {
                    case 0: msg = "行动完了"; break;
                    case 1:
                        msg = string.Format("{0}走到了幸运转盘,请选择1---交换位置 ,2----轰炸对方", PlayerNames[playerPos]);
                        int number = ReadInt(msg, 1, 2);
                        if (number == 1)
                        {
                            int temp = 0;
                            temp = PlayerPos[playerPos];
                            PlayerPos[playerPos] = PlayerPos[1- playerPos];
                            PlayerPos[1- playerPos] = temp;
                            msg = string.Format("玩家{0}选择了与玩家{1}交换了位置", PlayerNames[playerPos], PlayerNames[1- playerPos]);
                        }
                        else
                        {
                            PlayerPos[1- playerPos] = 0;
                            msg = string.Format("玩家{0}选择轰炸玩家{1}", PlayerNames[playerPos], PlayerNames[1- playerPos]);
                        }

                        break;
                    case 2:
                        //踩到地雷了
                        msg ="踩到地雷了,退6格";
                        PlayerPos[playerPos] -= 6;
                        CheckPos();
                        break;
                    case 3:
                       msg ="踩到暂停了";
                        flag[playerPos] = true; 
                        break;
                    case 4:
                       msg ="踩到时空隧道了,前进10步";
                        PlayerPos[playerPos] += 10;
                        CheckPos();
                        break;

                }
            }
            Console.Clear();
            DrawMap();
        }</span>


好了今天就写到这里了。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: