控制台C#飞行棋小项目

 ​​​​​​​目录

玩法介绍: 

游戏过程

初始界面

输入两名玩家的名字,进入游戏界面

​编辑 开始游戏

某一个玩家走到终点 

代码实现部分 


玩法介绍: 

        通过控制台实现一个基于C#语言制作的控制台飞行器小游戏,输入两名用户的名字,进入游戏后两名玩家的士兵用“A”“B”表示,在地图上共有100个格子,不同的符号表示不同的功能,包括
幸运轮盘:◎(可以和另一名玩家交换位置,或者轰炸另一位玩家,被轰炸玩家会退6格),地雷:★(踩到地雷的玩家会退6格),暂停:▲(踩到暂停的玩家会被暂停一轮,不能掷骰子),时空隧道:卍(踩到时空隧道的玩家会向前前进10格)。同时如果,某个玩家在前进的终点位置正好是另一位玩家,那么另一位玩家会被退回6格。

游戏过程

初始界面

输入两名玩家的名字,进入游戏界面

 开始游戏

某一个玩家走到终点 

代码实现部分 

using System;

namespace 飞行器项目
{
    class Program
    {
        // 用静态字段来模拟全局变量
        public static int[] Maps = new int[100];
        //声明一个静态数组用来存储玩家A和玩家B的坐标
        public static int[] PlayerPos = new int[2];
        // 声明一个用来存放玩家姓名的数组
        static string[] PlayerNames = new string[2];
        // 判断玩家是否踩到了暂停一轮
        public static bool[] Flags = new bool[2];// 默认都是false
        static void Main(string[] args)
        {
            Program.GameShow();
            Console.WriteLine("请输入玩家A的姓名");
            PlayerNames[0] = Console.ReadLine();
            while (PlayerNames[0] == "")
            {
                Console.WriteLine("玩家姓名不能为空,请重新输入");
                PlayerNames[0] = Console.ReadLine();
            }
            Console.WriteLine("请输入玩家B的姓名");
            PlayerNames[1] = Console.ReadLine();
            while (true)
            {
                if (PlayerNames[1] == "")
                {
                    Console.WriteLine("玩家姓名不能为空,请重新输入");
                    PlayerNames[1] = Console.ReadLine();
                }
                else if (PlayerNames[1] == PlayerNames[0])
                {
                    Console.WriteLine("玩家B与玩家A的名字不能相同,请重现输入");
                    PlayerNames[1] = Console.ReadLine();
                }
                else
                {
                    break;
                }
            }
            // 输入姓名完成后清平
            Console.Clear();
            Program.GameShow();
            Console.WriteLine("{0}的士兵用A表示\n{1}的士兵用B表示", PlayerNames[0], PlayerNames[1]);
            Program.IntailMap();
            Program.ShowMap();

            // 当玩家A和玩家B都不在终点时就不停的玩下去
            while (PlayerPos[0] < 99 && PlayerPos[1] < 99)
            {
                if (Flags[0] == false)
                {
                    PlayGame(0);
                }
                else
                {
                    Flags[0] = false;
                }
                if (PlayerPos[0] >= 99)
                {
                    Console.WriteLine("{0}玩家赢了{1}玩家,恭喜!!!", PlayerNames[0], PlayerNames[1]);
                    break;
                }
                if (Flags[1] == false)
                {
                    PlayGame(1);
                }
                else
                {
                    Flags[1] = false;
                }
                if (PlayerPos[1] >= 99)
                {
                    Console.WriteLine("{0}玩家赢了{1}玩家,恭喜!!!", PlayerNames[1], PlayerNames[0]);
                    break;
                }
            }//while
        }
        public static void GameShow()
        {
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("**********************");
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("**********************");
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("**********************");
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.WriteLine("******飞行棋项目******");
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine("**********************");
            Console.ForegroundColor = ConsoleColor.DarkGray;
            Console.WriteLine("**********************");
            Console.ForegroundColor = ConsoleColor.DarkBlue;
            Console.WriteLine("**********************");
            Console.ForegroundColor = ConsoleColor.DarkGreen;
        }
        public static void IntailMap()
        {
            int[] luckturn = { 6, 23, 40, 55, 69, 83 };//轮盘
            for (int i = 0; i < luckturn.Length; i++)
            {
                Maps[luckturn[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, 27, 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;
            }
        }
        public static void ShowMap()
        {
            Console.WriteLine("图例:\n幸运轮盘:◎\t地雷:★\t暂停:▲\t时空隧道:卍");
            // 第一横行
            for (int i = 0; i < 30; i++)
            {
                DrawstringMap(i);
            }
            Console.WriteLine();

            //第一竖行
            for (int i = 30; i < 35; i++)
            {
                for (int j = 0; j < 29; j++)
                {
                    Console.ForegroundColor = ConsoleColor.Black;
                    Console.Write("**");
                }
                DrawstringMap(i);
                Console.WriteLine();
            }

            // 第二横行
            for (int i = 64; i >= 35; i--)
            {
                DrawstringMap(i);
            }
            Console.WriteLine();

            // 第二竖行
            for (int i = 65; i < 70; i++)
            {
                DrawstringMap(i);
                Console.WriteLine();
            }

            // 第三横行
            for (int i = 70; i < 100; i++)
            {
                DrawstringMap(i);
            }
            Console.WriteLine();
            static void DrawstringMap(int i)
            {
                // 如果玩家A和B坐标相同,画<>
                if (PlayerPos[0] == PlayerPos[1] && PlayerPos[0] == i)
                {
                    Console.ForegroundColor = ConsoleColor.DarkGray;
                    Console.Write("<>");
                }
                else if (PlayerPos[0] == i)
                {
                    Console.ForegroundColor = ConsoleColor.DarkMagenta;
                    Console.Write("A");
                }
                else if (PlayerPos[1] == i)
                {
                    Console.ForegroundColor = ConsoleColor.DarkBlue;
                    Console.Write("B");
                }
                else
                {
                    switch (Maps[i])
                    {
                        case 0:
                            Console.ForegroundColor = ConsoleColor.Yellow;
                            Console.Write("□");
                            break;
                        case 1:
                            Console.ForegroundColor = ConsoleColor.Red;
                            Console.Write("◎");
                            break;
                        case 2:
                            Console.ForegroundColor = ConsoleColor.Green;
                            Console.Write("★");
                            break;
                        case 3:
                            Console.ForegroundColor = ConsoleColor.Blue;
                            Console.Write("▲");
                            break;
                        case 4:
                            Console.ForegroundColor = ConsoleColor.Cyan;
                            Console.Write("卍");
                            break;
                    }
                }
            }
        }
        public static void PlayGame(int playerNumber)
        {
            Random r = new Random();
            int rnumber = r.Next(1, 7);
            Console.WriteLine("{0}按任意键开始掷骰子", PlayerNames[playerNumber]);
            Console.ReadKey(true);
            Console.WriteLine("{0}掷出了{1}", PlayerNames[playerNumber], rnumber);
            PlayerPos[playerNumber] += rnumber;
            ChangePos();
            Console.ReadKey(true);
            Console.WriteLine("{0}按任意键开始行动", PlayerNames[playerNumber]);
            Console.ReadKey(true);
            Console.WriteLine("{0}行动结束", PlayerNames[playerNumber]);
            Console.ReadKey(true);
            if (PlayerPos[playerNumber] == PlayerPos[1 - playerNumber])
            {
                Console.WriteLine("玩家{0}踩到了玩家{1},玩家{1}退6格", PlayerNames[playerNumber], PlayerNames[1 - playerNumber]);
                PlayerPos[1 - playerNumber] -= 6;
                ChangePos();
                Console.ReadKey(true);
            }
            else
            {
                switch (Maps[PlayerPos[playerNumber]])
                {
                    case 0:
                        Console.WriteLine("{0}踩到了方块,什么都不发生", PlayerNames[playerNumber]);
                        Console.ReadKey(true);
                        break;
                    case 1:
                        Console.WriteLine("{0}踩到了幸运轮盘", PlayerNames[playerNumber]);
                        Console.WriteLine("请选择1---交换位置,2---轰炸对方");
                        string input = Console.ReadLine();
                        while (true)
                        {
                            if (input == "1")
                            {
                                Console.WriteLine("{0}选择和{1}交换位置", PlayerNames[playerNumber], PlayerNames[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},玩家{1}退6格!!!", PlayerNames[playerNumber], PlayerNames[1 - playerNumber]);
                                Console.ReadKey(true);
                                PlayerPos[1 - playerNumber] -= 6;
                                ChangePos();
                                Console.WriteLine("玩家{0}退了6格", PlayerNames[1 - playerNumber]);
                                Console.ReadKey(true);
                                break;
                            }
                            else
                            {
                                Console.WriteLine("只能输入1或者2,1---交换位置,2---轰炸对方!!!");
                                input = Console.ReadLine();
                            }
                        }
                        break;
                    case 2:
                        Console.WriteLine("玩家{0}踩到了地雷,需要退6格", PlayerNames[playerNumber]);
                        Console.ReadKey(true);
                        PlayerPos[playerNumber] -= 6;
                        ChangePos();
                        Console.WriteLine("玩家{0}退了6格", PlayerNames[playerNumber]);
                        break;
                    case 3:
                        Console.WriteLine("玩家{0}踩到了暂停,暂停一回合", PlayerNames[playerNumber]);
                        Flags[playerNumber] = true;
                        Console.ReadKey(true);
                        break;
                    case 4:
                        Console.WriteLine("玩家{0}踩到了时空隧道,前进10格", PlayerNames[playerNumber]);
                        PlayerPos[playerNumber] += 10;
                        ChangePos();
                        Console.ReadKey(true);
                        break;

                }// switch
            }// else
            ChangePos();
            Console.Clear();
            Program.ShowMap();
        }
        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;
            }
        }
    }
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值