首页 > 学院 > 开发设计 > 正文

C#编写飞行棋游戏

2019-11-06 06:53:56
字体:
来源:转载
供稿:网友
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Drawing;//绘图namespace PlaneDemo3{    class PRogram    {        public static string[] playerName=new string[2];//定义玩家的名称的数组        public static int[] Maps=new int[100];//定义整型的地图数组        public static int[] playerPos = new int[2];//定义玩家坐标        public static bool[] flags=new bool[2];//定义标识符        static void Main(string[] args)        {            GameHead();            #region 玩家输入            Console.WriteLine("请输入第一位玩家的昵称:");            playerName[0] = Console.ReadLine();            while (playerName[0]=="")            {                Console.WriteLine("用户名不能为空,请重新输入.");                playerName[0] = Console.ReadLine();            }            Console.WriteLine("请输入第二位玩家的昵称:");            playerName[1] = Console.ReadLine();            while (playerName[1]==playerName[0]||playerName[1]=="")            {                if (playerName[1]==playerName[0])                {                    Console.WriteLine("昵称重复出现,请重新输入.");                    playerName[1] = Console.ReadLine();                }                else                {                    Console.WriteLine("用户名不能为空,请重新输入.");                    playerName[1] = Console.ReadLine();                }            }            #endregion            Console.Clear();//清空            GameHead();            Console.WriteLine("游戏开始啦");            Console.WriteLine("我们用A代表第一位玩家{0}",playerName[0]);            Console.WriteLine("我们用B代表第二位玩家{0}", playerName[1]);            Console.ReadKey(true);            InitialMap();//初始化地图            drawMap();//画地图            #region 依据游戏规则开始            while (playerPos[0]<99||playerPos[1]<99)            {                #region 玩家1                if (flags[0]==false)                {                    PlayGame(0);                }                else                {                    flags[0] = false;                }                if (playerPos[0]>=99)                {                    Console.WriteLine("玩家{0}最终获得了胜利!!!",playerName[0]);                    break;                }                #endregion                #region 玩家2                if (flags[1]==false)                {                    PlayGame(1);                }                else                {                    flags[1] = false;                }                if (playerPos[1] >= 99)                {                    Console.WriteLine("玩家{0}最终获得了胜利!!!", playerName[1]);                    break;                }                #endregion            }            #endregion            Win();            Console.ReadKey();        }        /// <summary>        /// 绘制游戏头        /// </summary>        public static void GameHead()        {            Console.ForegroundColor = ConsoleColor.Blue;            Console.WriteLine("*******************************");            Console.ForegroundColor = ConsoleColor.Green;            Console.WriteLine("***********飞行棋5.1***********");            Console.ForegroundColor = ConsoleColor.Red;            Console.WriteLine("*******************************");            Console.ForegroundColor = ConsoleColor.White;        }        //初始化地图        public static void InitialMap()        {            //幸运轮盘            int[] luckyTurn = { 6,16,36,66};            for (int i = 0; i < luckyTurn.Length; i++)            {                Maps[luckyTurn[i]] = 1;            }            //地雷            int[] landMine = { 4,25,48,74,89};            for (int i = 0; i < landMine.Length; i++)            {                Maps[landMine[i]] = 2;            }            //暂停            int[] pause = { 12,15,58,69,80,90};            for (int i = 0; i < pause.Length; i++)            {                Maps[pause[i]] = 3;            }            //时空隧道            int[] space = { 30,59,76,90};            for (int i = 0; i < space.Length; i++)            {                Maps[space[i]] = 4;            }                    }        //画地图        public static void drawMap()        {            #region 第一横行            for (int i = 0; i <= 29; i++)            {                Console.Write(drawStringMap(i));            }            Console.WriteLine();            #endregion            #region 第一竖行            for (int i = 30; i <= 34; i++)            {                for (int j = 0; j <= 29; j++)                {                    Console.Write("  ");                }                Console.WriteLine(drawStringMap(i));            }            #endregion            #region 第二横行            for (int i = 64; i >= 35; i--)            {                Console.Write(drawStringMap(i));            }            Console.WriteLine();            #endregion            #region 第二竖行            for (int i = 65; i < 70; i++)            {                Console.WriteLine(drawStringMap(i));            }            #endregion            #region 第三横行            for (int i = 70; i < 100; i++)            {                Console.Write(drawStringMap(i));            }            Console.WriteLine();            #endregion        }        //抽象出地图的形状        public static string drawStringMap(int i)        {            // □  ◎  ☆  ▲  卐            string str = "";            if (playerPos[0]==playerPos[1]&&playerPos[1]==i)            {                Console.Write("<>");            }            else if (playerPos[0]==i)            {                Console.Write("A");            }            else if (playerPos[1]==i)            {                Console.Write("B");            }            else            {                switch (Maps[i])                {                    case 0:                        Console.ForegroundColor = ConsoleColor.DarkGreen;                        str = "□";                        break;                    case 1:                        Console.ForegroundColor = ConsoleColor.DarkRed;                        str = "◎";                        break;                    case 2:                        Console.ForegroundColor = ConsoleColor.DarkBlue;                        str = "☆";                        break;                    case 3:                        Console.ForegroundColor = ConsoleColor.DarkYellow;                        str = "▲";                        break;                    case 4:                        Console.ForegroundColor = ConsoleColor.DarkCyan;                        str = "卐";                        break;                    default:                        break;                }            }            return str;        }                //玩游戏        public static void PlayGame(int playerNumber)        {            Console.WriteLine("请按任意键开始游戏");            Console.ReadKey(true);            #region 掷骰子            Random r = new Random();            int rNumber=r.Next(1,7);            #endregion            Console.WriteLine("玩家{0}掷出了{1}.",playerName[playerNumber],rNumber);            Console.ReadKey(true);            Console.WriteLine("玩家{0}开始行动",playerName[playerNumber]);            Console.ReadKey(true);            playerPos[playerNumber] += rNumber;            Console.WriteLine("玩家{0}前进了{1}格",playerName[playerNumber],rNumber);            if (playerPos[1]==playerPos[0])            {                Console.ReadKey(true);                Console.WriteLine("玩家{0}踩到了玩家{1},玩家{2}需要后退6格.",playerName[playerNumber],playerName[1-playerNumber],playerName[1-playerNumber]);                playerPos[1 - playerNumber] -= 6;                chanPos();                Console.ReadKey(true);                Console.WriteLine("玩家{0}已经行动结束",playerName[1-playerNumber]);            }            else            {                Console.ReadKey(true);                switch (Maps[playerPos[playerNumber]])                {                    case 0:                        //Console.ReadKey(true);                        Console.WriteLine("玩家踩到方格,什么事也不会发生");                        //Console.ReadKey(true);                        break;                    case 1:                        Console.WriteLine("玩家{0}踩到幸运轮盘,可以选择:  1  前进6格,  2  轰炸对方.",playerName[playerNumber]);                        string input = Console.ReadLine();                        while (input=="")                        {                            Console.ReadKey(true);                            Console.WriteLine("请重新输入");                            input = Console.ReadLine();                        }                        if (input=="1")                        {                            Console.ReadKey(true);                            Console.WriteLine("玩家{0}可以前进6格",playerName[playerNumber]);                            playerPos[playerNumber] += 6;                            chanPos();                            Console.WriteLine("行动完成");                        }                        if (input=="2")                        {                            Console.ReadKey(true);                            Console.WriteLine("玩家{0}选择轰炸对方,玩家{1}需要后退8格",playerName[playerNumber],playerName[1-playerNumber]);                            playerPos[1 - playerNumber] -= 8;                            chanPos();                            Console.WriteLine("行动完成");                        }                        break;                    case 2:                        Console.ReadKey(true);                        Console.WriteLine("玩家{0}踩到地雷,需要后退6格",playerName[playerNumber]);                        playerPos[playerNumber] -= 6;                        chanPos();                        break;                    case 3:                        Console.ReadKey(true);                        Console.WriteLine("玩家{0}被暂停一局",playerName[playerNumber]);                        flags[playerNumber] = true;                        break;                    case 4:                        Console.ReadKey(true);                        Console.WriteLine("玩家{0}进行时空穿梭,瞬间传送10格",playerName[playerNumber]);                        playerPos[playerNumber] += 10;                        chanPos();                        break;                    default:                        break;                }            }            Console.Clear();            drawMap();            }        //判断玩家是否出界        public static void chanPos()        {            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;            }        }        //获胜        public static void Win()        {            Console.ForegroundColor = ConsoleColor.Red;            Console.WriteLine("*****************************");            Console.ForegroundColor = ConsoleColor.Red;            Console.WriteLine("************胜利************");            Console.ForegroundColor = ConsoleColor.Red;            Console.WriteLine("*****************************");        }    }}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表