C# 实现飞行棋小游戏

这是一个使用C#编写的飞行棋小游戏,包含了游戏规则、地图初始化、玩家交互等功能。玩家可以输入名字,游戏过程中会遇到各种事件,如幸运轮盘、地雷、暂停和时空隧道等,最终先到达终点的玩家获胜。游戏画面通过控制台展示,地图元素用不同符号表示,具有一定的趣味性和互动性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace demo1
{

    /** 游戏规则
     * 玩家A踩到了玩家B 玩家B退6格
     * 踩到地雷 退6格
     * 踩到时空隧道 进10格
     * 踩到幸运轮盘 1 交换位置 2 轰炸对方 使对方退6格
     * 踩到暂停 暂停一回合
     * 踩到方块 什么都不干
    */

    class Program
    {
        // 静态变量模拟全局变量
        static int[] Maps = new int[100];
        // 静态数组 存储玩家A和玩家B的位置
        static int[] PlayerPos = new int[2];
        // 存储两个玩家的姓名
        static string[] PlayerName = new string[2];
        // 用来暂停, 默认是false
        static bool[] Flag = new bool[2];


        static void Main(string[] args)
        {
            GameShow();
            // 输入玩家姓名
            InputName();
            // 玩家姓名输入之后, 清屏
            Console.Clear();
            GameShow();
            Console.WriteLine("{0}的玩家用A表示\n{1}的玩家用B表示", PlayerName[0], PlayerName[1]);
            // 初始化地图
            InitialMap();
            // 绘制地图
            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.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine("玩家{0}赢了玩家{1}, 率先到达终点!!!", PlayerName[0], PlayerName[1]);
                    break;
                }

                if (Flag[1] == false)
                {
                    PlayGame(1);
                }
                else
                {
                    Flag[1] = false;
                }
                if (PlayerPos[1] >= 99)
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine("玩家{0}赢了玩家{1}, 率先到达终点!!!", PlayerName[1], PlayerName[0]);
                    break;
                }
            }// while

            Win();

            Console.ReadKey();
        }

        /// <summary>
        /// 胜利
        /// </summary>
        public static void Win()
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("■■■■ ■  ■       ■■■    ■");
            Console.WriteLine("■  ■ ■  ■    ■■■■   ■  ■");
            Console.WriteLine("■  ■ ■■■■■■■   ■    ■  ■");
            Console.WriteLine("■■■■■   ■      ■    ■  ■");
            Console.WriteLine("■  ■■   ■   ■■■■■■■ ■  ■");
            Console.WriteLine("■  ■    ■      ■    ■  ■");
            Console.WriteLine("■  ■ ■■■■■■   ■■■   ■  ■");
            Console.WriteLine("■■■■    ■    ■ ■ ■  ■  ■");
            Console.WriteLine("■  ■    ■    ■ ■  ■ ■  ■");
            Console.WriteLine("■  ■    ■   ■  ■    ■  ■");
            Console.WriteLine("■  ■    ■      ■       ■");
            Console.WriteLine(" ■■ ■■■■■■■■   ■     ■■■");

        }

        /// <summary>
        /// 画游戏头
        /// </summary>
        public static void GameShow()
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("**********************");
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("**********************");
            Console.ForegroundColor = ConsoleColor.DarkMagenta;
            Console.WriteLine("**********************");
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.WriteLine("***自制飞行棋小游戏***");
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine("**********************");
            Console.ForegroundColor = ConsoleColor.Gray;
            Console.WriteLine("**********************");
        }

        /// <summary>
        /// 初始化地图
        /// </summary>
        public static void InitialMap()
        {
            int[] luckyturn = { 6, 23, 40, 55, 69, 83 }; // 幸运轮盘◎

            for (int i = 0; i < luckyturn.Length; i++) Maps[luckyturn[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;
        }

        /// <summary>
        /// 绘制地图
        /// </summary>
        public static void DrawMap()
        {
            Console.WriteLine("图例:幸运轮盘:◎   地雷:☆   暂停:▲   时空隧道:〓");
            #region 第一横行
            for (int i = 0; i <= 29; i++)
            {
                Console.Write(DrawStringMap(i));
            }
            #endregion

            // 换行
            Console.WriteLine();

            #region 第一竖行
            for (int i = 30; i <= 34; i++)
            {
                for (int j = 0; j < 29; 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();
        } // DrawMap

        /// <summary>
        /// 绘制地图图形
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        public static string DrawStringMap(int i)
        {
            string str = "";
            #region 画图
            // 玩家A与玩家B坐标相同,并且都在地图上 画<>
            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.Blue;
                        str = "□";
                        break;
                    case 1:
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        str = "◎";
                        break;
                    case 2:
                        Console.ForegroundColor = ConsoleColor.Red;
                        str = "☆";
                        break;
                    case 3:
                        Console.ForegroundColor = ConsoleColor.Green;
                        str = "▲";
                        break;
                    case 4:
                        Console.ForegroundColor = ConsoleColor.Cyan;
                        str = "〓";
                        break;
                } // switch
            } // else
            #endregion
            return str;
        } // DrawStringMap

        /// <summary>
        /// 玩家姓名输入
        /// </summary>
        public static void InputName()
        {
            #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姓名不能为空, 请重新输入: ");
                }
                else
                {
                    Console.WriteLine("玩家B的姓名不能与玩家A相同, 请重新输入: ");
                }
                PlayerName[1] = Console.ReadLine();
            }
            #endregion
        }

        /// <summary>
        /// 玩游戏
        /// </summary>
        public static void PlayGame(int playerNum)
        {
            Random r = new Random();
            int rNum = r.Next(1, 7);

            Console.WriteLine("玩家{0}按任意键开始掷骰子", PlayerName[playerNum]);
            // 将按下的键不显示在窗口中
            Console.ReadKey(true);

            Console.WriteLine("玩家{0}掷出了{1}", PlayerName[playerNum], rNum);
            PlayerPos[playerNum] += rNum;
            ChangePos();
            Console.ReadKey(true);

            Console.WriteLine("玩家{0}按任意键开始行动", PlayerName[playerNum]);
            Console.ReadKey(true);

            Console.WriteLine("玩家{0}行动完了", PlayerName[playerNum]);
            Console.ReadKey(true);

            // 玩家A踩到了玩家B 方块 幸运轮盘 地雷 暂停 时空隧道
            if (PlayerPos[playerNum] == PlayerPos[1 - playerNum])
            {
                Console.WriteLine("玩家{0}踩到了玩家{1}, 玩家{2}退6格", PlayerName[playerNum], PlayerName[1 - playerNum], PlayerName[1 - playerNum]);
                PlayerPos[1 - playerNum] -= 6;
                ChangePos();
                Console.ReadKey(true);
            }
            else // 踩到了其他关卡上
            {
                // 玩家坐标
                switch (Maps[PlayerPos[playerNum]])
                {
                    case 0:
                        Console.WriteLine("玩家{0}踩到方块,安全", PlayerName[playerNum]);
                        Console.ReadKey(true);
                        break;
                    case 1:
                        Console.WriteLine("玩家{0}踩到了幸运轮盘,请选择1-交换位置 2-轰炸对方", PlayerName[playerNum]);
                        string input = Console.ReadLine();
                        while (true)
                        {
                            if (input == "1")
                            {
                                Console.WriteLine("玩家{0}选择与玩家{1}交换位置", PlayerName[playerNum], PlayerName[1 - playerNum]);
                                Console.ReadKey(true);
                                int tmp = PlayerPos[playerNum];
                                PlayerPos[playerNum] = PlayerPos[1 - playerNum];
                                PlayerPos[1 - playerNum] = tmp;
                                Console.WriteLine("交换成功!!!按任意键继续游戏!!!!");
                                Console.ReadKey(true);
                                break;
                            }
                            else if (input == "2")
                            {
                                Console.WriteLine("玩家{0}选择轰炸玩家{1}, 玩家{2}退6格", PlayerName[playerNum], PlayerName[1 - playerNum], PlayerName[1 - playerNum]);
                                Console.ReadKey(true);
                                PlayerPos[1 - playerNum] -= 6;
                                ChangePos();
                                Console.WriteLine("玩家{0}已经退了6格", PlayerName[1 - playerNum]);
                                Console.ReadKey(true);
                                break;
                            }
                            else
                            {
                                Console.WriteLine("只能输入1或2 1-交换位置 2-轰炸对方");
                                input = Console.ReadLine();
                            }
                        }
                        break;
                    case 2:
                        Console.WriteLine("玩家{0}踩到了地雷 退6格", PlayerName[playerNum]);
                        Console.ReadKey(true);
                        PlayerPos[playerNum] -= 6;
                        ChangePos();
                        break;
                    case 3:
                        Console.WriteLine("玩家{0}踩到了暂停 暂停一回合", PlayerName[playerNum]);
                        Console.ReadKey(true);
                        Flag[playerNum] = true;
                        break;
                    case 4:
                        Console.WriteLine("玩家{0}踩到了时空隧道 前进10格", PlayerName[playerNum]);
                        Console.ReadKey(true);
                        PlayerPos[playerNum] += 10;
                        ChangePos();
                        break;
                }// switch
            }// else
            ChangePos();
            // 清屏
            Console.Clear();
            DrawMap();
        } // PlayGame

        /// <summary>
        /// 当玩家坐标发生改变的时候
        /// </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;
            }
        }

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值